1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Traits and functions through which this crate's encode and decode wrappers are implemented.
//!
//! While the traits are not sealed, there is little point in implementing them: If you'd consider
//! implementing them, just implement the equivalent trait from `heapless`.
//!
//! The traits are mainly public to give access to the list of foreign types on which they are
//! implemented.
//!
//! (There is *one* exception: If a variant of `minicbor-derive` were written that assigns this
//! crate as the default handler for `with`, then this crate can be extended to also cover all
//! `std`/`core` types, but third party types that would previously have implemented minicbor
//! traits will need to implement the traits here).
//!
//! # Usage example
//!
//! ```
//! use minicbor::*;
//! use cbor_macro::cbor;
//! use cboritem::CborItem;
//! #[derive(Decode, CborLen)]
//! struct IncompletelyDecoded<'a> {
//! #[n(0)]
//! x: u32,
//! #[cbor(n(1), with = "minicbor_adapters")]
//! y: heapless::String<4>,
//! #[cbor(b(2), with = "minicbor_adapters")]
//! z: CborItem<'a>,
//! };
//! let encoded = cbor!([23, "six", {"key": [1, 2, 3], "key2": "value2"}]);
//! let decoded: IncompletelyDecoded = minicbor::decode(&encoded).unwrap();
//! assert_eq!(decoded.x, 23);
//! assert_eq!(decoded.z, cbor!({"key": [1, 2, 3], "key2": "value2"}));
//! // This is particularly speed-up-py for complexly valued CborItems
//! assert_eq!(decoded.cbor_len(&mut ()), encoded.len());
//! ```
/// Function that can be used with minicbor's `cbor(decode_with=…)` or `cbor(with=…)` derive macro
/// argument.
///
/// Use like this:
///
/// ```
/// use minicbor::Decode;
///
/// #[derive(Decode, Debug)]
/// struct MyItem {
/// #[cbor(n(0), decode_with = "minicbor_adapters::decode")]
/// text: heapless::String<4>,
/// #[cbor(n(1), with = "minicbor_adapters")]
/// items: heapless::Vec<bool, 4>,
/// }
///
/// use cbor_macro::cbor;
///
/// let working = cbor!(["Hi", [false, true]]);
/// let parsed: MyItem = minicbor::decode(&working).unwrap();
/// assert_eq!(parsed.text, "Hi");
/// assert_eq!(parsed.items, [false, true]);
///
/// let too_long = cbor!(["Hello" / 5 bytes is more than 4 bytes /, []]);
/// let erred: Result<MyItem, _> = minicbor::decode(&too_long);
/// erred.unwrap_err();
/// ```
/// Function that can be used with minicbor's `cbor(encode_with=…)` or `cbor(with=…)` derive macro
/// argument.
///
/// Use like this:
///
/// ```
/// use minicbor::Encode;
///
/// #[derive(Encode, Debug)]
/// struct MyItem {
/// #[cbor(n(0), encode_with = "minicbor_adapters::encode")]
/// text: heapless::String<4>,
/// #[cbor(n(1), with = "minicbor_adapters")]
/// items: heapless::Vec<bool, 4>,
/// }
///
/// let item = MyItem {
/// text: heapless::String::try_from("Hi").unwrap(),
/// items: heapless::Vec::try_from([true, false].as_slice()).unwrap(),
/// };
/// let mut buffer = [0u8; 128];
/// minicbor::encode(item, buffer.as_mut()).unwrap();
/// use cbor_macro::cbor;
/// let reference = cbor!(["Hi", [true, false]]);
/// assert_eq!(&buffer[..reference.len()], *reference);
/// ```
/// This function enables deriving [`minicbor::CborLen`] in addition to [`minicbor::Encode`].
///
/// Use like this:
///
/// ```
/// use minicbor::{Encode, CborLen};
///
/// #[derive(Encode, CborLen, Debug)]
/// struct MyItem {
/// #[cbor(n(0), with = "minicbor_adapters")]
/// text: heapless::String<4>,
/// #[cbor(n(1), with = "minicbor_adapters")]
/// items: heapless::Vec<bool, 4>,
/// }
///
/// let item = MyItem {
/// text: heapless::String::try_from("Hi").unwrap(),
/// items: heapless::Vec::try_from([true, false].as_slice()).unwrap(),
/// };
/// let length = CborLen::cbor_len(&item, &mut ());
/// use cbor_macro::cbor;
/// let reference = cbor!(["Hi", [true, false]]);
/// assert_eq!(length, reference.len());
/// ```
/// Proxy trait for [`minicbor::Decode`]; see module level documentation.
///
/// This type is documented to give access to the list of types on which it is implemented.
/// Proxy trait for [`minicbor::Encode`]; see module level documentation.
///
/// This type is documented to give access to the list of types on which it is implemented.
/// Proxy trait for [`minicbor::CborLen`]; see module level documentation.
///
/// This type is documented to give access to the list of types on which it is implemented.