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
//! Deterministic CBOR under the profile in Section 4.1 of the specification.
//!
//! The profile is narrower than [RFC 8949] canonical encoding: floating
//! point is excluded entirely, map keys must be text, and text must be in
//! Unicode Normalization Form C. Off-the-shelf decoders are permissive by
//! design, which is the wrong disposition here — Section 4.1 requires that
//! non-canonical bytes be *rejected* rather than repaired, because an
//! object's identity is the hash of its bytes and a decoder that silently
//! normalizes would let two encodings claim one identity.
//!
//! [RFC 8949]: https://www.rfc-editor.org/rfc/rfc8949
//!
//! # Examples
//!
//! ```
//! use publet_core::cbor::{decode, encode, Value};
//!
//! let bytes = encode(&Value::Text("hello".into()));
//! assert_eq!(decode(&bytes)?, Value::Text("hello".into()));
//! # Ok::<(), publet_core::CanonError>(())
//! ```
//!
//! Non-canonical input is refused, and the error names the rule:
//!
//! ```
//! use publet_core::cbor::decode;
//!
//! // 0x18 0x05 encodes 5 in two bytes; canonical form is the single byte 0x05.
//! let err = decode(&[0x18, 0x05]).unwrap_err();
//! assert_eq!(err.rule(), "shortest-form integers");
//! ```
pub use ;
pub use encode;
use BTreeMap;
/// A value in the deterministic CBOR profile.
///
/// There is deliberately no float variant and no tag variant: the profile
/// admits neither, so they are unrepresentable rather than rejected later.
/// Maps are [`BTreeMap`] so that encoding is sorted by construction.