messagepack_core/decode/
int.rs

1use super::{Decode, Error, Result};
2use crate::formats::Format;
3
4impl<'a> Decode<'a> for u8 {
5    type Value = Self;
6    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
7        let (format, buf) = Format::decode(buf)?;
8        Self::decode_with_format(format, buf)
9    }
10
11    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
12        match format {
13            Format::PositiveFixInt(v) => Ok((v, buf)),
14            Format::Uint8 => {
15                let (first, rest) = buf.split_first().ok_or(Error::EofData)?;
16                Ok((*first, rest))
17            }
18            _ => Err(Error::UnexpectedFormat),
19        }
20    }
21}
22
23impl<'a> Decode<'a> for i8 {
24    type Value = Self;
25    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
26        let (format, buf) = Format::decode(buf)?;
27        Self::decode_with_format(format, buf)
28    }
29    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
30        match format {
31            Format::Int8 => {
32                let (first, rest) = buf.split_first().ok_or(Error::EofData)?;
33                Ok((*first as i8, rest))
34            }
35            Format::NegativeFixInt(v) => Ok((v, buf)),
36            _ => Err(Error::UnexpectedFormat),
37        }
38    }
39}
40
41macro_rules! impl_decode_int {
42    ($ty:ty,$format:path) => {
43        impl<'a> Decode<'a> for $ty {
44            type Value = Self;
45
46            fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
47                let (format, buf) = Format::decode(buf)?;
48                Self::decode_with_format(format, buf)
49            }
50            fn decode_with_format(
51                format: Format,
52                buf: &'a [u8],
53            ) -> Result<(Self::Value, &'a [u8])> {
54                match format {
55                    $format => {
56                        const SIZE: usize = core::mem::size_of::<$ty>();
57
58                        let (data, rest) = buf.split_at_checked(SIZE).ok_or(Error::EofData)?;
59                        let data: [u8; SIZE] = data.try_into().map_err(|_| Error::EofData)?;
60                        let val = <$ty>::from_be_bytes(data);
61                        Ok((val, rest))
62                    }
63                    _ => Err(Error::UnexpectedFormat),
64                }
65            }
66        }
67    };
68}
69
70impl_decode_int!(u16, Format::Uint16);
71impl_decode_int!(u32, Format::Uint32);
72impl_decode_int!(u64, Format::Uint64);
73impl_decode_int!(i16, Format::Int16);
74impl_decode_int!(i32, Format::Int32);
75impl_decode_int!(i64, Format::Int64);
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn decode_u8_fix_pos_int() {
83        // FixPosInt
84        let buf: &[u8] = &[0x00];
85        let (decoded, rest) = u8::decode(buf).unwrap();
86        let expect = u8::MIN;
87        assert_eq!(decoded, expect);
88        assert_eq!(rest.len(), 0);
89
90        let buf: &[u8] = &[0x7f];
91        let (decoded, rest) = u8::decode(buf).unwrap();
92        let expect = 0x7f;
93        assert_eq!(decoded, expect);
94        assert_eq!(rest.len(), 0);
95    }
96
97    #[test]
98    fn decode_u8() {
99        // Uint8
100        let buf: &[u8] = &[0xcc, 0x00];
101        let (decoded, rest) = u8::decode(buf).unwrap();
102        let expect = u8::MIN;
103        assert_eq!(decoded, expect);
104        assert_eq!(rest.len(), 0);
105
106        let buf: &[u8] = &[0xcc, 0xff];
107        let (decoded, rest) = u8::decode(buf).unwrap();
108        let expect = u8::MAX;
109        assert_eq!(decoded, expect);
110        assert_eq!(rest.len(), 0);
111    }
112
113    #[test]
114    fn decode_u16() {
115        // Uint16
116        let buf: &[u8] = &[0xcd, 0x00, 0x00];
117        let (decoded, rest) = u16::decode(buf).unwrap();
118        let expect = u16::MIN;
119        assert_eq!(decoded, expect);
120        assert_eq!(rest.len(), 0);
121
122        let buf: &[u8] = &[0xcd, 0x12, 0x34];
123        let (decoded, rest) = u16::decode(buf).unwrap();
124        let expect = 0x1234;
125        assert_eq!(decoded, expect);
126        assert_eq!(rest.len(), 0);
127
128        let buf: &[u8] = &[0xcd, 0xff, 0xff];
129        let (decoded, rest) = u16::decode(buf).unwrap();
130        let expect = u16::MAX;
131        assert_eq!(decoded, expect);
132        assert_eq!(rest.len(), 0);
133    }
134
135    #[test]
136    fn decode_u32() {
137        // Uint32
138        let buf: &[u8] = &[0xce, 0x00, 0x00, 0x00, 0x00];
139        let (decoded, rest) = u32::decode(buf).unwrap();
140        let expect = u32::MIN;
141        assert_eq!(decoded, expect);
142        assert_eq!(rest.len(), 0);
143
144        let buf: &[u8] = &[0xce, 0x12, 0x34, 0x56, 0x78];
145        let (decoded, rest) = u32::decode(buf).unwrap();
146        let expect = 0x12345678;
147        assert_eq!(decoded, expect);
148        assert_eq!(rest.len(), 0);
149
150        let buf: &[u8] = &[0xce, 0xff, 0xff, 0xff, 0xff];
151        let (decoded, rest) = u32::decode(buf).unwrap();
152        let expect = u32::MAX;
153        assert_eq!(decoded, expect);
154        assert_eq!(rest.len(), 0);
155    }
156
157    #[test]
158    fn decode_u64() {
159        // Uint64
160        let buf: &[u8] = &[0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
161        let (decoded, rest) = u64::decode(buf).unwrap();
162        let expect = u64::MIN;
163        assert_eq!(decoded, expect);
164        assert_eq!(rest.len(), 0);
165
166        let buf: &[u8] = &[0xcf, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
167        let (decoded, rest) = u64::decode(buf).unwrap();
168        let expect = 0x1234567812345678;
169        assert_eq!(decoded, expect);
170        assert_eq!(rest.len(), 0);
171
172        let buf: &[u8] = &[0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
173        let (decoded, rest) = u64::decode(buf).unwrap();
174        let expect = u64::MAX;
175        assert_eq!(decoded, expect);
176        assert_eq!(rest.len(), 0);
177    }
178
179    #[test]
180    fn decode_i8_fix_neg_int() {
181        // FixNegInt
182        let buf: &[u8] = &[0xff];
183        let (decoded, rest) = i8::decode(buf).unwrap();
184        let expect = -1;
185        assert_eq!(decoded, expect);
186        assert_eq!(rest.len(), 0);
187
188        let buf: &[u8] = &[0xe0];
189        let (decoded, rest) = i8::decode(buf).unwrap();
190        let expect = -32;
191        assert_eq!(decoded, expect);
192        assert_eq!(rest.len(), 0);
193    }
194
195    #[test]
196    fn decode_i8() {
197        // Int8
198        let buf: &[u8] = &[0xd0, 0x80];
199        let (decoded, rest) = i8::decode(buf).unwrap();
200        let expect = i8::MIN;
201        assert_eq!(decoded, expect);
202        assert_eq!(rest.len(), 0);
203
204        let buf: &[u8] = &[0xd0, 0x7f];
205        let (decoded, rest) = i8::decode(buf).unwrap();
206        let expect = i8::MAX;
207        assert_eq!(decoded, expect);
208        assert_eq!(rest.len(), 0);
209    }
210
211    #[test]
212    fn decode_i16() {
213        // Int16
214        let buf: &[u8] = &[0xd1, 0x80, 0x00];
215        let (decoded, rest) = i16::decode(buf).unwrap();
216        let expect = i16::MIN;
217        assert_eq!(decoded, expect);
218        assert_eq!(rest.len(), 0);
219
220        let buf: &[u8] = &[0xd1, 0x7f, 0xff];
221        let (decoded, rest) = i16::decode(buf).unwrap();
222        let expect = i16::MAX;
223        assert_eq!(decoded, expect);
224        assert_eq!(rest.len(), 0);
225    }
226
227    #[test]
228    fn decode_i32() {
229        // Int16
230        let buf: &[u8] = &[0xd2, 0x80, 0x00, 0x00, 0x00];
231        let (decoded, rest) = i32::decode(buf).unwrap();
232        let expect = i32::MIN;
233        assert_eq!(decoded, expect);
234        assert_eq!(rest.len(), 0);
235
236        let buf: &[u8] = &[0xd2, 0x7f, 0xff, 0xff, 0xff];
237        let (decoded, rest) = i32::decode(buf).unwrap();
238        let expect = i32::MAX;
239        assert_eq!(decoded, expect);
240        assert_eq!(rest.len(), 0);
241    }
242
243    #[test]
244    fn decode_i64() {
245        // Int16
246        let buf: &[u8] = &[0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
247        let (decoded, rest) = i64::decode(buf).unwrap();
248        let expect = i64::MIN;
249        assert_eq!(decoded, expect);
250        assert_eq!(rest.len(), 0);
251
252        let buf: &[u8] = &[0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
253        let (decoded, rest) = i64::decode(buf).unwrap();
254        let expect = i64::MAX;
255        assert_eq!(decoded, expect);
256        assert_eq!(rest.len(), 0);
257    }
258}