1pub mod flat;
3
4pub use minicbor;
6
7pub mod utils;
9
10pub trait Fragment: Sized + for<'b> minicbor::Decode<'b, ()> + minicbor::Encode<()> {}
11
12impl<T> Fragment for T where T: for<'b> minicbor::Decode<'b, ()> + minicbor::Encode<()> + Sized {}
13
14#[macro_export]
15macro_rules! codec_by_datatype {
16 (
17 $enum_name:ident $( < $lifetime:lifetime > )?,
18 $( $( $cbortype:ident )|* => $one_f:ident ),*,
19 ($( $( $vars:ident ),+ => $many_f:ident )?)
20 ) => {
21 impl<$( $lifetime, )? '__b $(:$lifetime)?, C> minicbor::decode::Decode<'__b, C> for $enum_name $(<$lifetime>)? {
22 fn decode(d: &mut minicbor::Decoder<'__b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
23 match d.datatype()? {
24 $( minicbor::data::Type::Array => {
25 d.array()?;
26 Ok($enum_name::$many_f($({ let $vars = d.decode_with(ctx)?; $vars }, )+ ))
28 }, )?
29 $( $( minicbor::data::Type::$cbortype )|* => Ok($enum_name::$one_f(d.decode_with(ctx)?)), )*
30 _ => Err(minicbor::decode::Error::message(
31 "Unknown cbor data type for this macro-defined enum.")
32 ),
33 }
34 }
35 }
36
37 impl< $( $lifetime, )? C> minicbor::encode::Encode<C> for $enum_name $(<$lifetime>)? {
38 fn encode<W: minicbor::encode::Write>(
39 &self,
40 e: &mut minicbor::Encoder<W>,
41 ctx: &mut C,
42 ) -> Result<(), minicbor::encode::Error<W::Error>> {
43 match self {
44 $( $enum_name::$many_f ($( $vars ),+) => {
45 e.array(2)?;
46 $( e.encode_with($vars, ctx)?; )+
47 }, )?
48 $( $enum_name::$one_f(__x666) => {
49 e.encode_with(__x666, ctx)?;
50 } )*
51 };
52
53 Ok(())
54 }
55 }
56 }
57}