aboutsummaryrefslogtreecommitdiff
path: root/src/datatypes.rs
blob: 771206493e034131bed91a4d4b19a7607731d17d (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
111
112
113
114
115
116
use crate::result::ArchiveResult;
use std::io::{Read, Write};

pub trait ArchiveDatatype
where
    Self: Sized,
{
    const SIZE: usize;

    fn read(reader: impl Read) -> ArchiveResult<Self>;

    fn write(&self, writer: impl Write) -> ArchiveResult<()>;
}

pub mod utils {
    pub fn read_string(buf: &[u8]) -> String {
        String::from_utf8(buf.into()).unwrap()
    }

    pub fn write_string(string: &str) -> &[u8] {
        string.as_bytes()
    }

    pub fn vec(vec: &[u8]) -> &[u8] {
        vec
    }

    #[macro_export]
    macro_rules! create_archive_datatype {
        {
            $vis:vis struct $struct_name:ident {
                $($field:ident: $field_type:ty,)*
            }

            $(
                let $custom_field:ident: $custom_field_type:ty {
                    size: $custom_field_size: expr,
                    read: $custom_field_read: path,
                    write: $custom_field_write: path,
                }
            )*

            $(
                const {
                    $($const_name:ident: $const_type:ty = $const_value:expr;)+
                }
            )?

            $(
                read $read_code:block
            )?

            $(
                write $write_code:block
            )?
        }
        => {
            #[derive(Debug)]
            $vis struct $struct_name {
                $($vis $field: $field_type),*,
                $($vis $custom_field: $custom_field_type),*
            }

            $(
                impl $struct_name {
                    $(const $const_name: $const_type = $const_value;)*
                }
            )?

            impl ArchiveDatatype for $struct_name {
                const SIZE: usize = ($((<$field_type>::BITS / 8)+)*0) as usize;

                fn read(mut reader: impl Read) -> ArchiveResult<Self> {
                    let mut buf = [0; Self::SIZE];
                    reader.read(&mut buf)?;

                    $(
                        let (bytes, buf) = buf.split_at((<$field_type>::BITS / 8) as usize);
                        let $field = <$field_type>::from_le_bytes(bytes.try_into().unwrap());
                    )*

                    $(
                        let $custom_field = {
                            let mut buf = vec![0; $custom_field_size as usize];
                            reader.read(&mut buf)?;
                            $custom_field_read(&buf).into()
                        };
                    )*

                    #[allow(dropping_references)]
                    drop(buf);

                    $($read_code;)?

                    Ok(
                        Self {
                            $($field),*,
                            $($custom_field),*
                        }
                    )
                }

                fn write(&self, mut writer: impl Write) -> ArchiveResult<()> {
                    $($write_code;)?
                    writer.write(&[
                        $(&self.$field.to_le_bytes()[..]),*,
                        $(&$custom_field_write(&self.$custom_field)),*
                    ].concat())?;
                    Ok(())
                }
            }
        };
    }

    pub use create_archive_datatype;
}