aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorIgor Tolmachov <me@igorek.dev>2023-09-08 17:33:59 +0900
committerIgor Tolmachev <me@igorek.dev>2024-06-23 15:34:33 +0900
commit9003b81813ff171edfc6101868c226c5c7d1957c (patch)
tree63db162b56b282bc2ec71f86f26dee8dbc2550c8 /src/utils.rs
parentb8c83ab5c133dc1330aa425a012d45f3c62e7ef1 (diff)
downloadarchivator-9003b81813ff171edfc6101868c226c5c7d1957c.tar.gz
archivator-9003b81813ff171edfc6101868c226c5c7d1957c.zip
Add basic zip reader
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
new file mode 100644
index 0000000..57ad1c9
--- /dev/null
+++ b/src/utils.rs
@@ -0,0 +1,60 @@
1use std::io::{Read, Result as IOResult};
2
3macro_rules! archive_datatype {
4 {
5 $vis:vis struct $struct_name:ident {
6 $(
7 $($fix_field_name:ident: $fix_field_type:ty)?
8 $([const] $const_field_name:ident: $const_field_type:ty = $const_field_value:expr)?
9 ,
10 )*
11 }
12 }
13 => {
14 #[derive(Debug)]
15 $vis struct $struct_name { $($(pub $fix_field_name: $fix_field_type,)?)* }
16
17 #[allow(unused)]
18 impl $struct_name {
19 $($(pub const $const_field_name: $const_field_type = $const_field_value;)?)*
20 }
21
22
23 impl ArchiveDatatype<{$($((<$fix_field_type>::BITS as usize / 8)+)?)* 0}> for $struct_name {
24 fn parse(buf: [u8; Self::SIZE]) -> Self {
25 let mut byte = 0;
26
27 $($(
28 byte += (<$fix_field_type>::BITS / 8) as usize;
29 let $fix_field_name = <$fix_field_type>::from_le_bytes(buf[byte - (<$fix_field_type>::BITS as usize / 8)..byte].try_into().unwrap());
30 )?)*
31
32 Self { $($($fix_field_name,)?)* }
33 }
34
35 fn dump(&self) -> [u8; Self::SIZE] {
36 [$($(&self.$fix_field_name.to_le_bytes()[..],)?)*]
37 .concat()
38 .try_into()
39 .unwrap()
40 }
41 }
42 }
43}
44pub(crate) use archive_datatype;
45
46pub trait ReadHelper: Read {
47 fn read2buf<const SIZE: usize>(&mut self) -> IOResult<[u8; SIZE]> {
48 let mut buf = [0; SIZE];
49 self.read(&mut buf)?;
50 Ok(buf)
51 }
52
53 fn read2vec(&mut self, size: usize) -> IOResult<Vec<u8>> {
54 let mut buf = vec![0; size];
55 self.read(&mut buf)?;
56 Ok(buf)
57 }
58}
59
60impl<R: Read> ReadHelper for R {}