messagepack_core/decode/
mod.rs1use crate::Format;
7
8mod array;
9pub use array::ArrayDecoder;
10mod bin;
11pub use bin::BinDecoder;
12mod bool;
13mod float;
14mod int;
15mod map;
16pub use map::MapDecoder;
17mod nil;
18pub use nil::NilDecoder;
19mod str;
20pub use str::StrDecoder;
21mod timestamp;
22
23#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
25pub enum Error {
26 InvalidData,
28 UnexpectedFormat,
30 EofFormat,
32 EofData,
34}
35
36impl core::fmt::Display for Error {
37 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38 match self {
39 Error::InvalidData => write!(f, "Cannot decode invalid data"),
40 Error::UnexpectedFormat => write!(f, "Unexpected format found"),
41 Error::EofFormat => write!(f, "EOF while parse format"),
42 Error::EofData => write!(f, "EOF while parse data"),
43 }
44 }
45}
46
47impl core::error::Error for Error {}
48
49type Result<T> = ::core::result::Result<T, Error>;
51
52pub trait Decode<'a> {
54 type Value: Sized;
56 fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])>;
59
60 fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])>;
64}
65
66impl<'a> Decode<'a> for Format {
67 type Value = Self;
68 fn decode(buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
69 let (first, rest) = buf.split_first().ok_or(Error::EofFormat)?;
70
71 Ok((Self::from_byte(*first), rest))
72 }
73
74 fn decode_with_format(format: Format, buf: &'a [u8]) -> Result<(Self::Value, &'a [u8])> {
75 let _ = (format, buf);
76 unreachable!()
77 }
78}
79
80pub struct NbyteReader<const NBYTE: usize>;
82
83macro_rules! impl_read {
84 ($ty:ty) => {
85 pub fn read(buf: &[u8]) -> Result<(usize, &[u8])> {
88 const SIZE: usize = core::mem::size_of::<$ty>();
89 let (data, rest) = buf.split_at_checked(SIZE).ok_or(Error::EofData)?;
90 let data: [u8; SIZE] = data.try_into().map_err(|_| Error::EofData)?;
91 let val =
92 usize::try_from(<$ty>::from_be_bytes(data)).map_err(|_| Error::InvalidData)?;
93 Ok((val, rest))
94 }
95 };
96}
97
98impl NbyteReader<1> {
99 impl_read! {u8}
100}
101
102impl NbyteReader<2> {
103 impl_read! {u16}
104}
105impl NbyteReader<4> {
106 impl_read! {u32}
107}