aboutsummaryrefslogtreecommitdiff
path: root/src/structs/settings.rs
blob: 63c4d3acc96c064ded7e918aa5836565c7685fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::structs::{ArchiveSerializer, StructError, StructResult};
use serde::Serialize;

pub enum ByteOrder {
    Le,
    Be,
    Ne,
}

pub enum VariantIndexType {
    U8,
    U16,
    U32,
    U64,
    I8,
    I16,
    I32,
    I64,
}

macro_rules! impl_byte_order {
    ($($n:ident),+) => {
        impl ByteOrder {$(
            pub fn $n(&self, num: $n) -> Vec<u8> {
                match self {
                    ByteOrder::Le => num.to_le_bytes().to_vec(),
                    ByteOrder::Be => num.to_be_bytes().to_vec(),
                    ByteOrder::Ne => num.to_ne_bytes().to_vec(),
                }
            }
        )+}
    };
}
impl_byte_order!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);

impl VariantIndexType {
    pub fn cast(&self, num: u32, byte_order: &ByteOrder) -> StructResult<Vec<u8>> {
        Ok(match self {
            VariantIndexType::U8 => byte_order.u8(num
                .try_into()
                .map_err(|_| StructError::IncorrectEnumVariant)?),
            VariantIndexType::U16 => byte_order.u16(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
            VariantIndexType::U32 => byte_order.u32(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
            VariantIndexType::U64 => byte_order.u64(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
            VariantIndexType::I8 => byte_order.i8(num
                .try_into()
                .map_err(|_| StructError::IncorrectEnumVariant)?),
            VariantIndexType::I16 => byte_order.i16(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
            VariantIndexType::I32 => byte_order.i32(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
            VariantIndexType::I64 => byte_order.i64(
                num.try_into()
                    .map_err(|_| StructError::IncorrectEnumVariant)?,
            ),
        })
    }
}

pub struct Settings {
    pub(crate) byte_order: ByteOrder,
    pub(crate) variant_index_type: VariantIndexType,
}

impl Settings {
    pub fn new(byte_order: ByteOrder, variant_index_type: VariantIndexType) -> Self {
        Self {
            byte_order,
            variant_index_type,
        }
    }

    pub fn byte_order(mut self, order: ByteOrder) -> Self {
        self.byte_order = order;
        self
    }

    pub fn variant_index_type(mut self, index_type: VariantIndexType) -> Self {
        self.variant_index_type = index_type;
        self
    }

    pub fn serialize(self, object: &impl Serialize) -> StructResult<Vec<u8>> {
        let mut serializer = ArchiveSerializer::new(self);
        object.serialize(&mut serializer)?;
        Ok(serializer.to_bytes())
    }
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            byte_order: ByteOrder::Le,
            variant_index_type: VariantIndexType::U32,
        }
    }
}