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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#![feature(more_qualified_paths)]
#![feature(iter_next_chunk)]
#![no_std]

#[cfg(feature = "non_fixed")]
extern crate alloc;

use core::fmt::Debug;

#[cfg(feature = "non_fixed")]
use alloc::borrow::Cow;

#[derive(Debug)]
/// A generic error encountered while parsing.
pub enum ParserError {
    /// The parser expected more data than it got.
    TooLittleData(usize),
    /// Just like TooLittleData, but more specific.
    HeaderIncomplete(usize),
    /// The expected magic was invalid.
    InvalidMagic,
    /// A value wasn't understood by the parser.
    ValueNotUnderstood,
}

/// Relevant for parsing numbers.
pub enum Endian {
    Little,
    Big,
}

#[cfg(feature = "non_fixed")]
/// A trait for reading a non fixed amount of data.
pub trait Read
where
    Self: Sized,
{
    fn from_bytes(data: &mut impl ExactSizeIterator<Item = u8>) -> Result<Self, ParserError>;
}
#[cfg(feature = "non_fixed")]
/// A trait for reading a non fixed amount of data, with context.
pub trait ReadCtx<Ctx>
where
    Self: Sized,
{
    fn from_bytes(
        data: &mut impl ExactSizeIterator<Item = u8>,
        ctx: Ctx,
    ) -> Result<Self, ParserError>;
}
#[cfg(feature = "non_fixed")]
/// A trait for writing data of variable length.
pub trait Write<'a> {
    fn to_bytes(&self) -> Cow<'a, [u8]>;
}
#[cfg(feature = "non_fixed")]
/// A trait for writing data of variable length, with context.
pub trait WriteCtx<'a, Ctx> {
    fn to_bytes(&self, ctx: Ctx) -> Cow<'a, [u8]>;
}
/// A trait for reading data of fixed length.
pub trait ReadFixed<const N: usize>
where
    Self: Sized,
{
    fn from_bytes(data: &[u8; N]) -> Result<Self, ParserError>;
}
/// A trait for reading data of fixed length, with context.
pub trait ReadFixedCtx<const N: usize, Ctx>
where
    Self: Sized,
{
    fn from_bytes(data: &[u8; N], ctx: Ctx) -> Result<Self, ParserError>;
}
/// A trait for writing data of fixed length.
pub trait WriteFixed<const N: usize>
where
    Self: Sized,
{
    fn to_bytes(&self) -> [u8; N];
}
/// A trait for writing data of fixed length, with context.
pub trait WriteFixedCtx<const N: usize, Ctx>
where
    Self: Sized,
{
    fn to_bytes(&self, ctx: Ctx) -> [u8; N];
}
#[cfg(feature = "non_fixed")]
impl<T> Read for Vec<T>
where
    T: Read,
{
    fn from_bytes(data: &mut impl ExactSizeIterator<Item = u8>) -> Result<Self, ParserError> {
        Ok((0..).map_while(|_| T::from_bytes(data).ok()).collect()) // Create an infinte iterator, map until from_bytes returns an Error and collect.
    }
}

