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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import_stdlib!;
/// Internal representation of a tag name, with optimization for statically
/// known names.
/// Represents the numeric value of a CBOR tag.
///
/// In CBOR, tags (major type 6) are identified by unsigned integer values.
/// Per RFC 8949, tag values are registered with IANA to ensure
/// interoperability.
///
/// # Examples of standard CBOR tag values
///
/// - 0: RFC3339 date/time string
/// - 1: Epoch-based date/time
/// - 2: Positive bignum
/// - 3: Negative bignum
/// - 4: Decimal fraction
/// - 5: Bigfloat
/// - 18: CBOR data item (tagged data is embedded CBOR)
/// - 32: URI
/// - 201: dCBOR tag (per the dCBOR specification)
///
/// For the full registry of tag values, see the IANA CBOR Tags registry:
/// <https://www.iana.org/assignments/cbor-tags/>
pub type TagValue = u64;
/// Represents a CBOR tag (major type 6) with optional associated name.
///
/// Tags in CBOR provide semantic information about the tagged data item.
/// They are used to indicate that the data item has some additional semantics
/// beyond its basic CBOR type. For example, a tag might indicate that a string
/// should be interpreted as a date, or that a byte string contains embedded
/// CBOR.
///
/// This implementation supports both the numeric tag value and an optional
/// human-readable name to improve code clarity and debugging.
///
/// # Tag equality and comparison
///
/// Tags are considered equal if their numeric values are equal, regardless of
/// their names. This matches the CBOR specification behavior where the tag
/// value (not the name) determines the semantic meaning.
///
/// # Deterministic encoding
///
/// Tags, like all other CBOR types, must follow deterministic encoding rules in
/// dCBOR. The tag value is encoded according to the general integer encoding
/// rules (shortest form possible), and the tagged content itself must also
/// follow deterministic encoding rules.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// // Create a tag with a name
/// let epoch_time_tag = Tag::new(1, "epoch-time");
/// assert_eq!(epoch_time_tag.value(), 1);
/// assert_eq!(epoch_time_tag.name(), Some("epoch-time".to_string()));
///
/// // Create a tag without a name
/// let unnamed_tag = Tag::with_value(42);
/// assert_eq!(unnamed_tag.value(), 42);
/// assert_eq!(unnamed_tag.name(), None);
///
/// // Create a tag at compile time with a static name
/// const REGISTERED_TAG: Tag = Tag::with_static_name(32, "uri");
/// assert_eq!(REGISTERED_TAG.value(), 32);
/// assert_eq!(REGISTERED_TAG.name(), Some("uri".to_string()));
///
/// // Tags with the same value are equal, even with different names
/// let tag1 = Tag::new(42, "meaning");
/// let tag2 = Tag::with_value(42);
/// assert_eq!(tag1, tag2);
/// ```
/// Compares tags for equality based on their numeric values.
///
/// Tags are considered equal if they have the same numeric value, regardless of
/// whether they have different names or if one has a name and the other
/// doesn't. This matches the CBOR standard behavior where the tag value, not
/// its human-readable name, determines its semantic meaning.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// let tag1 = Tag::new(32, "uri");
/// let tag2 = Tag::with_value(32);
/// let tag3 = Tag::new(32, "different-name");
/// let tag4 = Tag::with_value(42);
///
/// assert_eq!(tag1, tag2); // Same value, one named and one unnamed
/// assert_eq!(tag1, tag3); // Same value, different names
/// assert_ne!(tag1, tag4); // Different values
/// ```
/// Confirms `Tag` implements full equality, not just partial equality.
///
/// This is required for `Tag` to be used in collections like `HashSet` and
/// as keys in `HashMap` along with the `Hash` implementation.
/// Implements hashing for `Tag` based solely on the numeric tag value.
///
/// This implementation ensures that two tags with the same value but different
/// names will hash to the same value, which is consistent with the equality
/// implementation. This allows tags to be used as keys in hash-based
/// collections.
/// Formats a tag for display, preferring the name if available.
///
/// If the tag has a name, the name will be displayed. Otherwise, the numeric
/// value will be displayed.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// let named_tag = Tag::new(32, "uri");
/// let unnamed_tag = Tag::with_value(42);
///
/// assert_eq!(named_tag.to_string(), "uri");
/// assert_eq!(unnamed_tag.to_string(), "42");
/// ```
/// Converts a raw tag value to a `Tag` instance.
///
/// This provides a convenient way to create unnamed tags directly from numeric
/// values.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// // These are equivalent:
/// let tag1: Tag = 42.into();
/// let tag2 = Tag::with_value(42);
///
/// assert_eq!(tag1, tag2);
/// ```
/// Converts an `i32` integer to a `Tag` instance.
///
/// This provides a convenient way to create unnamed tags from 32-bit signed
/// integers. Note that the value will be converted to an unsigned 64-bit
/// integer internally.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// let tag: Tag = 42i32.into();
/// assert_eq!(tag.value(), 42);
/// ```
/// Converts a `usize` to a `Tag` instance.
///
/// This provides a convenient way to create unnamed tags from platform-specific
/// sized unsigned integers. Note that on platforms where `usize` is larger than
/// 64 bits, values that don't fit in 64 bits will be truncated.
///
/// # Examples
///
/// ```
/// use dcbor::prelude::*;
///
/// let tag: Tag = 42usize.into();
/// assert_eq!(tag.value(), 42);
/// ```
/// Converts a reference to a `Tag` into an owned `Tag` instance.