mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-04-25 15:42:45 -05:00
kbinxml: reformat more to 4 space indent
This commit is contained in:
parent
948152e900
commit
380e687115
|
|
@ -79,135 +79,135 @@ impl FromKbinBytes for Ipv4Addr {
|
|||
}
|
||||
|
||||
macro_rules! multibyte_impl {
|
||||
(
|
||||
$(($type:ty, $write_method:ident, $read_method:ident)),*$(,)?
|
||||
) => {
|
||||
$(
|
||||
impl IntoKbinBytes for $type {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
buf.$write_method(self);
|
||||
}
|
||||
}
|
||||
(
|
||||
$(($type:ty, $write_method:ident, $read_method:ident)),*$(,)?
|
||||
) => {
|
||||
$(
|
||||
impl IntoKbinBytes for $type {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
buf.$write_method(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl FromKbinBytes for $type {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
input.$read_method::<BigEndian>().context(DataConvertSnafu)
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
impl FromKbinBytes for $type {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
input.$read_method::<BigEndian>().context(DataConvertSnafu)
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! tuple_impl {
|
||||
(
|
||||
i8: [$($i8_count:expr),*],
|
||||
u8: [$($u8_count:expr),*],
|
||||
bool: [$($bool_count:expr),*],
|
||||
multi: [
|
||||
$([$type:ty ; $($count:expr),*] => ($write_method:ident, $read_method:ident)),*$(,)?
|
||||
]
|
||||
) => {
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [i8; $i8_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
buf.put_i8(*value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [u8; $u8_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
buf.put_slice(&self[..]);
|
||||
}
|
||||
}
|
||||
|
||||
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(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [bool; $bool_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
value.write_kbin_bytes(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromKbinBytes for [bool; $bool_count] {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut values = Self::default();
|
||||
|
||||
for i in 0..$bool_count {
|
||||
values[i] = bool::from_kbin_bytes(input)?;
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [$type; $count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
buf.$write_method(*value);
|
||||
(
|
||||
i8: [$($i8_count:expr),*],
|
||||
u8: [$($u8_count:expr),*],
|
||||
bool: [$($bool_count:expr),*],
|
||||
multi: [
|
||||
$([$type:ty ; $($count:expr),*] => ($write_method:ident, $read_method:ident)),*$(,)?
|
||||
]
|
||||
) => {
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [i8; $i8_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
buf.put_i8(*value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(DataConvertSnafu)?;
|
||||
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(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
)*
|
||||
};
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [u8; $u8_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
buf.put_slice(&self[..]);
|
||||
}
|
||||
}
|
||||
|
||||
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(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [bool; $bool_count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
value.write_kbin_bytes(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromKbinBytes for [bool; $bool_count] {
|
||||
fn from_kbin_bytes<R: Read>(input: &mut R) -> Result<Self> {
|
||||
let mut values = Self::default();
|
||||
|
||||
for i in 0..$bool_count {
|
||||
values[i] = bool::from_kbin_bytes(input)?;
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
$(
|
||||
impl<'a> IntoKbinBytes for &'a [$type; $count] {
|
||||
fn write_kbin_bytes<B: BufMut>(self, buf: &mut B) {
|
||||
for value in self.into_iter() {
|
||||
buf.$write_method(*value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(DataConvertSnafu)?;
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
)*
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
multibyte_impl! {
|
||||
(i16, put_i16, read_i16),
|
||||
(u16, put_u16, read_u16),
|
||||
(i32, put_i32, read_i32),
|
||||
(u32, put_u32, read_u32),
|
||||
(i64, put_i64, read_i64),
|
||||
(u64, put_u64, read_u64),
|
||||
(f32, put_f32, read_f32),
|
||||
(f64, put_f64, read_f64),
|
||||
(i16, put_i16, read_i16),
|
||||
(u16, put_u16, read_u16),
|
||||
(i32, put_i32, read_i32),
|
||||
(u32, put_u32, read_u32),
|
||||
(i64, put_i64, read_i64),
|
||||
(u64, put_u64, read_u64),
|
||||
(f32, put_f32, read_f32),
|
||||
(f64, put_f64, read_f64),
|
||||
}
|
||||
|
||||
tuple_impl! {
|
||||
i8: [2, 3, 4, 16],
|
||||
u8: [2, 3, 4, 16],
|
||||
bool: [2, 3, 4, 16],
|
||||
multi: [
|
||||
[i16; 2, 3, 4, 8] => (put_i16, read_i16_into),
|
||||
[u16; 2, 3, 4, 8] => (put_u16, read_u16_into),
|
||||
[i32; 2, 3, 4] => (put_i32, read_i32_into),
|
||||
[u32; 2, 3, 4] => (put_u32, read_u32_into),
|
||||
[i64; 2, 3, 4] => (put_i64, read_i64_into),
|
||||
[u64; 2, 3, 4] => (put_u64, read_u64_into),
|
||||
[f32; 2, 3, 4] => (put_f32, read_f32_into),
|
||||
[f64; 2, 3, 4] => (put_f64, read_f64_into),
|
||||
]
|
||||
i8: [2, 3, 4, 16],
|
||||
u8: [2, 3, 4, 16],
|
||||
bool: [2, 3, 4, 16],
|
||||
multi: [
|
||||
[i16; 2, 3, 4, 8] => (put_i16, read_i16_into),
|
||||
[u16; 2, 3, 4, 8] => (put_u16, read_u16_into),
|
||||
[i32; 2, 3, 4] => (put_i32, read_i32_into),
|
||||
[u32; 2, 3, 4] => (put_u32, read_u32_into),
|
||||
[i64; 2, 3, 4] => (put_i64, read_i64_into),
|
||||
[u64; 2, 3, 4] => (put_u64, read_u64_into),
|
||||
[f32; 2, 3, 4] => (put_f32, read_f32_into),
|
||||
[f64; 2, 3, 4] => (put_f64, read_f64_into),
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,109 +81,113 @@ impl FromKbinString for Ipv4Addr {
|
|||
}
|
||||
|
||||
macro_rules! basic_int_parse {
|
||||
(
|
||||
$($type:ty),*$(,)?
|
||||
) => {
|
||||
$(
|
||||
impl FromKbinString for $type {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
space_check(input)?;
|
||||
($($type:ty),*$(,)?) => {
|
||||
$(
|
||||
impl FromKbinString for $type {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
space_check(input)?;
|
||||
|
||||
if input.starts_with("0x") {
|
||||
<$type>::from_str_radix(&input[2..], 16)
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
} else {
|
||||
input.parse::<$type>()
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
if input.starts_with("0x") {
|
||||
<$type>::from_str_radix(&input[2..], 16)
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
} else {
|
||||
input
|
||||
.parse::<$type>()
|
||||
.context(StringParseIntSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! basic_float_parse {
|
||||
(
|
||||
$($type:ty),*$(,)?
|
||||
) => {
|
||||
$(
|
||||
impl FromKbinString for $type {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
space_check(input)?;
|
||||
($($type:ty),*$(,)?) => {
|
||||
$(
|
||||
impl FromKbinString for $type {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
space_check(input)?;
|
||||
|
||||
input.parse::<$type>()
|
||||
.context(StringParseFloatSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
input
|
||||
.parse::<$type>()
|
||||
.context(StringParseFloatSnafu { node_type: stringify!($type) })
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! tuple_parse {
|
||||
(
|
||||
bool: [$($bool_count:expr),*],
|
||||
multi: [
|
||||
$([$type:ident ; $($count:expr),*]),*$(,)?
|
||||
]
|
||||
) => {
|
||||
$(
|
||||
impl FromKbinString for [bool; $bool_count] {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
const TYPE_NAME: &'static str = concat!("[bool; ", stringify!($bool_count), "]");
|
||||
(
|
||||
bool: [$($bool_count:expr),*],
|
||||
multi: [
|
||||
$([$type:ident ; $($count:expr),*]),*$(,)?
|
||||
]
|
||||
) => {
|
||||
$(
|
||||
impl FromKbinString for [bool; $bool_count] {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
const TYPE_NAME: &'static str = concat!("[bool; ", stringify!($bool_count), "]");
|
||||
|
||||
let count = input.split(' ').count();
|
||||
if count != $bool_count {
|
||||
return Err(KbinError::SizeMismatch { node_type: TYPE_NAME, expected: $bool_count, actual: count });
|
||||
}
|
||||
let count = input.split(' ').count();
|
||||
|
||||
let mut value = Self::default();
|
||||
if count != $bool_count {
|
||||
return Err(KbinError::SizeMismatch {
|
||||
node_type: TYPE_NAME,
|
||||
expected: $bool_count,
|
||||
actual: count,
|
||||
});
|
||||
}
|
||||
|
||||
for (i, part) in input.split(' ').enumerate() {
|
||||
value[i] = bool::from_kbin_string(part)?;
|
||||
}
|
||||
let mut value = Self::default();
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
$(
|
||||
impl FromKbinString for [$type; $count] {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
let mut value = Self::default();
|
||||
parse_tuple(concat!("[", stringify!($type), "; ", stringify!($count), "]"), input, &mut value)?;
|
||||
for (i, part) in input.split(' ').enumerate() {
|
||||
value[i] = bool::from_kbin_string(part)?;
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
)*
|
||||
)*
|
||||
};
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
)*
|
||||
$(
|
||||
$(
|
||||
impl FromKbinString for [$type; $count] {
|
||||
fn from_kbin_string(input: &str) -> Result<Self> {
|
||||
let mut value = Self::default();
|
||||
|
||||
parse_tuple(concat!("[", stringify!($type), "; ", stringify!($count), "]"), input, &mut value)?;
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
)*
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
basic_int_parse! {
|
||||
i8, u8,
|
||||
i16, u16,
|
||||
i32, u32,
|
||||
i64, u64,
|
||||
i8, u8,
|
||||
i16, u16,
|
||||
i32, u32,
|
||||
i64, u64,
|
||||
}
|
||||
|
||||
basic_float_parse! {
|
||||
f32, f64,
|
||||
f32, f64,
|
||||
}
|
||||
|
||||
tuple_parse! {
|
||||
bool: [2, 3, 4, 16],
|
||||
multi: [
|
||||
[i8; 2, 3, 4, 16],
|
||||
[u8; 2, 3, 4, 16],
|
||||
[i16; 2, 3, 4, 8],
|
||||
[u16; 2, 3, 4, 8],
|
||||
[i32; 2, 3, 4],
|
||||
[u32; 2, 3, 4],
|
||||
[i64; 2, 3, 4],
|
||||
[u64; 2, 3, 4],
|
||||
[f32; 2, 3, 4],
|
||||
[f64; 2, 3, 4],
|
||||
]
|
||||
bool: [2, 3, 4, 16],
|
||||
multi: [
|
||||
[i8; 2, 3, 4, 16],
|
||||
[u8; 2, 3, 4, 16],
|
||||
[i16; 2, 3, 4, 8],
|
||||
[u16; 2, 3, 4, 8],
|
||||
[i32; 2, 3, 4],
|
||||
[u32; 2, 3, 4],
|
||||
[i64; 2, 3, 4],
|
||||
[u64; 2, 3, 4],
|
||||
[f32; 2, 3, 4],
|
||||
[f64; 2, 3, 4],
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,213 +16,237 @@ mod array;
|
|||
pub use self::array::ValueArray;
|
||||
|
||||
macro_rules! construct_types {
|
||||
(
|
||||
$(
|
||||
($konst:ident, $($value_type:tt)*);
|
||||
)+
|
||||
) => {
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
$(
|
||||
$konst($($value_type)*),
|
||||
)+
|
||||
Binary(Vec<u8>),
|
||||
Time(u32),
|
||||
Attribute(String),
|
||||
(
|
||||
$(
|
||||
($konst:ident, $($value_type:tt)*);
|
||||
)+
|
||||
) => {
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
$(
|
||||
$konst($($value_type)*),
|
||||
)+
|
||||
Binary(Vec<u8>),
|
||||
Time(u32),
|
||||
Attribute(String),
|
||||
|
||||
Array(ValueArray),
|
||||
Array(ValueArray),
|
||||
}
|
||||
|
||||
$(
|
||||
impl From<$($value_type)*> for Value {
|
||||
fn from(value: $($value_type)*) -> Value {
|
||||
Value::$konst(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Value> for $($value_type)* {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: Value) -> Result<Self> {
|
||||
match value {
|
||||
Value::$konst(v) => Ok(v),
|
||||
value => {
|
||||
Err(KbinError::ValueTypeMismatch {
|
||||
node_type: StandardType::$konst,
|
||||
value,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Value> for $($value_type)* {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: &Value) -> Result<Self> {
|
||||
match value {
|
||||
Value::$konst(ref v) => Ok(v.clone()),
|
||||
value => {
|
||||
Err(KbinError::ValueTypeMismatch {
|
||||
node_type: StandardType::$konst,
|
||||
value: value.clone(),
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
||||
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(ref value) => value.standard_type(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
impl From<$($value_type)*> for Value {
|
||||
fn from(value: $($value_type)*) -> Value {
|
||||
Value::$konst(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Value> for $($value_type)* {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: Value) -> Result<Self> {
|
||||
match value {
|
||||
Value::$konst(v) => Ok(v),
|
||||
value => {
|
||||
Err(KbinError::ValueTypeMismatch {
|
||||
node_type: StandardType::$konst,
|
||||
value,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Value> for $($value_type)* {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: &Value) -> Result<Self> {
|
||||
match value {
|
||||
Value::$konst(ref v) => Ok(v.clone()),
|
||||
value => {
|
||||
Err(KbinError::ValueTypeMismatch {
|
||||
node_type: StandardType::$konst,
|
||||
value: value.clone(),
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
||||
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(ref value) => value.standard_type(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! tuple {
|
||||
(
|
||||
$($konst:ident),*$(,)?
|
||||
) => {
|
||||
pub fn from_standard_type(node_type: StandardType, is_array: bool, input: &[u8]) -> Result<Option<Value>> {
|
||||
let node_size = node_type.size * node_type.count;
|
||||
(
|
||||
$($konst:ident),*$(,)?
|
||||
) => {
|
||||
pub fn from_standard_type(
|
||||
node_type: StandardType,
|
||||
is_array: bool,
|
||||
input: &[u8],
|
||||
) -> Result<Option<Value>> {
|
||||
let node_size = node_type.size * node_type.count;
|
||||
|
||||
if is_array {
|
||||
let value = match ValueArray::from_standard_type(node_type, input)? {
|
||||
Some(value) => value,
|
||||
None => return Err(KbinError::InvalidState),
|
||||
};
|
||||
debug!("Value::from_standard_type({:?}) input: 0x{:02x?} => {:?}", node_type, input, value);
|
||||
if is_array {
|
||||
let value = match ValueArray::from_standard_type(node_type, input)? {
|
||||
Some(value) => value,
|
||||
None => return Err(KbinError::InvalidState),
|
||||
};
|
||||
debug!(
|
||||
"Value::from_standard_type({:?}) input: 0x{:02x?} => {:?}",
|
||||
node_type, input, value
|
||||
);
|
||||
|
||||
return Ok(Some(Value::Array(value)));
|
||||
}
|
||||
return Ok(Some(Value::Array(value)));
|
||||
}
|
||||
|
||||
match node_type {
|
||||
StandardType::String |
|
||||
StandardType::Binary => {},
|
||||
_ => {
|
||||
if input.len() != node_size {
|
||||
return Err(KbinError::SizeMismatch { node_type: node_type.name, expected: node_size, actual: input.len() });
|
||||
}
|
||||
},
|
||||
};
|
||||
match node_type {
|
||||
StandardType::String | StandardType::Binary => {},
|
||||
_ => {
|
||||
if input.len() != node_size {
|
||||
return Err(KbinError::SizeMismatch {
|
||||
node_type: node_type.name,
|
||||
expected: node_size,
|
||||
actual: input.len(),
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let mut reader = Cursor::new(input);
|
||||
let mut reader = Cursor::new(input);
|
||||
|
||||
let value = match node_type {
|
||||
StandardType::NodeStart |
|
||||
StandardType::NodeEnd |
|
||||
StandardType::FileEnd |
|
||||
StandardType::Attribute |
|
||||
StandardType::String => return Ok(None),
|
||||
StandardType::S8 => i8::from_kbin_bytes(&mut reader).map(Value::S8)?,
|
||||
StandardType::U8 => u8::from_kbin_bytes(&mut reader).map(Value::U8)?,
|
||||
StandardType::S16 => i16::from_kbin_bytes(&mut reader).map(Value::S16)?,
|
||||
StandardType::U16 => u16::from_kbin_bytes(&mut reader).map(Value::U16)?,
|
||||
StandardType::S32 => i32::from_kbin_bytes(&mut reader).map(Value::S32)?,
|
||||
StandardType::U32 => u32::from_kbin_bytes(&mut reader).map(Value::U32)?,
|
||||
StandardType::S64 => i64::from_kbin_bytes(&mut reader).map(Value::S64)?,
|
||||
StandardType::U64 => u64::from_kbin_bytes(&mut reader).map(Value::U64)?,
|
||||
StandardType::Binary => Value::Binary(input.to_vec()),
|
||||
StandardType::Time => u32::from_kbin_bytes(&mut reader).map(Value::Time)?,
|
||||
StandardType::Ip4 => Ipv4Addr::from_kbin_bytes(&mut reader).map(Value::Ip4)?,
|
||||
StandardType::Float => f32::from_kbin_bytes(&mut reader).map(Value::Float)?,
|
||||
StandardType::Double => f64::from_kbin_bytes(&mut reader).map(Value::Double)?,
|
||||
StandardType::Boolean => bool::from_kbin_bytes(&mut reader).map(Value::Boolean)?,
|
||||
$(
|
||||
StandardType::$konst => {
|
||||
FromKbinBytes::from_kbin_bytes(&mut reader).map(Value::$konst)?
|
||||
},
|
||||
)*
|
||||
};
|
||||
debug!("Value::from_standard_type({:?}) input: 0x{:02x?} => {:?}", node_type, input, value);
|
||||
let value = match node_type {
|
||||
StandardType::NodeStart |
|
||||
StandardType::NodeEnd |
|
||||
StandardType::FileEnd |
|
||||
StandardType::Attribute |
|
||||
StandardType::String => return Ok(None),
|
||||
StandardType::S8 => i8::from_kbin_bytes(&mut reader).map(Value::S8)?,
|
||||
StandardType::U8 => u8::from_kbin_bytes(&mut reader).map(Value::U8)?,
|
||||
StandardType::S16 => i16::from_kbin_bytes(&mut reader).map(Value::S16)?,
|
||||
StandardType::U16 => u16::from_kbin_bytes(&mut reader).map(Value::U16)?,
|
||||
StandardType::S32 => i32::from_kbin_bytes(&mut reader).map(Value::S32)?,
|
||||
StandardType::U32 => u32::from_kbin_bytes(&mut reader).map(Value::U32)?,
|
||||
StandardType::S64 => i64::from_kbin_bytes(&mut reader).map(Value::S64)?,
|
||||
StandardType::U64 => u64::from_kbin_bytes(&mut reader).map(Value::U64)?,
|
||||
StandardType::Binary => Value::Binary(input.to_vec()),
|
||||
StandardType::Time => u32::from_kbin_bytes(&mut reader).map(Value::Time)?,
|
||||
StandardType::Ip4 => Ipv4Addr::from_kbin_bytes(&mut reader).map(Value::Ip4)?,
|
||||
StandardType::Float => f32::from_kbin_bytes(&mut reader).map(Value::Float)?,
|
||||
StandardType::Double => f64::from_kbin_bytes(&mut reader).map(Value::Double)?,
|
||||
StandardType::Boolean => bool::from_kbin_bytes(&mut reader).map(Value::Boolean)?,
|
||||
$(
|
||||
StandardType::$konst => {
|
||||
FromKbinBytes::from_kbin_bytes(&mut reader).map(Value::$konst)?
|
||||
},
|
||||
)*
|
||||
};
|
||||
debug!("Value::from_standard_type({:?}) input: 0x{:02x?} => {:?}", node_type, input, value);
|
||||
|
||||
Ok(Some(value))
|
||||
}
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
pub fn from_string(node_type: StandardType, input: &str, is_array: bool, arr_count: usize) -> Result<Value> {
|
||||
trace!("Value::from_string({:?}, is_array: {}, arr_count: {}) => input: {:?}", node_type, is_array, arr_count, input);
|
||||
pub fn from_string(
|
||||
node_type: StandardType,
|
||||
input: &str,
|
||||
is_array: bool,
|
||||
arr_count: usize,
|
||||
) -> Result<Value> {
|
||||
trace!(
|
||||
"Value::from_string({:?}, is_array: {}, arr_count: {}) => input: {:?}",
|
||||
node_type,
|
||||
is_array,
|
||||
arr_count,
|
||||
input
|
||||
);
|
||||
|
||||
if is_array {
|
||||
let value = match node_type.count {
|
||||
0 => return Err(KbinError::InvalidState.into()),
|
||||
count => Value::Array(ValueArray::from_string(node_type, count, input, arr_count)?),
|
||||
};
|
||||
debug!("Value::from_string({:?}) input: {:?} => {:?}", node_type, input, value);
|
||||
if is_array {
|
||||
let value = match node_type.count {
|
||||
0 => return Err(KbinError::InvalidState.into()),
|
||||
count => Value::Array(ValueArray::from_string(node_type, count, input, arr_count)?),
|
||||
};
|
||||
debug!("Value::from_string({:?}) input: {:?} => {:?}", node_type, input, value);
|
||||
|
||||
return Ok(value);
|
||||
}
|
||||
return Ok(value);
|
||||
}
|
||||
|
||||
let value = match node_type {
|
||||
StandardType::NodeStart |
|
||||
StandardType::NodeEnd |
|
||||
StandardType::FileEnd => return Err(KbinError::InvalidNodeType { node_type }),
|
||||
StandardType::S8 => i8::from_kbin_string(input).map(Value::S8)?,
|
||||
StandardType::U8 => u8::from_kbin_string(input).map(Value::U8)?,
|
||||
StandardType::S16 => i16::from_kbin_string(input).map(Value::S16)?,
|
||||
StandardType::U16 => u16::from_kbin_string(input).map(Value::U16)?,
|
||||
StandardType::S32 => i32::from_kbin_string(input).map(Value::S32)?,
|
||||
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 => 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)?,
|
||||
StandardType::Time => u32::from_kbin_string(input).map(Value::Time)?,
|
||||
StandardType::Float => f32::from_kbin_string(input).map(Value::Float)?,
|
||||
StandardType::Double => f64::from_kbin_string(input).map(Value::Double)?,
|
||||
StandardType::Boolean => bool::from_kbin_string(input).map(Value::Boolean)?,
|
||||
$(
|
||||
StandardType::$konst => FromKbinString::from_kbin_string(input).map(Value::$konst)?,
|
||||
)*
|
||||
};
|
||||
debug!("Value::from_string({:?}) input: {:?} => {:?}", node_type, input, value);
|
||||
let value = match node_type {
|
||||
StandardType::NodeStart |
|
||||
StandardType::NodeEnd |
|
||||
StandardType::FileEnd => return Err(KbinError::InvalidNodeType { node_type }),
|
||||
StandardType::S8 => i8::from_kbin_string(input).map(Value::S8)?,
|
||||
StandardType::U8 => u8::from_kbin_string(input).map(Value::U8)?,
|
||||
StandardType::S16 => i16::from_kbin_string(input).map(Value::S16)?,
|
||||
StandardType::U16 => u16::from_kbin_string(input).map(Value::U16)?,
|
||||
StandardType::S32 => i32::from_kbin_string(input).map(Value::S32)?,
|
||||
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 => 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)?,
|
||||
StandardType::Time => u32::from_kbin_string(input).map(Value::Time)?,
|
||||
StandardType::Float => f32::from_kbin_string(input).map(Value::Float)?,
|
||||
StandardType::Double => f64::from_kbin_string(input).map(Value::Double)?,
|
||||
StandardType::Boolean => bool::from_kbin_string(input).map(Value::Boolean)?,
|
||||
$(
|
||||
StandardType::$konst => FromKbinString::from_kbin_string(input)
|
||||
.map(Value::$konst)?,
|
||||
)*
|
||||
};
|
||||
debug!("Value::from_string({:?}) input: {:?} => {:?}", node_type, input, value);
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn to_bytes_inner(&self, output: &mut Vec<u8>) -> Result<()> {
|
||||
debug!("Value::to_bytes_inner(self: {:?})", self);
|
||||
fn to_bytes_inner(&self, output: &mut Vec<u8>) -> Result<()> {
|
||||
debug!("Value::to_bytes_inner(self: {:?})", self);
|
||||
|
||||
match self {
|
||||
Value::S8(n) => n.write_kbin_bytes(output),
|
||||
Value::U8(n) => n.write_kbin_bytes(output),
|
||||
Value::S16(n) => n.write_kbin_bytes(output),
|
||||
Value::U16(n) => n.write_kbin_bytes(output),
|
||||
Value::S32(n) => n.write_kbin_bytes(output),
|
||||
Value::U32(n) => n.write_kbin_bytes(output),
|
||||
Value::S64(n) => n.write_kbin_bytes(output),
|
||||
Value::U64(n) => n.write_kbin_bytes(output),
|
||||
Value::Binary(data) => output.extend_from_slice(data),
|
||||
Value::Time(n) => n.write_kbin_bytes(output),
|
||||
Value::Ip4(addr) => addr.write_kbin_bytes(output),
|
||||
Value::Float(n) => n.write_kbin_bytes(output),
|
||||
Value::Double(n) => n.write_kbin_bytes(output),
|
||||
Value::Boolean(v) => v.write_kbin_bytes(output),
|
||||
Value::Array(value) => value.to_bytes_into(output)?,
|
||||
Value::Attribute(_) |
|
||||
Value::String(_) => return Err(KbinError::InvalidNodeType { node_type: self.standard_type() }),
|
||||
$(
|
||||
Value::$konst(value) => {
|
||||
output.reserve(value.len() * StandardType::$konst.size);
|
||||
value.write_kbin_bytes(output);
|
||||
},
|
||||
)*
|
||||
};
|
||||
match self {
|
||||
Value::S8(n) => n.write_kbin_bytes(output),
|
||||
Value::U8(n) => n.write_kbin_bytes(output),
|
||||
Value::S16(n) => n.write_kbin_bytes(output),
|
||||
Value::U16(n) => n.write_kbin_bytes(output),
|
||||
Value::S32(n) => n.write_kbin_bytes(output),
|
||||
Value::U32(n) => n.write_kbin_bytes(output),
|
||||
Value::S64(n) => n.write_kbin_bytes(output),
|
||||
Value::U64(n) => n.write_kbin_bytes(output),
|
||||
Value::Binary(data) => output.extend_from_slice(data),
|
||||
Value::Time(n) => n.write_kbin_bytes(output),
|
||||
Value::Ip4(addr) => addr.write_kbin_bytes(output),
|
||||
Value::Float(n) => n.write_kbin_bytes(output),
|
||||
Value::Double(n) => n.write_kbin_bytes(output),
|
||||
Value::Boolean(v) => v.write_kbin_bytes(output),
|
||||
Value::Array(value) => value.to_bytes_into(output)?,
|
||||
Value::Attribute(_) |
|
||||
Value::String(_) => {
|
||||
return Err(KbinError::InvalidNodeType { node_type: self.standard_type() });
|
||||
},
|
||||
$(
|
||||
Value::$konst(value) => {
|
||||
output.reserve(value.len() * StandardType::$konst.size);
|
||||
value.write_kbin_bytes(output);
|
||||
},
|
||||
)*
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl Value {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user