aboutsummaryrefslogtreecommitdiff
path: root/src/structs/settings.rs
blob: cf714537274a74cbbcf9ae155e8b4a6bc4ffb4d4 (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
use crate::structs::{ArchiveDeserializer, ArchiveSerializer, StructResult};
use serde::{Deserialize, Serialize};
use std::mem::size_of;

pub enum ByteOrder {
    Le,
    Be,
    Ne,
}

pub enum VariantIndexType {
    U8,
    U16,
    U32,
    U64,
}

macro_rules! impl_byte_order {
    ($($to:ident $fr:ident $n:ident),+) => {
        impl ByteOrder {$(
            pub fn $fr(&self, num: $n) -> Vec<u8> {
                match self {
                    Self::Le => num.to_le_bytes().to_vec(),
                    Self::Be => num.to_be_bytes().to_vec(),
                    Self::Ne => num.to_ne_bytes().to_vec(),
                }
            }

            pub fn $to(&self, bytes: [u8; size_of::<$n>()]) -> $n {
                match self {
                    Self::Le => $n::from_le_bytes(bytes),
                    Self::Be => $n::from_be_bytes(bytes),
                    Self::Ne => $n::from_ne_bytes(bytes),
                }
            }
        )+}
    };
}

impl_byte_order!(
    to_u8 from_u8 u8,
    to_u16 form_u16 u16,
    to_u32 form_u32 u32,
    to_u64 form_u64 u64,

    to_i8 form_i8 i8,
    to_i16 form_i16 i16,
    to_i32 form_i32 i32,
    to_i64 form_i64 i64,

    to_f32 form_f32 f32,
    to_f64 form_f64 f64
);

impl VariantIndexType {
    pub fn cast(&self, num: u32, byte_order: &ByteOrder) -> Vec<u8> {
        match self {
            Self::U8 => byte_order.from_u8(num as u8),
            Self::U16 => byte_order.form_u16(num as u16),
            Self::U32 => byte_order.form_u32(num),
            Self::U64 => byte_order.form_u64(num as u64),
        }
    }
}

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())
    }

    pub fn deserialize<'de, T: Deserialize<'de>>(self, object: &'de [u8]) -> StructResult<T> {
        let mut deserializer = ArchiveDeserializer::new(object, self);
        Ok(T::deserialize(&mut deserializer)?)
    }
}

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