From 9b7d94e9fd944bed43de7fabbc6117ee510f332a Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Sat, 9 Nov 2019 00:50:11 +0000 Subject: [PATCH] compression: Compression -> CompressionType --- src/compression.rs | 31 ----------------------------- src/compression_type.rs | 44 +++++++++++++++++++++++++++++++++++++++++ src/error.rs | 2 -- src/lib.rs | 4 ++-- src/options.rs | 10 +++++----- src/reader.rs | 12 ++++++----- src/writer.rs | 28 +++++++++++++------------- 7 files changed, 72 insertions(+), 59 deletions(-) delete mode 100644 src/compression.rs create mode 100644 src/compression_type.rs diff --git a/src/compression.rs b/src/compression.rs deleted file mode 100644 index fb271ff..0000000 --- a/src/compression.rs +++ /dev/null @@ -1,31 +0,0 @@ -use super::{SIG_COMPRESSED, SIG_UNCOMPRESSED}; -use crate::error::KbinError; - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Compression { - Compressed, - Uncompressed, -} - -impl Compression { - pub fn from_byte(byte: u8) -> Result { - match byte { - SIG_COMPRESSED => Ok(Compression::Compressed), - SIG_UNCOMPRESSED => Ok(Compression::Uncompressed), - _ => Err(KbinError::UnknownCompression), - } - } - - pub fn to_byte(&self) -> u8 { - match *self { - Compression::Compressed => SIG_COMPRESSED, - Compression::Uncompressed => SIG_UNCOMPRESSED, - } - } -} - -impl Default for Compression { - fn default() -> Self { - Compression::Compressed - } -} diff --git a/src/compression_type.rs b/src/compression_type.rs new file mode 100644 index 0000000..4c4bacb --- /dev/null +++ b/src/compression_type.rs @@ -0,0 +1,44 @@ +use std::error::Error; +use std::fmt; + +use crate::{SIG_COMPRESSED, SIG_UNCOMPRESSED}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CompressionType { + Compressed, + Uncompressed, +} + +#[derive(Debug)] +pub struct UnknownCompression(u8); + +impl CompressionType { + pub fn from_byte(byte: u8) -> Result { + match byte { + SIG_COMPRESSED => Ok(Self::Compressed), + SIG_UNCOMPRESSED => Ok(Self::Uncompressed), + _ => Err(UnknownCompression(byte)), + } + } + + pub fn to_byte(&self) -> u8 { + match *self { + Self::Compressed => SIG_COMPRESSED, + Self::Uncompressed => SIG_UNCOMPRESSED, + } + } +} + +impl Default for CompressionType { + fn default() -> Self { + Self::Compressed + } +} + +impl fmt::Display for UnknownCompression { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Unknown compression type: 0x{:x}", self.0) + } +} + +impl Error for UnknownCompression {} diff --git a/src/error.rs b/src/error.rs index 3664aa3..5b83770 100644 --- a/src/error.rs +++ b/src/error.rs @@ -52,8 +52,6 @@ pub enum KbinError { #[snafu(display("Failed to interpret slice as UTF-8"))] Utf8Slice { source: Utf8Error }, - #[snafu(display("Unknown compression value"))] - UnknownCompression, #[snafu(display( "Size Mismatch, type: {}, expected size: {}, actual size: {}", diff --git a/src/lib.rs b/src/lib.rs index 8063a8f..8cb376a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ extern crate log; use bytes::Bytes; mod byte_buffer; -mod compression; +mod compression_type; mod encoding_type; mod error; mod node; @@ -27,7 +27,7 @@ use crate::text_reader::TextXmlReader; use crate::to_text_xml::TextXmlWriter; // Public exports -pub use crate::compression::Compression; +pub use crate::compression_type::CompressionType; pub use crate::encoding_type::EncodingType; pub use crate::error::{KbinError, Result}; pub use crate::node::{Node, NodeCollection}; diff --git a/src/options.rs b/src/options.rs index fd697ce..a2b570e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,20 +1,20 @@ -use crate::compression::Compression; +use crate::compression_type::CompressionType; use crate::encoding_type::EncodingType; #[derive(Clone, Debug, Default)] pub struct Options { - pub(crate) compression: Compression, + pub(crate) compression: CompressionType, pub(crate) encoding: EncodingType, } #[derive(Default)] pub struct OptionsBuilder { - compression: Compression, + compression: CompressionType, encoding: EncodingType, } impl Options { - pub fn new(compression: Compression, encoding: EncodingType) -> Self { + pub fn new(compression: CompressionType, encoding: EncodingType) -> Self { Self { compression, encoding, @@ -34,7 +34,7 @@ impl Options { } impl OptionsBuilder { - pub fn compression(&mut self, compression: Compression) -> &mut Self { + pub fn compression(&mut self, compression: CompressionType) -> &mut Self { self.compression = compression; self } diff --git a/src/reader.rs b/src/reader.rs index 5d433b0..4310fb8 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -5,14 +5,13 @@ use bytes::Bytes; use snafu::{ResultExt, Snafu}; use crate::byte_buffer::{ByteBufferError, ByteBufferRead}; -use crate::compression::Compression as CompressionType; +use crate::compression_type::{CompressionType, UnknownCompression}; use crate::encoding_type::EncodingType; use crate::error::KbinError; use crate::node::{Key, NodeData, NodeDefinition}; use crate::node_types::StandardType; use crate::sixbit::Sixbit; - -use super::{ARRAY_MASK, SIGNATURE}; +use crate::{ARRAY_MASK, SIGNATURE}; #[derive(Debug, Snafu)] pub enum ReaderError { @@ -22,6 +21,9 @@ pub enum ReaderError { #[snafu(display("Failed to read compression type from header"))] Compression { source: io::Error }, + #[snafu(display("Invalid compression type read from header"))] + InvalidCompression { source: UnknownCompression }, + #[snafu(display("Failed to read encoding type from header"))] Encoding { source: io::Error }, @@ -34,7 +36,7 @@ pub enum ReaderError { #[snafu(display("Failed to read data buffer length"))] DataBufferLength { source: io::Error }, - #[snafu(display("Reached the end of the node buffer"))] + #[snafu(display("Attempted to read past the end of the node buffer"))] EndOfNodeBuffer, #[snafu(display("Failed to read node type"))] @@ -72,7 +74,7 @@ impl Reader { } let compress_byte = node_buf.read_u8().context(Compression)?; - let compression = CompressionType::from_byte(compress_byte)?; + let compression = CompressionType::from_byte(compress_byte).context(InvalidCompression)?; let encoding_byte = node_buf.read_u8().context(Encoding)?; let encoding_negation = node_buf.read_u8().context(EncodingNegate)?; diff --git a/src/writer.rs b/src/writer.rs index c352509..ad8706c 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -4,7 +4,7 @@ use byteorder::{BigEndian, WriteBytesExt}; use snafu::ResultExt; use crate::byte_buffer::ByteBufferWrite; -use crate::compression::Compression; +use crate::compression_type::CompressionType; use crate::error::*; use crate::node::{Node, NodeCollection}; use crate::node_types::StandardType; @@ -91,11 +91,11 @@ impl Writeable for NodeCollection { let name = self.base().key()?.ok_or(KbinError::InvalidState)?; debug!("NodeCollection write_node => name: {}, type: {:?}, type_size: {}, type_count: {}, is_array: {}", - name, - node_type, - node_type.size, - node_type.count, - is_array); + name, + node_type, + node_type.size, + node_type.count, + is_array); node_buf .write_u8(node_type as u8 | array_mask) @@ -103,8 +103,8 @@ impl Writeable for NodeCollection { node_type: node_type.name, })?; match options.compression { - Compression::Compressed => Sixbit::pack(&mut **node_buf, &name)?, - Compression::Uncompressed => { + CompressionType::Compressed => Sixbit::pack(&mut **node_buf, &name)?, + CompressionType::Uncompressed => { let data = options.encoding.encode_bytes(&name)?; let len = (data.len() - 1) as u8; node_buf.write_u8(len | ARRAY_MASK).context(DataWrite { @@ -139,8 +139,8 @@ impl Writeable for NodeCollection { node_type: StandardType::Attribute.name, })?; match options.compression { - Compression::Compressed => Sixbit::pack(&mut **node_buf, &key)?, - Compression::Uncompressed => { + CompressionType::Compressed => Sixbit::pack(&mut **node_buf, &key)?, + CompressionType::Uncompressed => { let data = options.encoding.encode_bytes(&key)?; let len = (data.len() - 1) as u8; node_buf.write_u8(len | ARRAY_MASK).context(DataWrite { @@ -197,8 +197,8 @@ impl Writeable for Node { node_type: node_type.name, })?; match options.compression { - Compression::Compressed => Sixbit::pack(&mut **node_buf, &self.key())?, - Compression::Uncompressed => { + CompressionType::Compressed => Sixbit::pack(&mut **node_buf, &self.key())?, + CompressionType::Uncompressed => { let data = options.encoding.encode_bytes(&self.key())?; let len = (data.len() - 1) as u8; node_buf.write_u8(len | ARRAY_MASK).context(DataWrite { @@ -226,8 +226,8 @@ impl Writeable for Node { node_type: StandardType::Attribute.name, })?; match options.compression { - Compression::Compressed => Sixbit::pack(&mut **node_buf, &key)?, - Compression::Uncompressed => { + CompressionType::Compressed => Sixbit::pack(&mut **node_buf, &key)?, + CompressionType::Uncompressed => { let data = options.encoding.encode_bytes(&key)?; let len = (data.len() - 1) as u8; node_buf.write_u8(len | ARRAY_MASK).context(DataWrite {