testnumbat_codec/
top_de.rs1use alloc::boxed::Box;
2
3use crate::{
4 codec_err::DecodeError, nested_de::*, top_de_input::TopDecodeInput, NestedDecodeInput, TypeInfo,
5};
6
7pub trait TopDecode: Sized {
21 #[doc(hidden)]
22 const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
23
24 fn top_decode<I: TopDecodeInput>(input: I) -> Result<Self, DecodeError>;
26
27 #[inline]
31 fn top_decode_or_exit<I: TopDecodeInput, ExitCtx: Clone>(
32 input: I,
33 c: ExitCtx,
34 exit: fn(ExitCtx, DecodeError) -> !,
35 ) -> Self {
36 match Self::top_decode(input) {
37 Ok(v) => v,
38 Err(e) => exit(c, e),
39 }
40 }
41
42 #[doc(hidden)]
45 #[inline]
46 fn top_decode_boxed<I: TopDecodeInput>(input: I) -> Result<Box<Self>, DecodeError> {
47 Ok(Box::new(Self::top_decode(input)?))
48 }
49
50 #[doc(hidden)]
51 #[inline]
52 fn top_decode_boxed_or_exit<I: TopDecodeInput, ExitCtx: Clone>(
53 input: I,
54 c: ExitCtx,
55 exit: fn(ExitCtx, DecodeError) -> !,
56 ) -> Box<Self> {
57 Box::new(Self::top_decode_or_exit(input, c, exit))
58 }
59}
60
61pub fn top_decode_from_nested<T, I>(input: I) -> Result<T, DecodeError>
63where
64 I: TopDecodeInput,
65 T: NestedDecode,
66{
67 let mut nested_buffer = input.into_nested_buffer();
68 let result = T::dep_decode(&mut nested_buffer)?;
69 if !nested_buffer.is_depleted() {
70 return Err(DecodeError::INPUT_TOO_LONG);
71 }
72 Ok(result)
73}
74
75pub fn top_decode_from_nested_or_exit<T, I, ExitCtx: Clone>(
78 input: I,
79 c: ExitCtx,
80 exit: fn(ExitCtx, DecodeError) -> !,
81) -> T
82where
83 I: TopDecodeInput,
84 T: NestedDecode,
85{
86 let mut nested_buffer = input.into_nested_buffer();
87 let result = T::dep_decode_or_exit(&mut nested_buffer, c.clone(), exit);
88 if !nested_buffer.is_depleted() {
89 exit(c, DecodeError::INPUT_TOO_LONG);
90 }
91 result
92}