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
//! Per-decode policy for accepting non-deterministic CBOR encodings.
//!
//! See [`Strictness`].
/// Policy for accepting non-deterministic CBOR encodings during a decode.
///
/// CBOR::Core requires every value to be encoded in a single canonical
/// form. The default decoder enforces that and rejects any deviation
/// with [`Error::NonDeterministic`](crate::Error::NonDeterministic).
/// Some producers (legacy encoders, bridges from other formats, hand
/// written test vectors) emit valid CBOR that violates one or more of
/// these rules. `Strictness` selects which violations the decoder
/// tolerates so that such input can still be read.
///
/// Each tolerated violation is normalized while decoding: the resulting
/// [`Value`](crate::Value) is the same value the canonical encoder
/// would produce, and re-encoding it always yields a CBOR::Core
/// compliant byte sequence. The original wire bytes are not preserved.
///
/// The default, [`Strictness::STRICT`], matches the CBOR::Core draft
/// exactly. [`Strictness::LENIENT`] accepts every supported deviation.
/// Set individual fields for a custom mix.
///
/// # Examples
///
/// ```
/// use cbor_core::{DecodeOptions, Strictness, Value};
///
/// // 255 wrongly encoded with a two byte argument (canonical: 0x18 0xff).
/// let bytes = [0x19, 0x00, 0xff];
///
/// // Default: rejected.
/// assert!(DecodeOptions::new().decode(&bytes).is_err());
///
/// // Lenient: accepted and normalized.
/// let v = DecodeOptions::new()
/// .strictness(Strictness::LENIENT)
/// .decode(&bytes)
/// .unwrap();
/// assert_eq!(v, Value::from(255));
/// assert_eq!(v.encode(), vec![0x18, 0xff]);
/// ```