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
77impl<'a> Decode<'a> for u128 {
78    type Value = Self;
79
80    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
81        let (val, buf) = u64::decode(buf)?;
82        Ok((Self::from(val), buf))
83    }
84
85    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
86        let (val, buf) = u64::decode_with_format(format, buf)?;
87        Ok((Self::from(val), buf))
88    }
89}
90
91impl<'a> Decode<'a> for i128 {
92    type Value = Self;
93
94    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
95        let (val, buf) = i64::decode(buf)?;
96        Ok((Self::from(val), buf))
97    }
98
99    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
100        let (val, buf) = i64::decode_with_format(format, buf)?;
101        Ok((Self::from(val), buf))
102    }
103}
104
105impl<'a> Decode<'a> for usize {
106    type Value = Self;
107
108    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
109        let (val, buf) = u64::decode(buf)?;
110        match usize::try_from(val) {
111            Ok(usize_val) => Ok((usize_val, buf)),
112            Err(_) => Err(Error::InvalidData),
113        }
114    }
115
116    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
117        let (val, buf) = u64::decode_with_format(format, buf)?;
118        match usize::try_from(val) {
119            Ok(usize_val) => Ok((usize_val, buf)),
120            Err(_) => Err(Error::InvalidData),
121        }
122    }
123}
124
125impl<'a> Decode<'a> for isize {
126    type Value = Self;
127
128    fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
129        let (val, buf) = i64::decode(buf)?;
130        match isize::try_from(val) {
131            Ok(isize_val) => Ok((isize_val, buf)),
132            Err(_) => Err(Error::InvalidData),
133        }
134    }
135
136    fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
137        let (val, buf) = i64::decode_with_format(format, buf)?;
138        match isize::try_from(val) {
139            Ok(isize_val) => Ok((isize_val, buf)),
140            Err(_) => Err(Error::InvalidData),
141        }
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn decode_u8_fix_pos_int() {
151        // FixPosInt
152        let buf: &[u8] = &[0x00];
153        let (decoded, rest) = u8::decode(buf).unwrap();
154        let expect = u8::MIN;
155        assert_eq!(decoded, expect);
156        assert_eq!(rest.len(), 0);
157
158        let buf: &[u8] = &[0x7f];
159        let (decoded, rest) = u8::decode(buf).unwrap();
160        let expect = 0x7f;
161        assert_eq!(decoded, expect);
162        assert_eq!(rest.len(), 0);
163    }
164
165    #[test]
166    fn decode_u8() {
167        // Uint8
168        let buf: &[u8] = &[0xcc, 0x00];
169        let (decoded, rest) = u8::decode(buf).unwrap();
170        let expect = u8::MIN;
171        assert_eq!(decoded, expect);
172        assert_eq!(rest.len(), 0);
173
174        let buf: &[u8] = &[0xcc, 0xff];
175        let (decoded, rest) = u8::decode(buf).unwrap();
176        let expect = u8::MAX;
177        assert_eq!(decoded, expect);
178        assert_eq!(rest.len(), 0);
179    }
180
181    #[test]
182    fn decode_u16() {
183        // Uint16
184        let buf: &[u8] = &[0xcd, 0x00, 0x00];
185        let (decoded, rest) = u16::decode(buf).unwrap();
186        let expect = u16::MIN;
187        assert_eq!(decoded, expect);
188        assert_eq!(rest.len(), 0);
189
190        let buf: &[u8] = &[0xcd, 0x12, 0x34];
191        let (decoded, rest) = u16::decode(buf).unwrap();
192        let expect = 0x1234;
193        assert_eq!(decoded, expect);
194        assert_eq!(rest.len(), 0);
195
196        let buf: &[u8] = &[0xcd, 0xff, 0xff];
197        let (decoded, rest) = u16::decode(buf).unwrap();
198        let expect = u16::MAX;
199        assert_eq!(decoded, expect);
200        assert_eq!(rest.len(), 0);
201    }
202
203    #[test]
204    fn decode_u32() {
205        // Uint32
206        let buf: &[u8] = &[0xce, 0x00, 0x00, 0x00, 0x00];
207        let (decoded, rest) = u32::decode(buf).unwrap();
208        let expect = u32::MIN;
209        assert_eq!(decoded, expect);
210        assert_eq!(rest.len(), 0);
211
212        let buf: &[u8] = &[0xce, 0x12, 0x34, 0x56, 0x78];
213        let (decoded, rest) = u32::decode(buf).unwrap();
214        let expect = 0x12345678;
215        assert_eq!(decoded, expect);
216        assert_eq!(rest.len(), 0);
217
218        let buf: &[u8] = &[0xce, 0xff, 0xff, 0xff, 0xff];
219        let (decoded, rest) = u32::decode(buf).unwrap();
220        let expect = u32::MAX;
221        assert_eq!(decoded, expect);
222        assert_eq!(rest.len(), 0);
223    }
224
225    #[test]
226    fn decode_u64() {
227        // Uint64
228        let buf: &[u8] = &[0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
229        let (decoded, rest) = u64::decode(buf).unwrap();
230        let expect = u64::MIN;
231        assert_eq!(decoded, expect);
232        assert_eq!(rest.len(), 0);
233
234        let buf: &[u8] = &[0xcf, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78];
235        let (decoded, rest) = u64::decode(buf).unwrap();
236        let expect = 0x1234567812345678;
237        assert_eq!(decoded, expect);
238        assert_eq!(rest.len(), 0);
239
240        let buf: &[u8] = &[0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
241        let (decoded, rest) = u64::decode(buf).unwrap();
242        let expect = u64::MAX;
243        assert_eq!(decoded, expect);
244        assert_eq!(rest.len(), 0);
245    }
246
247    #[test]
248    fn decode_i8_fix_neg_int() {
249        // FixNegInt
250        let buf: &[u8] = &[0xff];
251        let (decoded, rest) = i8::decode(buf).unwrap();
252        let expect = -1;
253        assert_eq!(decoded, expect);
254        assert_eq!(rest.len(), 0);
255
256        let buf: &[u8] = &[0xe0];
257        let (decoded, rest) = i8::decode(buf).unwrap();
258        let expect = -32;
259        assert_eq!(decoded, expect);
260        assert_eq!(rest.len(), 0);
261    }
262
263    #[test]
264    fn decode_i8() {
265        // Int8
266        let buf: &[u8] = &[0xd0, 0x80];
267        let (decoded, rest) = i8::decode(buf).unwrap();
268        let expect = i8::MIN;
269        assert_eq!(decoded, expect);
270        assert_eq!(rest.len(), 0);
271
272        let buf: &[u8] = &[0xd0, 0x7f];
273        let (decoded, rest) = i8::decode(buf).unwrap();
274        let expect = i8::MAX;
275        assert_eq!(decoded, expect);
276        assert_eq!(rest.len(), 0);
277    }
278
279    #[test]
280    fn decode_i16() {
281        // Int16
282        let buf: &[u8] = &[0xd1, 0x80, 0x00];
283        let (decoded, rest) = i16::decode(buf).unwrap();
284        let expect = i16::MIN;
285        assert_eq!(decoded, expect);
286        assert_eq!(rest.len(), 0);
287
288        let buf: &[u8] = &[0xd1, 0x7f, 0xff];
289        let (decoded, rest) = i16::decode(buf).unwrap();
290        let expect = i16::MAX;
291        assert_eq!(decoded, expect);
292        assert_eq!(rest.len(), 0);
293    }
294
295    #[test]
296    fn decode_i32() {
297        // Int16
298        let buf: &[u8] = &[0xd2, 0x80, 0x00, 0x00, 0x00];
299        let (decoded, rest) = i32::decode(buf).unwrap();
300        let expect = i32::MIN;
301        assert_eq!(decoded, expect);
302        assert_eq!(rest.len(), 0);
303
304        let buf: &[u8] = &[0xd2, 0x7f, 0xff, 0xff, 0xff];
305        let (decoded, rest) = i32::decode(buf).unwrap();
306        let expect = i32::MAX;
307        assert_eq!(decoded, expect);
308        assert_eq!(rest.len(), 0);
309    }
310
311    #[test]
312    fn decode_i64() {
313        // Int16
314        let buf: &[u8] = &[0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
315        let (decoded, rest) = i64::decode(buf).unwrap();
316        let expect = i64::MIN;
317        assert_eq!(decoded, expect);
318        assert_eq!(rest.len(), 0);
319
320        let buf: &[u8] = &[0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
321        let (decoded, rest) = i64::decode(buf).unwrap();
322        let expect = i64::MAX;
323        assert_eq!(decoded, expect);
324        assert_eq!(rest.len(), 0);
325    }
326}