compression: Compression -> CompressionType

This commit is contained in:
Matt Bilker
2019-11-09 00:50:11 +00:00
parent f273d5b13e
commit 9b7d94e9fd
7 changed files with 72 additions and 59 deletions

View File

@@ -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<Self, KbinError> {
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
}
}

44
src/compression_type.rs Normal file
View File

@@ -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<Self, UnknownCompression> {
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 {}

View File

@@ -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: {}",

View File

@@ -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};

View File

@@ -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
}

View File

@@ -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)?;

View File

@@ -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 {