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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import_stdlib!;
use crateCBOR;
/// # CBOR Encoding and Decoding Traits
///
/// These traits provide functionality for converting between Rust types and
/// CBOR data. They form the foundation of the dCBOR serialization
/// infrastructure.
///
/// The main traits are:
///
/// - `CBOREncodable`: For types that can be encoded to CBOR
/// - `CBORDecodable`: For types that can be decoded from CBOR
/// - `CBORCodable`: For types that can do both (a combination of the above)
///
/// These traits allow for ergonomic conversions using Rust's type system and
/// enable seamless integration with dCBOR's deterministic encoding rules.
/// A trait for types that can be encoded to CBOR.
///
/// This trait is automatically implemented for any type that implements
/// `Into<CBOR>` and `Clone`. It provides convenient methods for converting
/// instances into CBOR objects and binary data.
///
/// ## Example
///
/// ```
/// use dcbor::prelude::*;
///
/// // Custom type that implements Into<CBOR>
/// #[derive(Clone)]
/// struct Person {
/// name: String,
/// age: u8,
/// }
///
/// // Implement conversion to CBOR
/// impl From<Person> for CBOR {
/// fn from(person: Person) -> Self {
/// let mut map = Map::new();
/// map.insert("name", person.name);
/// map.insert("age", person.age);
/// map.into()
/// }
/// }
///
/// // The CBOREncodable trait is automatically implemented
/// let person = Person { name: "Alice".to_string(), age: 30 };
///
/// // Convert to CBOR with to_cbor()
/// let cbor = person.to_cbor();
///
/// // Convert directly to binary CBOR data
/// let data = person.to_cbor_data();
/// ```
/// A trait for types that can be decoded from CBOR.
///
/// This trait is automatically implemented for any type that implements
/// `TryFrom<CBOR>`. It serves as a marker trait to indicate that a type
/// supports being created from CBOR data.
///
/// ## Example
///
/// ```no_run
/// use dcbor::prelude::*;
///
/// // Custom type that implements TryFrom<CBOR>
/// struct Person {
/// name: String,
/// age: u8,
/// }
///
/// // Implement conversion from CBOR
/// impl TryFrom<CBOR> for Person {
/// type Error = dcbor::Error;
///
/// fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
/// if let CBORCase::Map(map) = cbor.into_case() {
/// let name: String = map.extract("name")?;
/// let age: u8 = map.extract("age")?;
/// Ok(Person { name, age })
/// } else {
/// Err("Expected a CBOR map".into())
/// }
/// }
/// }
///
/// // The CBORDecodable trait is automatically implemented
/// // Convert a CBOR object to our type
///
/// // Create a sample CBOR map
/// let mut map = Map::new();
/// map.insert("name", "Alice");
/// map.insert("age", 42);
/// let cbor = map.to_cbor();
///
/// // Parse from CBOR to our type
/// let person: Person = cbor.try_into().unwrap();
/// ```
/// A trait for types that can be both encoded to and decoded from CBOR.
///
/// This trait is automatically implemented for any type that implements both
/// `CBOREncodable` and `CBORDecodable`. It serves as a convenience marker trait
/// to indicate full CBOR serialization support.
///
/// ## Example
///
/// ```
/// use dcbor::prelude::*;
///
/// // Custom type that implements both conversion directions
/// #[derive(Clone)]
/// struct Person {
/// name: String,
/// age: u8,
/// }
///
/// // Implement conversion to CBOR
/// impl From<Person> for CBOR {
/// fn from(person: Person) -> Self {
/// let mut map = Map::new();
/// map.insert("name", person.name);
/// map.insert("age", person.age);
/// map.into()
/// }
/// }
///
/// // Implement conversion from CBOR
/// impl TryFrom<CBOR> for Person {
/// type Error = dcbor::Error;
///
/// fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
/// if let CBORCase::Map(map) = cbor.into_case() {
/// let name: String = map.extract("name")?;
/// let age: u8 = map.extract("age")?;
/// Ok(Person { name, age })
/// } else {
/// Err("Expected a CBOR map".into())
/// }
/// }
/// }
///
/// // Person now automatically implements CBORCodable
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let cbor = person.to_cbor(); // Using CBOREncodable
///
/// // Create a round-trip copy
/// let person_copy: Person = cbor.try_into().unwrap(); // Using CBORDecodable
/// ```