aboutsummaryrefslogtreecommitdiff
path: root/src/structs/settings.rs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-06-16 21:36:13 +0900
committerIgor Tolmachev <me@igorek.dev>2024-06-23 15:34:34 +0900
commitd6055b5ac4f3ff5016bc4881cf1cc109a22c40ba (patch)
tree40a9044f923945e6effc13c627261630dddc574c /src/structs/settings.rs
parent6444bee8f3e188be014841ea8cd7cfb53eb03ed9 (diff)
downloadarchivator-d6055b5ac4f3ff5016bc4881cf1cc109a22c40ba.tar.gz
archivator-d6055b5ac4f3ff5016bc4881cf1cc109a22c40ba.zip
Implement serialize
Diffstat (limited to 'src/structs/settings.rs')
-rw-r--r--src/structs/settings.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/structs/settings.rs b/src/structs/settings.rs
new file mode 100644
index 0000000..63c4d3a
--- /dev/null
+++ b/src/structs/settings.rs
@@ -0,0 +1,110 @@
1use crate::structs::{ArchiveSerializer, StructError, StructResult};
2use serde::Serialize;
3
4pub enum ByteOrder {
5 Le,
6 Be,
7 Ne,
8}
9
10pub enum VariantIndexType {
11 U8,
12 U16,
13 U32,
14 U64,
15 I8,
16 I16,
17 I32,
18 I64,
19}
20
21macro_rules! impl_byte_order {
22 ($($n:ident),+) => {
23 impl ByteOrder {$(
24 pub fn $n(&self, num: $n) -> Vec<u8> {
25 match self {
26 ByteOrder::Le => num.to_le_bytes().to_vec(),
27 ByteOrder::Be => num.to_be_bytes().to_vec(),
28 ByteOrder::Ne => num.to_ne_bytes().to_vec(),
29 }
30 }
31 )+}
32 };
33}
34impl_byte_order!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
35
36impl VariantIndexType {
37 pub fn cast(&self, num: u32, byte_order: &ByteOrder) -> StructResult<Vec<u8>> {
38 Ok(match self {
39 VariantIndexType::U8 => byte_order.u8(num
40 .try_into()
41 .map_err(|_| StructError::IncorrectEnumVariant)?),
42 VariantIndexType::U16 => byte_order.u16(
43 num.try_into()
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 }
71}
72
73pub struct Settings {
74 pub(crate) byte_order: ByteOrder,
75 pub(crate) variant_index_type: VariantIndexType,
76}
77
78impl Settings {
79 pub fn new(byte_order: ByteOrder, variant_index_type: VariantIndexType) -> Self {
80 Self {
81 byte_order,
82 variant_index_type,
83 }
84 }
85
86 pub fn byte_order(mut self, order: ByteOrder) -> Self {
87 self.byte_order = order;
88 self
89 }
90
91 pub fn variant_index_type(mut self, index_type: VariantIndexType) -> Self {
92 self.variant_index_type = index_type;
93 self
94 }
95
96 pub fn serialize(self, object: &impl Serialize) -> StructResult<Vec<u8>> {
97 let mut serializer = ArchiveSerializer::new(self);
98 object.serialize(&mut serializer)?;
99 Ok(serializer.to_bytes())
100 }
101}
102
103impl Default for Settings {
104 fn default() -> Self {
105 Self {
106 byte_order: ByteOrder::Le,
107 variant_index_type: VariantIndexType::U32,
108 }
109 }
110}