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
use super::*;
use core::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

pub trait FixedLenInt: TryFrom<usize> {}
impl FixedLenInt for u8 {}
impl FixedLenInt for u16 {}
impl FixedLenInt for u32 {}
impl FixedLenInt for u64 {}
impl FixedLenInt for usize {}

/// `Record` can be used to represent fixed-size integer to represent the length of a record.
///
/// It accepts fixed-length unsigned interger type of `N` (`u8`, `u32`, `usize`, etc..) and a generic type of `T` (`Vec<T>`, `String` etc..)
/// ### Example
///
/// ```rust
/// use bin_layout::{Record, Encoder};
///
/// let record: Record<u8, &str> = "HelloWorld".into();
/// assert_eq!(record.len(), 10);
///
/// let bytes = record.encode();
/// assert_eq!(bytes.len(), 11);
/// ```
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Record<Len: FixedLenInt, T> {
    pub data: T,
    _marker: PhantomData<Len>,
}

// ---------------------------------------------------------------------------------

macro_rules! impls {
    [Encoder for $($ty:ty),*] => {$(
        impl<Len: Encoder + FixedLenInt> Encoder for Record<Len, $ty> {
            #[inline]
            fn size_hint(&self) -> usize {
                let bytes: &[u8] = self.as_ref();
                Len::SIZE + bytes.len()
            }
            #[inline]
            fn encoder(self, c: &mut Cursor<impl Bytes>) {
                let len: Len = unsafe { self.data.len().try_into().unwrap_unchecked() };
                len.encoder(c);
                c.write_slice(self.data);
            }
        }
    )*};
}
impls!(Encoder for &[u8], &str, String);

impl<'de, E: Error, Len> Decoder<'de, E> for Record<Len, &'de [u8]>
where
    usize: TryFrom<Len>,
    Len: FixedLenInt + Decoder<'de, E>,
{
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, E> {
        let len: usize = Len::decoder(c)?.try_into().map_err(|_| E::invalid_data())?;
        c.read_slice(len).map(|bytes| bytes.into())
    }
}

impl<'de, E: Error, Len> Decoder<'de, E> for Record<Len, &'de str>
where
    usize: TryFrom<Len>,
    Len: FixedLenInt + Decoder<'de, E>,
{
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, E> {
        let bytes: Record<Len, &'de [u8]> = Record::decoder(c)?;

        core::str::from_utf8(bytes.data)
            .map(|string| string.into())
            .map_err(E::utf8_err)
    }
}

impl<'de, E: Error, Len> Decoder<'de, E> for Record<Len, String>
where
    usize: TryFrom<Len>,
    Len: FixedLenInt + Decoder<'de, E>,
{
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, E> {
        let bytes: Record<Len, &'de [u8]> = Record::decoder(c)?;

        String::from_utf8(bytes.to_vec())
            .map(|string| string.into())
            .map_err(E::from_utf8_err)
    }
}

impl<Len, T> Encoder for Record<Len, Vec<T>>
where
    T: Encoder,
    Len: Encoder + FixedLenInt,
{
    #[inline]
    fn size_hint(&self) -> usize {
        Len::SIZE + self.iter().map(T::size_hint).sum::<usize>()
    }

    #[inline]
    fn encoder(self, c: &mut Cursor<impl Bytes>) {
        let len: Len = unsafe { self.data.len().try_into().unwrap_unchecked() };
        len.encoder(c);

        for record in self.data {
            record.encoder(c);
        }
    }
}

impl<'de, E: Error, Len, T> Decoder<'de, E> for Record<Len, Vec<T>>
where
    T: Decoder<'de, E>,
    usize: TryFrom<Len>,
    Len: Decoder<'de, E> + FixedLenInt,
{
    #[inline]
    fn decoder(c: &mut Cursor<&'de [u8]>) -> Result<Self, E> {
        let len: usize = Len::decoder(c)?.try_into().map_err(|_| E::invalid_data())?;

        let mut vec = Vec::with_capacity(len as usize);
        for _ in 0..len {
            vec.push(T::decoder(c)?);
        }
        Ok(vec.into())
    }
}

impl<N: FixedLenInt, T> Record<N, T> {
    #[inline]
    pub fn new(data: T) -> Self {
        Self {
            data,
            _marker: PhantomData,
        }
    }
}
impl<N: FixedLenInt, T: fmt::Debug> fmt::Debug for Record<N, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.data.fmt(f)
    }
}
impl<N: FixedLenInt, T> From<T> for Record<N, T> {
    #[inline]
    fn from(data: T) -> Self {
        Self::new(data)
    }
}
impl<N: FixedLenInt, T> Deref for Record<N, T> {
    type Target = T;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}
impl<N: FixedLenInt, T> DerefMut for Record<N, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}