aboutsummaryrefslogtreecommitdiff
path: root/src/structs/settings.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/structs/settings.rs')
-rw-r--r--src/structs/settings.rs82
1 files changed, 40 insertions, 42 deletions
diff --git a/src/structs/settings.rs b/src/structs/settings.rs
index 63c4d3a..fce6d9e 100644
--- a/src/structs/settings.rs
+++ b/src/structs/settings.rs
@@ -1,5 +1,6 @@
1use crate::structs::{ArchiveSerializer, StructError, StructResult}; 1use crate::structs::{ArchiveDeserializer, ArchiveSerializer, StructResult};
2use serde::Serialize; 2use serde::{Deserialize, Serialize};
3use std::mem::size_of;
3 4
4pub enum ByteOrder { 5pub enum ByteOrder {
5 Le, 6 Le,
@@ -12,61 +13,53 @@ pub enum VariantIndexType {
12 U16, 13 U16,
13 U32, 14 U32,
14 U64, 15 U64,
15 I8,
16 I16,
17 I32,
18 I64,
19} 16}
20 17
21macro_rules! impl_byte_order { 18macro_rules! impl_byte_order {
22 ($($n:ident),+) => { 19 ($($to:ident $fr:ident $n:ident),+) => {
23 impl ByteOrder {$( 20 impl ByteOrder {$(
24 pub fn $n(&self, num: $n) -> Vec<u8> { 21 pub fn $fr(&self, num: $n) -> Vec<u8> {
25 match self { 22 match self {
26 ByteOrder::Le => num.to_le_bytes().to_vec(), 23 ByteOrder::Le => num.to_le_bytes().to_vec(),
27 ByteOrder::Be => num.to_be_bytes().to_vec(), 24 ByteOrder::Be => num.to_be_bytes().to_vec(),
28 ByteOrder::Ne => num.to_ne_bytes().to_vec(), 25 ByteOrder::Ne => num.to_ne_bytes().to_vec(),
29 } 26 }
30 } 27 }
28
29 pub fn $to(&self, bytes: [u8; size_of::<$n>()]) -> $n {
30 match self {
31 ByteOrder::Le => $n::from_le_bytes(bytes),
32 ByteOrder::Be => $n::from_be_bytes(bytes),
33 ByteOrder::Ne => $n::from_ne_bytes(bytes),
34 }
35 }
31 )+} 36 )+}
32 }; 37 };
33} 38}
34impl_byte_order!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64); 39
40impl_byte_order!(
41 to_u8 from_u8 u8,
42 to_u16 form_u16 u16,
43 to_u32 form_u32 u32,
44 to_u64 form_u64 u64,
45
46 to_i8 form_i8 i8,
47 to_i16 form_i16 i16,
48 to_i32 form_i32 i32,
49 to_i64 form_i64 i64,
50
51 to_f32 form_f32 f32,
52 to_f64 form_f64 f64
53);
35 54
36impl VariantIndexType { 55impl VariantIndexType {
37 pub fn cast(&self, num: u32, byte_order: &ByteOrder) -> StructResult<Vec<u8>> { 56 pub fn cast(&self, num: u32, byte_order: &ByteOrder) -> Vec<u8> {
38 Ok(match self { 57 match self {
39 VariantIndexType::U8 => byte_order.u8(num 58 VariantIndexType::U8 => byte_order.from_u8(num as u8),
40 .try_into() 59 VariantIndexType::U16 => byte_order.form_u16(num as u16),
41 .map_err(|_| StructError::IncorrectEnumVariant)?), 60 VariantIndexType::U32 => byte_order.form_u32(num as u32),
42 VariantIndexType::U16 => byte_order.u16( 61 VariantIndexType::U64 => byte_order.form_u64(num as u64),
43 num.try_into() 62 }
44 .map_err(|_| StructError::IncorrectEnumVariant)?,
45 ),
46 VariantIndexType::U32 => byte_order.u32(
47 num.try_into()
48 .map_err(|_| StructError::IncorrectEnumVariant)?,
49 ),
50 VariantIndexType::U64 => byte_order.u64(
51 num.try_into()
52 .map_err(|_| StructError::IncorrectEnumVariant)?,
53 ),
54 VariantIndexType::I8 => byte_order.i8(num
55 .try_into()
56 .map_err(|_| StructError::IncorrectEnumVariant)?),
57 VariantIndexType::I16 => byte_order.i16(
58 num.try_into()
59 .map_err(|_| StructError::IncorrectEnumVariant)?,
60 ),
61 VariantIndexType::I32 => byte_order.i32(
62 num.try_into()
63 .map_err(|_| StructError::IncorrectEnumVariant)?,
64 ),
65 VariantIndexType::I64 => byte_order.i64(
66 num.try_into()
67 .map_err(|_| StructError::IncorrectEnumVariant)?,
68 ),
69 })
70 } 63 }
71} 64}
72 65
@@ -98,6 +91,11 @@ impl Settings {
98 object.serialize(&mut serializer)?; 91 object.serialize(&mut serializer)?;
99 Ok(serializer.to_bytes()) 92 Ok(serializer.to_bytes())
100 } 93 }
94
95 pub fn deserialize<'de, T: Deserialize<'de>>(self, object: &'de [u8]) -> StructResult<T> {
96 let mut deserializer = ArchiveDeserializer::new(object, self);
97 Ok(T::deserialize(&mut deserializer)?)
98 }
101} 99}
102 100
103impl Default for Settings { 101impl Default for Settings {