veriform/decoder/traits.rs
1//! Message decoding traits.
2//!
3//! These traits infer the wire type to decode using the type system.
4//!
5//! They're intened to be impl'd by `veriform::decoder::Decoder`.
6
7use super::sequence;
8use crate::{field::Tag, Error};
9use digest::Digest;
10
11/// Try to decode a field to a value of the given type.
12///
13/// This trait is intended to be impl'd by the `Decoder` type.
14pub trait Decode<T> {
15 /// Try to decode a value of type `T`
16 fn decode(&mut self, tag: Tag, input: &mut &[u8]) -> Result<T, Error>;
17}
18
19/// Try to decode a field to a reference of the given type.
20///
21/// This trait is intended to be impl'd by the `Decoder` type.
22pub trait DecodeRef<T: ?Sized> {
23 /// Try to decode a reference to type `T`
24 fn decode_ref<'a>(&mut self, tag: Tag, input: &mut &'a [u8]) -> Result<&'a T, Error>;
25}
26
27/// Decode a sequence of values to a [`sequence::Iter`].
28///
29/// This trait is intended to be impl'd by the `Decoder` type.
30pub trait DecodeSeq<T, D>
31where
32 D: Digest,
33{
34 /// Try to decode a sequence of values of type `T`
35 fn decode_seq<'a, 'b>(
36 &'a mut self,
37 tag: Tag,
38 input: &mut &'b [u8],
39 ) -> Result<sequence::Iter<'a, 'b, T, D>, Error>;
40}