rseip_core/codec/
decode.rs

1// rseip
2//
3// rseip - Ethernet/IP (CIP) in pure Rust.
4// Copyright: 2021, Joylei <leingliu@gmail.com>
5// License: MIT
6
7mod impls;
8mod little_endian;
9pub mod visitor;
10
11use crate::Error;
12use bytes::Buf;
13pub use little_endian::LittleEndianDecoder;
14pub use visitor::Visitor;
15
16pub trait Decoder<'de> {
17    type Error: Error;
18    type Buf: Buf;
19    /// inner buffer
20    fn buf(&self) -> &Self::Buf;
21
22    /// inner buffer
23    fn buf_mut(&mut self) -> &mut Self::Buf;
24
25    /// check remaining buffer size
26    #[inline(always)]
27    fn ensure_size(&self, expected: usize) -> Result<(), Self::Error> {
28        let actual_len = self.buf().remaining();
29        if actual_len < expected {
30            Err(Self::Error::invalid_length(actual_len, expected))
31        } else {
32            Ok(())
33        }
34    }
35
36    /// decode any type `T` that derive [`Decode`]
37    #[inline]
38    fn decode_any<T>(&mut self) -> Result<T, Self::Error>
39    where
40        T: Decode<'de>,
41        Self: Sized,
42    {
43        T::decode(self)
44    }
45
46    /// get bool unchecked
47    #[inline(always)]
48    fn decode_bool(&mut self) -> bool {
49        self.buf_mut().get_u8() != 0
50    }
51
52    /// get i8 unchecked
53    #[inline(always)]
54    fn decode_i8(&mut self) -> i8 {
55        self.buf_mut().get_i8()
56    }
57
58    /// get u8 unchecked
59    #[inline(always)]
60    fn decode_u8(&mut self) -> u8 {
61        self.buf_mut().get_u8()
62    }
63
64    /// get i16 unchecked
65    #[inline(always)]
66    fn decode_i16(&mut self) -> i16 {
67        self.buf_mut().get_i16_le()
68    }
69
70    /// get u16 unchecked
71    #[inline(always)]
72    fn decode_u16(&mut self) -> u16 {
73        self.buf_mut().get_u16_le()
74    }
75
76    /// get i32 unchecked
77    #[inline(always)]
78    fn decode_i32(&mut self) -> i32 {
79        self.buf_mut().get_i32_le()
80    }
81
82    /// get u32 unchecked
83    #[inline(always)]
84    fn decode_u32(&mut self) -> u32 {
85        self.buf_mut().get_u32_le()
86    }
87
88    /// get i64 unchecked
89    #[inline(always)]
90    fn decode_i64(&mut self) -> i64 {
91        self.buf_mut().get_i64_le()
92    }
93
94    /// get u64 unchecked
95    #[inline(always)]
96    fn decode_u64(&mut self) -> u64 {
97        self.buf_mut().get_u64_le()
98    }
99
100    /// get f32 unchecked
101    #[inline(always)]
102    fn decode_f32(&mut self) -> f32 {
103        self.buf_mut().get_f32_le()
104    }
105
106    /// get f64 unchecked
107    #[inline(always)]
108    fn decode_f64(&mut self) -> f64 {
109        self.buf_mut().get_f64_le()
110    }
111
112    /// get i128 unchecked
113    #[inline(always)]
114    fn decode_i128(&mut self) -> i128 {
115        self.buf_mut().get_i128_le()
116    }
117
118    /// get u128 unchecked
119    #[inline(always)]
120    fn decode_u128(&mut self) -> u128 {
121        self.buf_mut().get_u128_le()
122    }
123
124    #[inline(always)]
125    fn remaining(&mut self) -> usize {
126        self.buf().remaining()
127    }
128
129    #[inline(always)]
130    fn has_remaining(&mut self) -> bool {
131        self.buf().has_remaining()
132    }
133
134    /// decode with a dedicated [`Visitor`]. A [`Visitor`] gives you some context information while decoding.
135    #[inline]
136    fn decode_with<V: Visitor<'de>>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
137    where
138        Self: Sized,
139    {
140        visitor.visit(self)
141    }
142
143    /// take specified number of bytes, and decode it with the specified visitor
144    fn decode_sized<V: Visitor<'de>>(
145        &mut self,
146        size: usize,
147        visitor: V,
148    ) -> Result<V::Value, Self::Error>
149    where
150        Self: Sized;
151}
152
153pub trait Decode<'de>: Sized {
154    fn decode<D>(decoder: D) -> Result<Self, D::Error>
155    where
156        D: Decoder<'de>;
157
158    #[doc(hidden)]
159    #[inline]
160    fn decode_in_place<D>(decoder: D, place: &mut Self) -> Result<(), D::Error>
161    where
162        D: Decoder<'de>,
163    {
164        *place = Self::decode(decoder)?;
165        Ok(())
166    }
167}