#[cfg(feature = "non_fixed")]
impl<T, Ctx> ReadCtx<Ctx> for Vec<T>
where
    T: ReadCtx<Ctx>,
    Ctx: Clone,
{
    fn from_bytes(
        data: &mut impl ExactSizeIterator<Item = u8>,
        ctx: Ctx,
    ) -> Result<Self, ParserError> {
        Ok((0..)
            .map_while(|_| T::from_bytes(data, ctx.clone()).ok())
            .collect()) // Create an infinte iterator, map until from_bytes returns an Error and collect.
    }
}
#[cfg(feature = "non_fixed")]
impl<'a, T> Write<'a> for Vec<T>
where
    T: Write<'a>,
{
    fn to_bytes(&self) -> Cow<'a, [u8]> {
        self.iter()
            .map(T::to_bytes)
            .collect::<Vec<Cow<[u8]>>>()
            .concat()
            .into()
    }
}
#[cfg(feature = "non_fixed")]
impl<'a, T, Ctx> WriteCtx<'a, Ctx> for Vec<T>
where
    T: WriteCtx<'a, Ctx>,
    Ctx: Clone,
{
    fn to_bytes(&self, ctx: Ctx) -> Cow<'a, [u8]> {
        self.iter()
            .map(|x| T::to_bytes(x, ctx.clone()))
            .collect::<Vec<Cow<[u8]>>>()
            .concat()
            .into()
    }
}
#[macro_export]
/// This macro allows turning enums into numbers and vice versa.
/// ```
/// #![feature(more_qualified_paths)]
///
/// #[derive(Debug, PartialEq)]
/// enum ABC {
///     A,
///     B,
///     C,
///     Unknown(u8),
/// }
/// bin_utils::enum_to_int! {
///     u8,
///     ABC,
///
///     0x01,
///     ABC::A,
///     0x02,
///     ABC::B,
///     0x03,
///     ABC::C
/// }
///
/// let a: ABC = 0x01.into();
/// assert_eq!(a, ABC::A);
/// let a: u8 = a.into();
/// assert_eq!(a, 0x01);
/// let unknown: ABC = 0xff.into();
/// assert_eq!(unknown, ABC::Unknown(0xff));
/// ```
macro_rules! enum_to_int {
    ($a:ty, $b:ty, $($x:expr, $y:path), +) => {
        impl From<$a> for $b {
            fn from(value: $a) -> Self {
                match value {
                    $($x => $y,)+
                    _ => Self::Unknown(value),
                }
            }
        }
        impl From<$b> for $a {
            fn from(value: $b) -> Self {
                match value {
                    $($y => $x,)+
                    <$b>::Unknown(value) => value
                }
            }
        }
    }
}

#[cfg(feature = "numeric_rw")]
mod numeric_rw {
    use super::*;
    use alloc::borrow::ToOwned;
    impl Read for u8 {
        fn from_bytes(data: &mut impl ExactSizeIterator<Item = u8>) -> Result<Self, ParserError> {
            data.next().ok_or(ParserError::TooLittleData(1))
        }
    }
    impl Read for i8 {
        fn from_bytes(data: &mut impl ExactSizeIterator<Item = u8>) -> Result<Self, ParserError> {
            data.next()
                .ok_or(ParserError::TooLittleData(1))
                .map(|x| x as i8)
        }
    }
    impl<'a> Write<'a> for u8 {
        fn to_bytes(&self) -> Cow<'a, [u8]> {
            Cow::Owned([*self].as_slice().to_owned())
        }
    }
    impl<'a> Write<'a> for i8 {
        fn to_bytes(&self) -> Cow<'a, [u8]> {
            Cow::Owned([*self as u8].as_slice().to_owned())
        }
    }
    macro_rules! impl_rw_numeric {
        ($number_type:ty) => {
            impl ReadCtx<Endian> for $number_type {
                fn from_bytes(
                    data: &mut impl ExactSizeIterator<Item = u8>,
                    ctx: Endian,
                ) -> Result<Self, ParserError> {
                    Ok(match ctx {
                        Endian::Little => {
                            <$number_type>::from_le_bytes(data.next_chunk().map_err(|x| {
                                ParserError::TooLittleData(
                                    core::mem::size_of::<$number_type>() - x.len(),
                                )
                            })?)
                        }
                        Endian::Big => {
                            <$number_type>::from_be_bytes(data.next_chunk().map_err(|x| {
                                ParserError::TooLittleData(
                                    core::mem::size_of::<$number_type>() - x.len(),
                                )
                            })?)
                        }
                    })
                }
            }
            impl<'a> WriteCtx<'a, Endian> for $number_type {
                fn to_bytes(&self, ctx: Endian) -> Cow<'a, [u8]> {
                    match ctx {
                        Endian::Little => self.to_le_bytes(),
                        Endian::Big => self.to_be_bytes(),
                    }
                    .as_slice()
                    .to_owned()
                    .into()
                }
            }
        };
    }
    impl_rw_numeric!(u16);
    impl_rw_numeric!(i16);
    impl_rw_numeric!(u32);
    impl_rw_numeric!(i32);
    impl_rw_numeric!(u64);
    impl_rw_numeric!(i64);
    impl_rw_numeric!(u128);
    impl_rw_numeric!(i128);
}
use alloc::vec::Vec;
pub use numeric_rw::*;