1mod dec;
13mod ext;
14mod sint;
15mod str;
16mod uint;
17
18#[cfg(feature = "std")]
19mod est;
20#[cfg(feature = "std")]
21pub use est::{MessageLen, LenError};
22
23pub use self::dec::{read_f32, read_f64};
24pub use self::ext::{
25 read_ext_meta, read_fixext1, read_fixext16, read_fixext2, read_fixext4, read_fixext8, ExtMeta,
26};
27pub use self::sint::{read_i16, read_i32, read_i64, read_i8, read_nfix};
28#[allow(deprecated)]
29pub use self::str::{read_str, read_str_from_slice, read_str_len, read_str_ref, DecodeStringError};
31pub use self::uint::{read_pfix, read_u16, read_u32, read_u64, read_u8};
32
33use core::fmt::{self, Debug, Display, Formatter};
34#[cfg(feature = "std")]
35use std::error;
36
37use num_traits::cast::FromPrimitive;
38
39use crate::Marker;
40
41pub mod bytes;
42pub use bytes::Bytes;
43
44#[doc(inline)]
45#[allow(deprecated)]
46pub use crate::errors::Error;
47
48pub trait RmpReadErr: Display + Debug + crate::errors::MaybeErrBound + 'static {}
52#[cfg(feature = "std")]
53impl RmpReadErr for std::io::Error {}
54impl RmpReadErr for core::convert::Infallible {}
55
56macro_rules! read_byteorder_utils {
57 ($($name:ident => $tp:ident),* $(,)?) => {
58 $(
59 #[inline]
60 #[doc(hidden)]
61 fn $name(&mut self) -> Result<$tp, ValueReadError<Self::Error>> where Self: Sized {
62 const SIZE: usize = core::mem::size_of::<$tp>();
63 let mut buf: [u8; SIZE] = [0u8; SIZE];
64 self.read_exact_buf(&mut buf).map_err(ValueReadError::InvalidDataRead)?;
65 Ok($tp::from_be_bytes(buf))
66 }
67 )*
68 };
69}
70mod sealed {
71 pub trait Sealed {}
72 #[cfg(feature = "std")]
73 impl<T: ?Sized + std::io::Read> Sealed for T {}
74 #[cfg(not(feature = "std"))]
75 impl<'a> Sealed for &'a [u8] {}
76 impl Sealed for super::Bytes<'_> {}
77}
78
79pub trait RmpRead: sealed::Sealed {
86 type Error: RmpReadErr;
87 #[inline]
89 fn read_u8(&mut self) -> Result<u8, Self::Error> {
90 let mut buf = [0; 1];
91 self.read_exact_buf(&mut buf)?;
92 Ok(buf[0])
93 }
94
95 fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>;
101
102 #[inline]
106 #[doc(hidden)]
107 fn read_data_u8(&mut self) -> Result<u8, ValueReadError<Self::Error>> {
108 self.read_u8().map_err(ValueReadError::InvalidDataRead)
109 }
110 #[inline]
112 #[doc(hidden)]
113 fn read_data_i8(&mut self) -> Result<i8, ValueReadError<Self::Error>> {
114 self.read_data_u8().map(|b| b as i8)
115 }
116
117 read_byteorder_utils!(
118 read_data_u16 => u16,
119 read_data_u32 => u32,
120 read_data_u64 => u64,
121 read_data_i16 => i16,
122 read_data_i32 => i32,
123 read_data_i64 => i64,
124 read_data_f32 => f32,
125 read_data_f64 => f64
126 );
127}
128
129#[cfg(feature = "std")]
130impl<T: std::io::Read> RmpRead for T {
131 type Error = std::io::Error;
132
133 #[inline]
134 fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
135 std::io::Read::read_exact(self, buf)
136 }
137}
138
139#[derive(Debug)]
141#[allow(deprecated)] pub struct MarkerReadError<E: RmpReadErr = Error>(pub E);
143
144#[derive(Debug)]
146#[allow(deprecated)] pub enum ValueReadError<E: RmpReadErr = Error> {
148 InvalidMarkerRead(E),
150 InvalidDataRead(E),
152 TypeMismatch(Marker),
154}
155
156#[cfg(feature = "std")]
157impl error::Error for ValueReadError {
158 #[cold]
159 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
160 match *self {
161 Self::InvalidMarkerRead(ref err) |
162 Self::InvalidDataRead(ref err) => Some(err),
163 Self::TypeMismatch(..) => None,
164 }
165 }
166}
167
168impl Display for ValueReadError {
169 #[cold]
170 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
171 f.write_str(match *self {
173 Self::InvalidMarkerRead(..) => "failed to read MessagePack marker",
174 Self::InvalidDataRead(..) => "failed to read MessagePack data",
175 Self::TypeMismatch(..) => "the type decoded isn't match with the expected one",
176 })
177 }
178}
179
180impl<E: RmpReadErr> From<MarkerReadError<E>> for ValueReadError<E> {
181 #[cold]
182 fn from(err: MarkerReadError<E>) -> Self {
183 match err {
184 MarkerReadError(err) => Self::InvalidMarkerRead(err),
185 }
186 }
187}
188
189impl<E: RmpReadErr> From<E> for MarkerReadError<E> {
190 #[cold]
191 fn from(err: E) -> Self {
192 Self(err)
193 }
194}
195
196#[inline]
198pub fn read_marker<R: RmpRead>(rd: &mut R) -> Result<Marker, MarkerReadError<R::Error>> {
199 Ok(Marker::from_u8(rd.read_u8()?))
200}
201
202pub fn read_nil<R: RmpRead>(rd: &mut R) -> Result<(), ValueReadError<R::Error>> {
219 match read_marker(rd)? {
220 Marker::Null => Ok(()),
221 marker => Err(ValueReadError::TypeMismatch(marker)),
222 }
223}
224
225pub fn read_bool<R: RmpRead>(rd: &mut R) -> Result<bool, ValueReadError<R::Error>> {
243 match read_marker(rd)? {
244 Marker::True => Ok(true),
245 Marker::False => Ok(false),
246 marker => Err(ValueReadError::TypeMismatch(marker)),
247 }
248}
249
250#[derive(Debug)]
252#[allow(deprecated)] pub enum NumValueReadError<E: RmpReadErr = Error> {
254 InvalidMarkerRead(E),
256 InvalidDataRead(E),
258 TypeMismatch(Marker),
260 OutOfRange,
262}
263
264#[cfg(feature = "std")]
265impl error::Error for NumValueReadError {
266 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
267 match *self {
268 Self::InvalidMarkerRead(ref err) |
269 Self::InvalidDataRead(ref err) => Some(err),
270 Self::TypeMismatch(..) |
271 Self::OutOfRange => None,
272 }
273 }
274}
275
276impl<E: RmpReadErr> Display for NumValueReadError<E> {
277 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
278 f.write_str(match *self {
279 Self::InvalidMarkerRead(..) => "failed to read MessagePack marker",
280 Self::InvalidDataRead(..) => "failed to read MessagePack data",
281 Self::TypeMismatch(..) => "the type decoded isn't match with the expected one",
282 Self::OutOfRange => "out of range integral type conversion attempted",
283 })
284 }
285}
286
287impl<E: RmpReadErr> From<MarkerReadError<E>> for NumValueReadError<E> {
288 #[cold]
289 fn from(err: MarkerReadError<E>) -> Self {
290 match err {
291 MarkerReadError(err) => Self::InvalidMarkerRead(err),
292 }
293 }
294}
295
296impl<E: RmpReadErr> From<ValueReadError<E>> for NumValueReadError<E> {
297 #[cold]
298 fn from(err: ValueReadError<E>) -> Self {
299 match err {
300 ValueReadError::InvalidMarkerRead(err) => Self::InvalidMarkerRead(err),
301 ValueReadError::InvalidDataRead(err) => Self::InvalidDataRead(err),
302 ValueReadError::TypeMismatch(err) => Self::TypeMismatch(err),
303 }
304 }
305}
306
307pub fn read_int<T: FromPrimitive, R: RmpRead>(rd: &mut R) -> Result<T, NumValueReadError<R::Error>> {
338 let val = match read_marker(rd)? {
339 Marker::FixPos(val) => T::from_u8(val),
340 Marker::FixNeg(val) => T::from_i8(val),
341 Marker::U8 => T::from_u8(rd.read_data_u8()?),
342 Marker::U16 => T::from_u16(rd.read_data_u16()?),
343 Marker::U32 => T::from_u32(rd.read_data_u32()?),
344 Marker::U64 => T::from_u64(rd.read_data_u64()?),
345 Marker::I8 => T::from_i8(rd.read_data_i8()?),
346 Marker::I16 => T::from_i16(rd.read_data_i16()?),
347 Marker::I32 => T::from_i32(rd.read_data_i32()?),
348 Marker::I64 => T::from_i64(rd.read_data_i64()?),
349 marker => return Err(NumValueReadError::TypeMismatch(marker)),
350 };
351
352 val.ok_or(NumValueReadError::OutOfRange)
353}
354
355pub fn read_array_len<R>(rd: &mut R) -> Result<u32, ValueReadError<R::Error>>
368where
369 R: RmpRead,
370{
371 match read_marker(rd)? {
372 Marker::FixArray(size) => Ok(u32::from(size)),
373 Marker::Array16 => Ok(u32::from(rd.read_data_u16()?)),
374 Marker::Array32 => Ok(rd.read_data_u32()?),
375 marker => Err(ValueReadError::TypeMismatch(marker)),
376 }
377}
378
379pub fn read_map_len<R: RmpRead>(rd: &mut R) -> Result<u32, ValueReadError<R::Error>> {
391 let marker = read_marker(rd)?;
392 marker_to_len(rd, marker)
393}
394
395pub fn marker_to_len<R: RmpRead>(rd: &mut R, marker: Marker) -> Result<u32, ValueReadError<R::Error>> {
396 match marker {
397 Marker::FixMap(size) => Ok(u32::from(size)),
398 Marker::Map16 => Ok(u32::from(rd.read_data_u16()?)),
399 Marker::Map32 => Ok(rd.read_data_u32()?),
400 marker => Err(ValueReadError::TypeMismatch(marker)),
401 }
402}
403
404pub fn read_bin_len<R: RmpRead>(rd: &mut R) -> Result<u32, ValueReadError<R::Error>> {
412 match read_marker(rd)? {
413 Marker::Bin8 => Ok(u32::from(rd.read_data_u8()?)),
414 Marker::Bin16 => Ok(u32::from(rd.read_data_u16()?)),
415 Marker::Bin32 => Ok(rd.read_data_u32()?),
416 marker => Err(ValueReadError::TypeMismatch(marker)),
417 }
418}