1use std::borrow::Cow;
2
3use const_varint::{decode_u32_varint, encode_u32_varint_to, encoded_u32_varint_len};
4
5use super::WireType;
6
7pub use tuple::TupleEncoder;
8
9#[cfg(any(feature = "std", feature = "alloc"))]
10mod bytes;
11#[cfg(any(feature = "std", feature = "alloc"))]
12mod nodecraft;
13mod primitives;
14#[cfg(any(feature = "std", feature = "alloc"))]
15mod string;
16
17mod tuple;
18
19pub trait DataRef<'a, D>
21where
22 D: Data + ?Sized,
23 Self: Copy + core::fmt::Debug + Send + Sync,
24{
25 fn decode(buf: &'a [u8]) -> Result<(usize, Self), DecodeError>
29 where
30 Self: Sized;
31
32 fn decode_length_delimited(src: &'a [u8]) -> Result<(usize, Self), DecodeError>
34 where
35 Self: Sized,
36 {
37 if D::WIRE_TYPE != WireType::LengthDelimited {
38 return Self::decode(src);
39 }
40
41 let (mut offset, len) = decode_u32_varint(src)?;
42 let len = len as usize;
43 if len + offset > src.len() {
44 return Err(DecodeError::buffer_underflow());
45 }
46
47 let src = &src[offset..offset + len];
48 let (bytes_read, value) = Self::decode(src)?;
49
50 #[cfg(debug_assertions)]
51 super::debug_assert_read_eq::<Self>(bytes_read, len);
52
53 offset += bytes_read;
54 Ok((offset, value))
55 }
56}
57
58pub trait Data: core::fmt::Debug + Send + Sync {
60 const WIRE_TYPE: WireType = WireType::LengthDelimited;
62
63 type Ref<'a>: DataRef<'a, Self>;
65
66 fn from_ref(val: Self::Ref<'_>) -> Result<Self, DecodeError>
68 where
69 Self: Sized;
70
71 fn encoded_len(&self) -> usize;
73
74 fn encoded_len_with_length_delimited(&self) -> usize {
76 let len = self.encoded_len();
77 match Self::WIRE_TYPE {
78 WireType::LengthDelimited => encoded_u32_varint_len(len as u32) + len,
79 _ => len,
80 }
81 }
82
83 fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>;
87
88 #[cfg(any(feature = "std", feature = "alloc"))]
90 fn encode_to_vec(&self) -> Result<std::vec::Vec<u8>, EncodeError> {
91 let len = self.encoded_len();
92 let mut vec = std::vec![0; len];
93 self.encode(&mut vec).map(|_| vec)
94 }
95
96 #[cfg(any(feature = "std", feature = "alloc"))]
98 fn encode_to_bytes(&self) -> Result<::bytes::Bytes, EncodeError> {
99 self.encode_to_vec().map(Into::into)
100 }
101
102 fn encode_length_delimited(&self, buf: &mut [u8]) -> Result<usize, EncodeError> {
106 if Self::WIRE_TYPE != WireType::LengthDelimited {
107 return self.encode(buf);
108 }
109
110 let len = self.encoded_len();
111 if len > u32::MAX as usize {
112 return Err(EncodeError::TooLarge);
113 }
114
115 let mut offset = 0;
116 offset += encode_u32_varint_to(len as u32, buf)?;
117 offset += self.encode(&mut buf[offset..])?;
118
119 #[cfg(debug_assertions)]
120 super::debug_assert_write_eq::<Self>(offset, self.encoded_len_with_length_delimited());
121
122 Ok(offset)
123 }
124
125 #[cfg(any(feature = "std", feature = "alloc"))]
127 fn encode_length_delimited_to_vec(&self) -> Result<std::vec::Vec<u8>, EncodeError> {
128 let len = self.encoded_len_with_length_delimited();
129 let mut vec = std::vec![0; len];
130 self.encode_length_delimited(&mut vec).map(|_| vec)
131 }
132
133 #[cfg(any(feature = "std", feature = "alloc"))]
135 fn encode_length_delimited_to_bytes(&self) -> Result<::bytes::Bytes, EncodeError> {
136 self.encode_length_delimited_to_vec().map(Into::into)
137 }
138
139 fn decode(src: &[u8]) -> Result<(usize, Self), DecodeError>
143 where
144 Self: Sized,
145 {
146 <Self::Ref<'_> as DataRef<Self>>::decode(src)
147 .and_then(|(bytes_read, value)| Self::from_ref(value).map(|val| (bytes_read, val)))
148 }
149
150 fn decode_length_delimited(buf: &[u8]) -> Result<(usize, Self), DecodeError>
152 where
153 Self: Sized,
154 {
155 <Self::Ref<'_> as DataRef<Self>>::decode_length_delimited(buf)
156 .and_then(|(bytes_read, value)| Self::from_ref(value).map(|val| (bytes_read, val)))
157 }
158}
159
160#[derive(Debug, thiserror::Error)]
162pub enum EncodeError {
163 #[error("insufficient buffer capacity, required: {required}, remaining: {remaining}")]
165 InsufficientBuffer {
166 required: usize,
168 remaining: usize,
170 },
171 #[error("encoded data is too large, the maximum allowed size is {MAX} bytes", MAX = u32::MAX)]
173 TooLarge,
174 #[error("{0}")]
176 Custom(Cow<'static, str>),
177}
178
179impl EncodeError {
180 #[inline]
182 pub const fn insufficient_buffer(required: usize, remaining: usize) -> Self {
183 Self::InsufficientBuffer {
184 required,
185 remaining,
186 }
187 }
188
189 pub fn custom<T>(value: T) -> Self
191 where
192 T: Into<Cow<'static, str>>,
193 {
194 Self::Custom(value.into())
195 }
196
197 pub fn update(mut self, required: usize, remaining: usize) -> Self {
199 match self {
200 Self::InsufficientBuffer {
201 required: ref mut r,
202 remaining: ref mut rem,
203 } => {
204 *r = required;
205 *rem = remaining;
206 self
207 }
208 _ => self,
209 }
210 }
211}
212
213impl From<const_varint::EncodeError> for EncodeError {
214 #[inline]
215 fn from(value: const_varint::EncodeError) -> Self {
216 match value {
217 const_varint::EncodeError::Underflow {
218 required,
219 remaining,
220 } => Self::InsufficientBuffer {
221 required,
222 remaining,
223 },
224 }
225 }
226}
227
228impl From<Cow<'static, str>> for EncodeError {
229 fn from(value: Cow<'static, str>) -> Self {
230 Self::Custom(value)
231 }
232}
233
234#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, derive_more::IsVariant)]
240pub enum DecodeError {
241 #[error("buffer underflow")]
243 BufferUnderflow,
244
245 #[error("missing {field} in {ty}")]
247 MissingField {
248 ty: &'static str,
250 field: &'static str,
252 },
253
254 #[error("duplicate field {field} with tag {tag} in {ty}")]
256 DuplicateField {
257 ty: &'static str,
259 field: &'static str,
261 tag: u8,
263 },
264
265 #[error("unknown wire type value {value} with tag {tag} when decoding {ty}")]
267 UnknownWireType {
268 ty: &'static str,
270 value: u8,
272 tag: u8,
274 },
275
276 #[error("unknown tag {tag} when decoding {ty}")]
278 UnknownTag {
279 ty: &'static str,
281 tag: u8,
283 },
284
285 #[error("length-delimited overflow the maximum value of u32")]
287 LengthDelimitedOverflow,
288
289 #[error("{0}")]
291 Custom(Cow<'static, str>),
292}
293
294impl From<const_varint::DecodeError> for DecodeError {
295 #[inline]
296 fn from(e: const_varint::DecodeError) -> Self {
297 match e {
298 const_varint::DecodeError::Underflow => Self::BufferUnderflow,
299 const_varint::DecodeError::Overflow => Self::LengthDelimitedOverflow,
300 }
301 }
302}
303
304impl DecodeError {
305 #[inline]
307 pub const fn buffer_underflow() -> Self {
308 Self::BufferUnderflow
309 }
310
311 #[inline]
313 pub const fn missing_field(ty: &'static str, field: &'static str) -> Self {
314 Self::MissingField { ty, field }
315 }
316
317 #[inline]
319 pub const fn duplicate_field(ty: &'static str, field: &'static str, tag: u8) -> Self {
320 Self::DuplicateField { ty, field, tag }
321 }
322
323 #[inline]
325 pub const fn unknown_wire_type(ty: &'static str, value: u8, tag: u8) -> Self {
326 Self::UnknownWireType { ty, value, tag }
327 }
328
329 #[inline]
331 pub const fn unknown_tag(ty: &'static str, tag: u8) -> Self {
332 Self::UnknownTag { ty, tag }
333 }
334
335 #[inline]
337 pub fn custom<T>(value: T) -> Self
338 where
339 T: Into<Cow<'static, str>>,
340 {
341 Self::Custom(value.into())
342 }
343}