mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-04-24 15:06:59 -05:00
kbinxml(cargo): upgrade snafu to 0.7.0
This commit is contained in:
parent
78f7a9b93a
commit
948152e900
|
|
@ -15,4 +15,4 @@ lazy_static = "1.0.0"
|
|||
log = "0.4.6"
|
||||
quick-xml = "0.22.0"
|
||||
rustc-hex = "2.0.1"
|
||||
snafu = "0.6.0"
|
||||
snafu = "0.7.0"
|
||||
|
|
|
|||
|
|
@ -151,13 +151,13 @@ impl ByteBufferRead {
|
|||
|
||||
self.cursor
|
||||
.seek(SeekFrom::Current(size as i64))
|
||||
.context(ReadSizeSeek { size })?;
|
||||
.context(ReadSizeSeekSnafu { size })?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub fn buf_read(&mut self) -> Result<Bytes, ByteBufferError> {
|
||||
let size = self.cursor.read_u32::<BigEndian>().context(ReadSize)?;
|
||||
let size = self.cursor.read_u32::<BigEndian>().context(ReadSizeSnafu)?;
|
||||
debug!(
|
||||
"buf_read => index: {}, size: {}",
|
||||
self.cursor.position(),
|
||||
|
|
@ -208,7 +208,7 @@ impl ByteBufferRead {
|
|||
let data = self
|
||||
.buf_read_size(size as usize)
|
||||
.map_err(Box::new)
|
||||
.context(ReadAligned { size })?;
|
||||
.context(ReadAlignedSnafu { size })?;
|
||||
self.realign_reads(None)?;
|
||||
|
||||
(false, data)
|
||||
|
|
@ -226,7 +226,7 @@ impl ByteBufferRead {
|
|||
if old_pos < trailing {
|
||||
self.cursor
|
||||
.seek(SeekFrom::Start(trailing as u64))
|
||||
.context(SeekOffset { offset: trailing })?;
|
||||
.context(SeekOffsetSnafu { offset: trailing })?;
|
||||
self.realign_reads(None)?;
|
||||
}
|
||||
}
|
||||
|
|
@ -245,7 +245,7 @@ impl ByteBufferRead {
|
|||
while self.cursor.position() % size > 0 {
|
||||
self.cursor
|
||||
.seek(SeekFrom::Current(1))
|
||||
.context(SeekForward { size: 1usize })?;
|
||||
.context(SeekForwardSnafu { size: 1usize })?;
|
||||
}
|
||||
trace!("realign_reads => realigned to: {}", self.cursor.position());
|
||||
|
||||
|
|
@ -279,14 +279,14 @@ impl ByteBufferWrite {
|
|||
pub fn buf_write(&mut self, data: &[u8]) -> Result<(), ByteBufferError> {
|
||||
self.buffer
|
||||
.write_u32::<BigEndian>(data.len() as u32)
|
||||
.context(WriteLength { len: data.len() })?;
|
||||
.context(WriteLengthSnafu { len: data.len() })?;
|
||||
debug!(
|
||||
"buf_write => index: {}, size: {}",
|
||||
self.buffer.position(),
|
||||
data.len()
|
||||
);
|
||||
|
||||
self.buffer.write_all(data).context(WriteDataBlock)?;
|
||||
self.buffer.write_all(data).context(WriteDataBlockSnafu)?;
|
||||
trace!(
|
||||
"buf_write => index: {}, size: {}, data: 0x{:02x?}",
|
||||
self.buffer.position(),
|
||||
|
|
@ -306,7 +306,7 @@ impl ByteBufferWrite {
|
|||
data.as_bytes()
|
||||
);
|
||||
|
||||
let bytes = encoding.encode_bytes(data).context(StringEncode)?;
|
||||
let bytes = encoding.encode_bytes(data).context(StringEncodeSnafu)?;
|
||||
self.buf_write(&bytes)?;
|
||||
|
||||
Ok(())
|
||||
|
|
@ -347,17 +347,17 @@ impl ByteBufferWrite {
|
|||
if self.offset_1 % 4 == 0 {
|
||||
self.buffer
|
||||
.write_u32::<BigEndian>(0)
|
||||
.context(WritePadding { size: 4usize })?;
|
||||
.context(WritePaddingSnafu { size: 4usize })?;
|
||||
}
|
||||
|
||||
self.buffer
|
||||
.seek(SeekFrom::Start(self.offset_1))
|
||||
.context(SeekOffset {
|
||||
.context(SeekOffsetSnafu {
|
||||
offset: self.offset_1 as usize,
|
||||
})?;
|
||||
self.buffer
|
||||
.write_u8(data[0])
|
||||
.context(WriteDataByte { offset: 1usize })?;
|
||||
.context(WriteDataByteSnafu { offset: 1usize })?;
|
||||
self.offset_1 += 1;
|
||||
|
||||
true
|
||||
|
|
@ -367,26 +367,26 @@ impl ByteBufferWrite {
|
|||
if self.offset_2 % 4 == 0 {
|
||||
self.buffer
|
||||
.write_u32::<BigEndian>(0)
|
||||
.context(WritePadding { size: 4usize })?;
|
||||
.context(WritePaddingSnafu { size: 4usize })?;
|
||||
}
|
||||
|
||||
self.buffer
|
||||
.seek(SeekFrom::Start(self.offset_2))
|
||||
.context(SeekOffset {
|
||||
.context(SeekOffsetSnafu {
|
||||
offset: self.offset_2 as usize,
|
||||
})?;
|
||||
self.buffer
|
||||
.write_u8(data[0])
|
||||
.context(WriteDataByte { offset: 1usize })?;
|
||||
.context(WriteDataByteSnafu { offset: 1usize })?;
|
||||
self.buffer
|
||||
.write_u8(data[1])
|
||||
.context(WriteDataByte { offset: 2usize })?;
|
||||
.context(WriteDataByteSnafu { offset: 2usize })?;
|
||||
self.offset_2 += 2;
|
||||
|
||||
true
|
||||
},
|
||||
_ => {
|
||||
self.buffer.write_all(data).context(WriteDataBlock)?;
|
||||
self.buffer.write_all(data).context(WriteDataBlockSnafu)?;
|
||||
self.realign_writes(None)?;
|
||||
|
||||
false
|
||||
|
|
@ -396,7 +396,7 @@ impl ByteBufferWrite {
|
|||
if check_old {
|
||||
self.buffer
|
||||
.seek(SeekFrom::Start(old_pos))
|
||||
.context(SeekOffset {
|
||||
.context(SeekOffsetSnafu {
|
||||
offset: old_pos as usize,
|
||||
})?;
|
||||
|
||||
|
|
@ -409,7 +409,7 @@ impl ByteBufferWrite {
|
|||
if old_pos < trailing {
|
||||
self.buffer
|
||||
.seek(SeekFrom::Start(trailing))
|
||||
.context(SeekOffset {
|
||||
.context(SeekOffsetSnafu {
|
||||
offset: trailing as usize,
|
||||
})?;
|
||||
self.realign_writes(None)?;
|
||||
|
|
@ -430,7 +430,7 @@ impl ByteBufferWrite {
|
|||
while self.buffer.position() % size > 0 {
|
||||
self.buffer
|
||||
.write_u8(0)
|
||||
.context(WritePadding { size: 1usize })?;
|
||||
.context(WritePaddingSnafu { size: 1usize })?;
|
||||
}
|
||||
|
||||
trace!("realign_writes => realigned to: {}", self.buffer.position());
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ impl EncodingType {
|
|||
// ASCII only goes up to 0x7F
|
||||
match input.iter().position(|&ch| ch >= 0x80) {
|
||||
Some(index) => Err(EncodingError::InvalidAscii { index }),
|
||||
None => String::from_utf8(input.to_vec()).context(InvalidUtf8),
|
||||
None => String::from_utf8(input.to_vec()).context(InvalidUtf8Snafu),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -181,7 +181,7 @@ impl EncodingType {
|
|||
pub fn decode_bytes(&self, input: &[u8]) -> Result<String, EncodingError> {
|
||||
match *self {
|
||||
EncodingType::None | EncodingType::UTF_8 => {
|
||||
String::from_utf8(input.to_vec()).context(InvalidUtf8)
|
||||
String::from_utf8(input.to_vec()).context(InvalidUtf8Snafu)
|
||||
},
|
||||
|
||||
EncodingType::ASCII => Self::decode_ascii(input),
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ use crate::writer::WriterError;
|
|||
pub type Result<T> = StdResult<T, KbinError>;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility = "pub(crate)")]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
pub enum KbinError {
|
||||
#[snafu(display("Unable to read bytes or not enough data read"))]
|
||||
DataConvert { source: io::Error },
|
||||
|
|
@ -58,7 +58,7 @@ pub enum KbinError {
|
|||
},
|
||||
|
||||
#[snafu(display("Unable to convert from hexadecimal"))]
|
||||
HexError { source: FromHexError },
|
||||
Hex { source: FromHexError },
|
||||
|
||||
#[snafu(display("Type mismatch, expected: {}, found: {}", expected, found))]
|
||||
TypeMismatch {
|
||||
|
|
|
|||
|
|
@ -98,17 +98,18 @@ impl Reader {
|
|||
pub fn new(input: Bytes) -> Result<Self, ReaderError> {
|
||||
let mut header = Cursor::new(&input);
|
||||
|
||||
let signature = header.read_u8().context(Signature)?;
|
||||
let signature = header.read_u8().context(SignatureSnafu)?;
|
||||
if signature != SIGNATURE {
|
||||
return Err(ReaderError::InvalidSignature { signature });
|
||||
}
|
||||
|
||||
let compress_byte = header.read_u8().context(Compression)?;
|
||||
let compression = CompressionType::from_byte(compress_byte).context(InvalidCompression)?;
|
||||
let compress_byte = header.read_u8().context(CompressionSnafu)?;
|
||||
let compression =
|
||||
CompressionType::from_byte(compress_byte).context(InvalidCompressionSnafu)?;
|
||||
|
||||
let encoding_byte = header.read_u8().context(Encoding)?;
|
||||
let encoding_negation = header.read_u8().context(EncodingNegate)?;
|
||||
let encoding = EncodingType::from_byte(encoding_byte).context(InvalidEncoding)?;
|
||||
let encoding_byte = header.read_u8().context(EncodingSnafu)?;
|
||||
let encoding_negation = header.read_u8().context(EncodingNegateSnafu)?;
|
||||
let encoding = EncodingType::from_byte(encoding_byte).context(InvalidEncodingSnafu)?;
|
||||
if encoding_negation != !encoding_byte {
|
||||
return Err(ReaderError::MismatchedEncoding);
|
||||
}
|
||||
|
|
@ -118,15 +119,19 @@ impl Reader {
|
|||
signature, compress_byte, compression, encoding_byte, encoding
|
||||
);
|
||||
|
||||
let len_node = header.read_u32::<BigEndian>().context(NodeBufferLength)?;
|
||||
let len_node = header
|
||||
.read_u32::<BigEndian>()
|
||||
.context(NodeBufferLengthSnafu)?;
|
||||
info!("len_node: {0} (0x{0:x})", len_node);
|
||||
|
||||
// The length of the data buffer is the 4 bytes right after the node buffer.
|
||||
header
|
||||
.seek(SeekFrom::Current(len_node as i64))
|
||||
.context(DataLengthSeek { len_node })?;
|
||||
.context(DataLengthSeekSnafu { len_node })?;
|
||||
|
||||
let len_data = header.read_u32::<BigEndian>().context(DataBufferLength)?;
|
||||
let len_data = header
|
||||
.read_u32::<BigEndian>()
|
||||
.context(DataBufferLengthSnafu)?;
|
||||
info!("len_data: {0} (0x{0:x})", len_data);
|
||||
|
||||
// We have read 8 bytes so far, so offset the start of the node buffer from
|
||||
|
|
@ -152,7 +157,7 @@ impl Reader {
|
|||
let is_array = raw_node_type & ARRAY_MASK == ARRAY_MASK;
|
||||
let node_type = raw_node_type & !ARRAY_MASK;
|
||||
|
||||
let xml_type = StandardType::from_u8(node_type).context(InvalidNodeType)?;
|
||||
let xml_type = StandardType::from_u8(node_type).context(InvalidNodeTypeSnafu)?;
|
||||
debug!(
|
||||
"Reader::parse_node_type() => raw_node_type: {}, node_type: {:?} ({}), is_array: {}",
|
||||
raw_node_type, xml_type, node_type, is_array
|
||||
|
|
@ -177,7 +182,7 @@ impl Reader {
|
|||
pub fn read_node_type(&mut self) -> Result<(StandardType, bool), ReaderError> {
|
||||
self.check_if_node_buffer_end()?;
|
||||
|
||||
let raw_node_type = self.node_buf.read_u8().context(NodeType)?;
|
||||
let raw_node_type = self.node_buf.read_u8().context(NodeTypeSnafu)?;
|
||||
let value = Self::parse_node_type(raw_node_type)?;
|
||||
|
||||
Ok(value)
|
||||
|
|
@ -195,27 +200,31 @@ impl Reader {
|
|||
);
|
||||
|
||||
let value = match node_type {
|
||||
StandardType::Attribute | StandardType::String => {
|
||||
self.data_buf.buf_read().context(DataBuffer { node_type })?
|
||||
},
|
||||
StandardType::Binary => self.read_bytes().context(DataBuffer { node_type })?,
|
||||
StandardType::Attribute | StandardType::String => self
|
||||
.data_buf
|
||||
.buf_read()
|
||||
.context(DataBufferSnafu { node_type })?,
|
||||
StandardType::Binary => self.read_bytes().context(DataBufferSnafu { node_type })?,
|
||||
StandardType::NodeStart | StandardType::NodeEnd | StandardType::FileEnd => Bytes::new(),
|
||||
node_type if is_array => {
|
||||
let arr_size = self.data_buf.read_u32::<BigEndian>().context(ArrayLength)?;
|
||||
let arr_size = self
|
||||
.data_buf
|
||||
.read_u32::<BigEndian>()
|
||||
.context(ArrayLengthSnafu)?;
|
||||
let data = self
|
||||
.data_buf
|
||||
.get(arr_size)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
self.data_buf
|
||||
.realign_reads(None)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
|
||||
data
|
||||
},
|
||||
node_type => self
|
||||
.data_buf
|
||||
.get_aligned(node_type)
|
||||
.context(DataBuffer { node_type })?,
|
||||
.context(DataBufferSnafu { node_type })?,
|
||||
};
|
||||
debug!(
|
||||
"Reader::read_node_data(node_type: {:?}, is_array: {}) => value: 0x{:02x?}",
|
||||
|
|
@ -237,22 +246,23 @@ impl Reader {
|
|||
_ => {
|
||||
let key = match self.compression {
|
||||
CompressionType::Compressed => {
|
||||
let size = Sixbit::size(&mut *self.node_buf).context(NodeSixbitName)?;
|
||||
let size =
|
||||
Sixbit::size(&mut *self.node_buf).context(NodeSixbitNameSnafu)?;
|
||||
let data = self
|
||||
.node_buf
|
||||
.get(size.real_len as u32)
|
||||
.context(NodeBuffer { node_type })?;
|
||||
.context(NodeBufferSnafu { node_type })?;
|
||||
|
||||
Key::Compressed { size, data }
|
||||
},
|
||||
CompressionType::Uncompressed => {
|
||||
let encoding = self.encoding;
|
||||
let length =
|
||||
(self.node_buf.read_u8().context(NameLength)? & !ARRAY_MASK) + 1;
|
||||
(self.node_buf.read_u8().context(NameLengthSnafu)? & !ARRAY_MASK) + 1;
|
||||
let data = self
|
||||
.node_buf
|
||||
.get(length as u32)
|
||||
.context(NodeBuffer { node_type })?;
|
||||
.context(NodeBufferSnafu { node_type })?;
|
||||
|
||||
Key::Uncompressed { encoding, data }
|
||||
},
|
||||
|
|
@ -273,7 +283,7 @@ impl Reader {
|
|||
let value = self
|
||||
.data_buf
|
||||
.read_u32::<BigEndian>()
|
||||
.context(DataRead { size: 4usize })?;
|
||||
.context(DataReadSnafu { size: 4usize })?;
|
||||
debug!("Reader::read_u32() => result: {}", value);
|
||||
|
||||
Ok(value)
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ impl Sixbit {
|
|||
where
|
||||
T: Read,
|
||||
{
|
||||
let sixbit_len = reader.read_u8().context(LengthRead)?;
|
||||
let sixbit_len = reader.read_u8().context(LengthReadSnafu)?;
|
||||
let real_len = (f32::from(sixbit_len * 6) / 8f32).ceil();
|
||||
let real_len = (real_len as u32) as usize;
|
||||
debug!("sixbit_len: {}, real_len: {}", sixbit_len, real_len);
|
||||
|
|
@ -85,8 +85,8 @@ impl Sixbit {
|
|||
}
|
||||
}
|
||||
|
||||
writer.write_u8(len as u8).context(LengthWrite)?;
|
||||
writer.write_all(&bytes).context(DataWrite)?;
|
||||
writer.write_u8(len as u8).context(LengthWriteSnafu)?;
|
||||
writer.write_all(&bytes).context(DataWriteSnafu)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,16 +148,17 @@ impl<'a> TextXmlReader<'a> {
|
|||
if attr.key == b"__type" {
|
||||
let value = str::from_utf8(&*value)?;
|
||||
|
||||
node_type = Some(StandardType::from_name(value).context(InvalidKbinType)?);
|
||||
node_type =
|
||||
Some(StandardType::from_name(value).context(InvalidKbinTypeSnafu)?);
|
||||
} else if attr.key == b"__count" {
|
||||
let value = str::from_utf8(&*value)?;
|
||||
let num_count = value.parse::<u32>().context(ParseArrayCount)?;
|
||||
let num_count = value.parse::<u32>().context(ParseArrayCountSnafu)?;
|
||||
|
||||
count = num_count as usize;
|
||||
} else if attr.key == b"__size" {
|
||||
let value = str::from_utf8(&*value)?
|
||||
.parse::<usize>()
|
||||
.context(ParseBinarySize)?;
|
||||
.context(ParseBinarySizeSnafu)?;
|
||||
|
||||
size = Some(value);
|
||||
} else {
|
||||
|
|
@ -229,7 +230,7 @@ impl<'a> TextXmlReader<'a> {
|
|||
node_type => {
|
||||
let text = str::from_utf8(&*data)?;
|
||||
let value = Value::from_string(node_type, text, definition.is_array, count)
|
||||
.context(ValueDecode { node_type })?;
|
||||
.context(ValueDecodeSnafu { node_type })?;
|
||||
|
||||
// The read number of bytes must match the size attribute, if set
|
||||
if let Value::Binary(data) = &value {
|
||||
|
|
@ -243,7 +244,7 @@ impl<'a> TextXmlReader<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
Bytes::from(value.to_bytes().context(ValueEncode { node_type })?)
|
||||
Bytes::from(value.to_bytes().context(ValueEncodeSnafu { node_type })?)
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -309,7 +310,7 @@ impl<'a> TextXmlReader<'a> {
|
|||
Event::Decl(e) => {
|
||||
if let Some(encoding) = e.encoding() {
|
||||
self.encoding =
|
||||
EncodingType::from_label(&encoding?).context(InvalidEncoding)?;
|
||||
EncodingType::from_label(&encoding?).context(InvalidEncodingSnafu)?;
|
||||
}
|
||||
},
|
||||
Event::Eof => break,
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ impl IntoKbinBytes for i8 {
|
|||
|
||||
impl FromKbinBytes for i8 {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
input.read_i8().context(DataConvert)
|
||||
input.read_i8().context(DataConvertSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ impl IntoKbinBytes for u8 {
|
|||
|
||||
impl FromKbinBytes for u8 {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
input.read_u8().context(DataConvert)
|
||||
input.read_u8().context(DataConvertSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,7 +72,7 @@ impl IntoKbinBytes for Ipv4Addr {
|
|||
impl FromKbinBytes for Ipv4Addr {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut octets = [0; 4];
|
||||
input.read_exact(&mut octets).context(DataConvert)?;
|
||||
input.read_exact(&mut octets).context(DataConvertSnafu)?;
|
||||
|
||||
Ok(Ipv4Addr::from(octets))
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ macro_rules! multibyte_impl {
|
|||
|
||||
impl FromKbinBytes for $type {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
input.$read_method::<BigEndian>().context(DataConvert)
|
||||
input.$read_method::<BigEndian>().context(DataConvertSnafu)
|
||||
}
|
||||
}
|
||||
)*
|
||||
|
|
@ -119,7 +119,7 @@ macro_rules! tuple_impl {
|
|||
impl FromKbinBytes for [i8; $i8_count] {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut values = Self::default();
|
||||
input.read_i8_into(&mut values).context(DataConvert)?;
|
||||
input.read_i8_into(&mut values).context(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
|
@ -135,7 +135,7 @@ macro_rules! tuple_impl {
|
|||
impl FromKbinBytes for [u8; $u8_count] {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut values = Self::default();
|
||||
input.read_exact(&mut values).context(DataConvert)?;
|
||||
input.read_exact(&mut values).context(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
|
@ -175,7 +175,7 @@ macro_rules! tuple_impl {
|
|||
impl FromKbinBytes for [$type; $count] {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut values = Self::default();
|
||||
input.$read_method::<BigEndian>(&mut values).context(DataConvert)?;
|
||||
input.$read_method::<BigEndian>(&mut values).context(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ where
|
|||
output[i] = part
|
||||
.parse::<T>()
|
||||
.map_err(|e| Box::new(e) as Box<(dyn Error + Send + Sync + 'static)>)
|
||||
.context(StringParse { node_type })?;
|
||||
.context(StringParseSnafu { node_type })?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -71,7 +71,7 @@ impl FromKbinString for Ipv4Addr {
|
|||
|
||||
// IP addresses are split by a period, so do not use `parse_tuple`
|
||||
for (i, part) in input.split('.').enumerate() {
|
||||
octets[i] = part.parse::<u8>().context(StringParseInt {
|
||||
octets[i] = part.parse::<u8>().context(StringParseIntSnafu {
|
||||
node_type: "Ipv4Addr",
|
||||
})?;
|
||||
}
|
||||
|
|
@ -91,10 +91,10 @@ macro_rules! basic_int_parse {
|
|||
|
||||
if input.starts_with("0x") {
|
||||
<$type>::from_str_radix(&input[2..], 16)
|
||||
.context(StringParseInt { node_type: stringify!($type) })
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
} else {
|
||||
input.parse::<$type>()
|
||||
.context(StringParseInt { node_type: stringify!($type) })
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -112,7 +112,7 @@ macro_rules! basic_float_parse {
|
|||
space_check(input)?;
|
||||
|
||||
input.parse::<$type>()
|
||||
.context(StringParseFloat { node_type: stringify!($type) })
|
||||
.context(StringParseFloatSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
)*
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use std::net::Ipv4Addr;
|
|||
use rustc_hex::FromHex;
|
||||
use snafu::ResultExt;
|
||||
|
||||
use crate::error::*;
|
||||
use crate::error::{HexSnafu, KbinError, Result};
|
||||
use crate::node_types::StandardType;
|
||||
use crate::types::{FromKbinBytes, FromKbinString, IntoKbinBytes};
|
||||
|
||||
|
|
@ -174,10 +174,7 @@ macro_rules! tuple {
|
|||
StandardType::U32 => u32::from_kbin_string(input).map(Value::U32)?,
|
||||
StandardType::S64 => i64::from_kbin_string(input).map(Value::S64)?,
|
||||
StandardType::U64 => u64::from_kbin_string(input).map(Value::U64)?,
|
||||
StandardType::Binary => {
|
||||
let data: Vec<u8> = input.from_hex().context(HexError)?;
|
||||
Value::Binary(data)
|
||||
},
|
||||
StandardType::Binary => input.from_hex().map(Value::Binary).context(HexSnafu)?,
|
||||
StandardType::String => Value::String(input.to_owned()),
|
||||
StandardType::Attribute => Value::Attribute(input.to_owned()),
|
||||
StandardType::Ip4 => Ipv4Addr::from_kbin_string(input).map(Value::Ip4)?,
|
||||
|
|
|
|||
|
|
@ -130,16 +130,18 @@ fn write_value(
|
|||
let size = (data.len() * node_type.size) as u32;
|
||||
data_buf
|
||||
.write_u32::<BigEndian>(size)
|
||||
.context(NodeSize { node_type, size })?;
|
||||
data_buf.write_all(&data).context(DataWrite { node_type })?;
|
||||
.context(NodeSizeSnafu { node_type, size })?;
|
||||
data_buf
|
||||
.write_all(&data)
|
||||
.context(DataWriteSnafu { node_type })?;
|
||||
data_buf
|
||||
.realign_writes(None)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
},
|
||||
Value::String(text) => {
|
||||
data_buf
|
||||
.write_str(options.encoding, &text)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
},
|
||||
Value::Array(values) => {
|
||||
if !is_array {
|
||||
|
|
@ -151,28 +153,30 @@ fn write_value(
|
|||
let mut data = Vec::with_capacity(total_size);
|
||||
values
|
||||
.to_bytes_into(&mut data)
|
||||
.context(ValueEncode { node_type })?;
|
||||
.context(ValueEncodeSnafu { node_type })?;
|
||||
|
||||
data_buf
|
||||
.write_u32::<BigEndian>(total_size as u32)
|
||||
.context(NodeSize {
|
||||
.context(NodeSizeSnafu {
|
||||
node_type,
|
||||
size: total_size as u32,
|
||||
})?;
|
||||
data_buf.write_all(&data).context(DataWrite { node_type })?;
|
||||
data_buf
|
||||
.write_all(&data)
|
||||
.context(DataWriteSnafu { node_type })?;
|
||||
data_buf
|
||||
.realign_writes(None)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
},
|
||||
value => {
|
||||
if is_array {
|
||||
panic!("Attempted to write non-array value but was marked as array");
|
||||
}
|
||||
|
||||
let data = value.to_bytes().context(ValueEncode { node_type })?;
|
||||
let data = value.to_bytes().context(ValueEncodeSnafu { node_type })?;
|
||||
data_buf
|
||||
.write_aligned(node_type, &data)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -200,7 +204,7 @@ impl Writeable for NodeCollection {
|
|||
let name = self
|
||||
.base()
|
||||
.key()
|
||||
.context(DefinitionValue { node_type })?
|
||||
.context(DefinitionValueSnafu { node_type })?
|
||||
.ok_or(WriterError::NoNodeKey)?;
|
||||
|
||||
debug!("NodeCollection write_node => name: {}, type: {:?}, type_size: {}, type_count: {}, is_array: {}",
|
||||
|
|
@ -212,32 +216,33 @@ impl Writeable for NodeCollection {
|
|||
|
||||
node_buf
|
||||
.write_u8(node_type as u8 | array_mask)
|
||||
.context(DataWrite { node_type })?;
|
||||
.context(DataWriteSnafu { node_type })?;
|
||||
|
||||
match options.compression {
|
||||
CompressionType::Compressed => {
|
||||
Sixbit::pack(&mut **node_buf, &name).context(NodeSixbitName)?
|
||||
Sixbit::pack(&mut **node_buf, &name).context(NodeSixbitNameSnafu)?
|
||||
},
|
||||
CompressionType::Uncompressed => {
|
||||
let data =
|
||||
options
|
||||
.encoding
|
||||
.encode_bytes(&name)
|
||||
.context(NodeUncompressedNameEncode {
|
||||
encoding: options.encoding,
|
||||
})?;
|
||||
let data = options.encoding.encode_bytes(&name).context(
|
||||
NodeUncompressedNameEncodeSnafu {
|
||||
encoding: options.encoding,
|
||||
},
|
||||
)?;
|
||||
let len = (data.len() - 1) as u8;
|
||||
node_buf
|
||||
.write_u8(len | ARRAY_MASK)
|
||||
.context(NodeUncompressedNameLength)?;
|
||||
.context(NodeUncompressedNameLengthSnafu)?;
|
||||
node_buf
|
||||
.write_all(&data)
|
||||
.context(NodeUncompressedNameData)?;
|
||||
.context(NodeUncompressedNameDataSnafu)?;
|
||||
},
|
||||
};
|
||||
|
||||
if node_type != StandardType::NodeStart {
|
||||
let value = self.base().value().context(DefinitionValue { node_type })?;
|
||||
let value = self
|
||||
.base()
|
||||
.value()
|
||||
.context(DefinitionValueSnafu { node_type })?;
|
||||
write_value(options, data_buf, node_type, is_array, &value)?;
|
||||
}
|
||||
|
||||
|
|
@ -245,7 +250,7 @@ impl Writeable for NodeCollection {
|
|||
let node_type = StandardType::Attribute;
|
||||
let key = attr
|
||||
.key()
|
||||
.context(DefinitionKey { node_type })?
|
||||
.context(DefinitionKeySnafu { node_type })?
|
||||
.ok_or(WriterError::NoNodeKey)?;
|
||||
let value = attr.value_bytes().ok_or(WriterError::NoNodeValue)?;
|
||||
|
||||
|
|
@ -257,29 +262,29 @@ impl Writeable for NodeCollection {
|
|||
|
||||
data_buf
|
||||
.buf_write(value)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
|
||||
node_buf
|
||||
.write_u8(StandardType::Attribute as u8)
|
||||
.context(DataWrite { node_type })?;
|
||||
.context(DataWriteSnafu { node_type })?;
|
||||
|
||||
match options.compression {
|
||||
CompressionType::Compressed => {
|
||||
Sixbit::pack(&mut **node_buf, &key).context(NodeSixbitName)?
|
||||
Sixbit::pack(&mut **node_buf, &key).context(NodeSixbitNameSnafu)?
|
||||
},
|
||||
CompressionType::Uncompressed => {
|
||||
let data = options.encoding.encode_bytes(&key).context(
|
||||
NodeUncompressedNameEncode {
|
||||
NodeUncompressedNameEncodeSnafu {
|
||||
encoding: options.encoding,
|
||||
},
|
||||
)?;
|
||||
let len = (data.len() - 1) as u8;
|
||||
node_buf
|
||||
.write_u8(len | ARRAY_MASK)
|
||||
.context(NodeUncompressedNameLength)?;
|
||||
.context(NodeUncompressedNameLengthSnafu)?;
|
||||
node_buf
|
||||
.write_all(&data)
|
||||
.context(NodeUncompressedNameData)?;
|
||||
.context(NodeUncompressedNameDataSnafu)?;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -291,7 +296,7 @@ impl Writeable for NodeCollection {
|
|||
// node end always has the array bit set
|
||||
node_buf
|
||||
.write_u8(StandardType::NodeEnd as u8 | ARRAY_MASK)
|
||||
.context(NodeType {
|
||||
.context(NodeTypeSnafu {
|
||||
node_type: StandardType::NodeEnd,
|
||||
})?;
|
||||
|
||||
|
|
@ -324,26 +329,26 @@ impl Writeable for Node {
|
|||
|
||||
node_buf
|
||||
.write_u8(node_type as u8 | array_mask)
|
||||
.context(DataWrite {
|
||||
.context(DataWriteSnafu {
|
||||
node_type: node_type,
|
||||
})?;
|
||||
match options.compression {
|
||||
CompressionType::Compressed => {
|
||||
Sixbit::pack(&mut **node_buf, &self.key()).context(NodeSixbitName)?
|
||||
Sixbit::pack(&mut **node_buf, &self.key()).context(NodeSixbitNameSnafu)?
|
||||
},
|
||||
CompressionType::Uncompressed => {
|
||||
let data = options.encoding.encode_bytes(&self.key()).context(
|
||||
NodeUncompressedNameEncode {
|
||||
NodeUncompressedNameEncodeSnafu {
|
||||
encoding: options.encoding,
|
||||
},
|
||||
)?;
|
||||
let len = (data.len() - 1) as u8;
|
||||
node_buf
|
||||
.write_u8(len | ARRAY_MASK)
|
||||
.context(NodeUncompressedNameLength)?;
|
||||
.context(NodeUncompressedNameLengthSnafu)?;
|
||||
node_buf
|
||||
.write_all(&data)
|
||||
.context(NodeUncompressedNameData)?;
|
||||
.context(NodeUncompressedNameDataSnafu)?;
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -356,31 +361,31 @@ impl Writeable for Node {
|
|||
|
||||
data_buf
|
||||
.write_str(options.encoding, value)
|
||||
.context(DataBuffer { node_type })?;
|
||||
.context(DataBufferSnafu { node_type })?;
|
||||
|
||||
node_buf
|
||||
.write_u8(StandardType::Attribute as u8)
|
||||
.context(DataWrite {
|
||||
.context(DataWriteSnafu {
|
||||
node_type: StandardType::Attribute,
|
||||
})?;
|
||||
|
||||
match options.compression {
|
||||
CompressionType::Compressed => {
|
||||
Sixbit::pack(&mut **node_buf, &key).context(NodeSixbitName)?
|
||||
Sixbit::pack(&mut **node_buf, &key).context(NodeSixbitNameSnafu)?
|
||||
},
|
||||
CompressionType::Uncompressed => {
|
||||
let data = options.encoding.encode_bytes(&key).context(
|
||||
NodeUncompressedNameEncode {
|
||||
NodeUncompressedNameEncodeSnafu {
|
||||
encoding: options.encoding,
|
||||
},
|
||||
)?;
|
||||
let len = (data.len() - 1) as u8;
|
||||
node_buf
|
||||
.write_u8(len | ARRAY_MASK)
|
||||
.context(NodeUncompressedNameLength)?;
|
||||
.context(NodeUncompressedNameLengthSnafu)?;
|
||||
node_buf
|
||||
.write_all(&data)
|
||||
.context(NodeUncompressedNameData)?;
|
||||
.context(NodeUncompressedNameDataSnafu)?;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -392,7 +397,7 @@ impl Writeable for Node {
|
|||
// node end always has the array bit set
|
||||
node_buf
|
||||
.write_u8(StandardType::NodeEnd as u8 | ARRAY_MASK)
|
||||
.context(NodeType {
|
||||
.context(NodeTypeSnafu {
|
||||
node_type: StandardType::NodeEnd,
|
||||
})?;
|
||||
|
||||
|
|
@ -420,14 +425,16 @@ impl Writer {
|
|||
T: Writeable,
|
||||
{
|
||||
let mut header = Cursor::new(Vec::with_capacity(8));
|
||||
header.write_u8(SIGNATURE).context(Signature)?;
|
||||
header.write_u8(SIGNATURE).context(SignatureSnafu)?;
|
||||
|
||||
let compression = self.options.compression.to_byte();
|
||||
header.write_u8(compression).context(Compression)?;
|
||||
header.write_u8(compression).context(CompressionSnafu)?;
|
||||
|
||||
let encoding = self.options.encoding.to_byte();
|
||||
header.write_u8(encoding).context(Encoding)?;
|
||||
header.write_u8(0xFF ^ encoding).context(EncodingNegate)?;
|
||||
header.write_u8(encoding).context(EncodingSnafu)?;
|
||||
header
|
||||
.write_u8(0xFF ^ encoding)
|
||||
.context(EncodingNegateSnafu)?;
|
||||
|
||||
let mut node_buf = ByteBufferWrite::new(Vec::new());
|
||||
let mut data_buf = ByteBufferWrite::new(Vec::new());
|
||||
|
|
@ -436,10 +443,10 @@ impl Writer {
|
|||
|
||||
node_buf
|
||||
.write_u8(StandardType::FileEnd as u8 | ARRAY_MASK)
|
||||
.context(NodeType {
|
||||
.context(NodeTypeSnafu {
|
||||
node_type: StandardType::FileEnd,
|
||||
})?;
|
||||
node_buf.realign_writes(None).context(NodeBuffer {
|
||||
node_buf.realign_writes(None).context(NodeBufferSnafu {
|
||||
node_type: StandardType::FileEnd,
|
||||
})?;
|
||||
|
||||
|
|
@ -452,7 +459,7 @@ impl Writer {
|
|||
);
|
||||
output
|
||||
.write_u32::<BigEndian>(node_buf.len() as u32)
|
||||
.context(NodeBufferLength)?;
|
||||
.context(NodeBufferLengthSnafu)?;
|
||||
output.extend_from_slice(&node_buf);
|
||||
|
||||
let data_buf = data_buf.into_inner();
|
||||
|
|
@ -462,7 +469,7 @@ impl Writer {
|
|||
);
|
||||
output
|
||||
.write_u32::<BigEndian>(data_buf.len() as u32)
|
||||
.context(DataBufferLength)?;
|
||||
.context(DataBufferLengthSnafu)?;
|
||||
output.extend_from_slice(&data_buf);
|
||||
|
||||
Ok(output)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user