diff --git a/src/compression.rs b/src/compression.rs index cb47b22..3f0f6fd 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -18,7 +18,7 @@ impl Compression { pub fn to_byte(&self) -> u8 { match *self { - Compression::Compressed => SIG_COMPRESSED, + Compression::Compressed => SIG_COMPRESSED, Compression::Uncompressed => SIG_UNCOMPRESSED, } } diff --git a/src/value.rs b/src/value.rs index aac2b95..33d4666 100644 --- a/src/value.rs +++ b/src/value.rs @@ -8,9 +8,72 @@ use failure::{Fail, ResultExt}; use rustc_hex::FromHex; use crate::error::{KbinError, KbinErrorKind}; -use crate::node::Node; use crate::node_types::{self, StandardType}; +macro_rules! construct_types { + ( + $( + ($konst:ident, $($value_type:tt)*); + )+ + ) => { + #[derive(Clone, PartialEq)] + pub enum Value { + $( + $konst($($value_type)*), + )+ + Binary(Vec), + Time(u32), + Attribute(String), + + Array(StandardType, Vec), + } + + $( + impl From<$($value_type)*> for Value { + fn from(value: $($value_type)*) -> Value { + Value::$konst(value) + } + } + + impl TryFrom for $($value_type)* { + type Error = KbinError; + + fn try_from(value: Value) -> Result { + match value { + Value::$konst(v) => Ok(v), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value).into()), + } + } + } + + impl TryFrom<&Value> for $($value_type)* { + type Error = KbinError; + + fn try_from(value: &Value) -> Result { + match value { + Value::$konst(ref v) => Ok(v.clone()), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value.clone()).into()), + } + } + } + )+ + + impl Value { + pub fn standard_type(&self) -> StandardType { + match *self { + $( + Value::$konst(_) => StandardType::$konst, + )+ + Value::Binary(_) => StandardType::Binary, + Value::Time(_) => StandardType::Time, + Value::Attribute(_) => StandardType::Attribute, + Value::Array(node_type, _) => node_type, + } + } + } + } +} + macro_rules! tuple { ( byte: [ @@ -333,8 +396,7 @@ macro_rules! tuple { } }, Value::Attribute(_) | - Value::String(_) | - Value::Node(_) => return Err(KbinErrorKind::InvalidNodeType(self.standard_type()).into()), + Value::String(_) => return Err(KbinErrorKind::InvalidNodeType(self.standard_type()).into()), $( Value::$s8_konst(value) => { output.reserve(value.len()); @@ -376,72 +438,6 @@ macro_rules! tuple { }; } -macro_rules! construct_types { - ( - $( - ($konst:ident, $($value_type:tt)*); - )+ - ) => { - #[derive(Clone, PartialEq)] - pub enum Value { - $( - $konst($($value_type)*), - )+ - Binary(Vec), - Time(u32), - Attribute(String), - - Array(StandardType, Vec), - Node(Box), - } - - $( - impl From<$($value_type)*> for Value { - fn from(value: $($value_type)*) -> Value { - Value::$konst(value) - } - } - - impl TryFrom for $($value_type)* { - type Error = KbinError; - - fn try_from(value: Value) -> Result { - match value { - Value::$konst(v) => Ok(v), - value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value).into()), - } - } - } - - impl TryFrom<&Value> for $($value_type)* { - type Error = KbinError; - - fn try_from(value: &Value) -> Result { - match value { - Value::$konst(ref v) => Ok(v.clone()), - value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value.clone()).into()), - } - } - } - )+ - - impl Value { - pub fn standard_type(&self) -> StandardType { - match *self { - $( - Value::$konst(_) => StandardType::$konst, - )+ - Value::Binary(_) => StandardType::Binary, - Value::Time(_) => StandardType::Time, - Value::Attribute(_) => StandardType::Attribute, - Value::Array(node_type, _) => node_type, - Value::Node(_) => StandardType::NodeStart, - } - } - } - } -} - impl Value { tuple! { byte: [ @@ -642,20 +638,12 @@ impl fmt::Debug for Value { macro_rules! field { ( display: [$($konst_display:ident),*], - debug_alternate: [$($konst_alternate:ident),*], debug: [$($konst_debug:ident),*] ) => { match *self { $( Value::$konst_display(ref v) => write!(f, concat!(stringify!($konst_display), "({})"), v), )* - $( - Value::$konst_alternate(ref v) => if f.alternate() { - write!(f, concat!(stringify!($konst_alternate), "({:#?})"), v) - } else { - write!(f, concat!(stringify!($konst_alternate), "({:?})"), v) - }, - )* $( Value::$konst_debug(ref v) => write!(f, concat!(stringify!($konst_debug), "({:?})"), v), )* @@ -675,9 +663,6 @@ impl fmt::Debug for Value { U8, U16, U32, U64, Float, Double, Boolean ], - debug_alternate: [ - Node - ], debug: [ String, Time, Ip4, Attribute, @@ -756,7 +741,6 @@ impl fmt::Display for Value { false => f.write_str("0"), }, Value::Array(_, values) => BorrowedValueArray(&values).fmt(f), - Value::Node(_) => Ok(()), } }; }