blob: 57ad1c99a946440cfcf710f2b635f29f8199e6d2 (
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
|
use std::io::{Read, Result as IOResult};
macro_rules! archive_datatype {
{
$vis:vis struct $struct_name:ident {
$(
$($fix_field_name:ident: $fix_field_type:ty)?
$([const] $const_field_name:ident: $const_field_type:ty = $const_field_value:expr)?
,
)*
}
}
=> {
#[derive(Debug)]
$vis struct $struct_name { $($(pub $fix_field_name: $fix_field_type,)?)* }
#[allow(unused)]
impl $struct_name {
$($(pub const $const_field_name: $const_field_type = $const_field_value;)?)*
}
impl ArchiveDatatype<{$($((<$fix_field_type>::BITS as usize / 8)+)?)* 0}> for $struct_name {
fn parse(buf: [u8; Self::SIZE]) -> Self {
let mut byte = 0;
$($(
byte += (<$fix_field_type>::BITS / 8) as usize;
let $fix_field_name = <$fix_field_type>::from_le_bytes(buf[byte - (<$fix_field_type>::BITS as usize / 8)..byte].try_into().unwrap());
)?)*
Self { $($($fix_field_name,)?)* }
}
fn dump(&self) -> [u8; Self::SIZE] {
[$($(&self.$fix_field_name.to_le_bytes()[..],)?)*]
.concat()
.try_into()
.unwrap()
}
}
}
}
pub(crate) use archive_datatype;
pub trait ReadHelper: Read {
fn read2buf<const SIZE: usize>(&mut self) -> IOResult<[u8; SIZE]> {
let mut buf = [0; SIZE];
self.read(&mut buf)?;
Ok(buf)
}
fn read2vec(&mut self, size: usize) -> IOResult<Vec<u8>> {
let mut buf = vec![0; size];
self.read(&mut buf)?;
Ok(buf)
}
}
impl<R: Read> ReadHelper for R {}
|