pub struct Strictness {
pub allow_non_shortest_integers: bool,
pub allow_non_shortest_floats: bool,
pub allow_oversized_bigints: bool,
pub allow_unsorted_map_keys: bool,
pub allow_duplicate_map_keys: bool,
pub allow_indefinite_length: bool,
}Expand description
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.
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 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]);Fields§
§allow_non_shortest_integers: boolAccept integers, lengths, and tag numbers encoded in a wider
argument than necessary (for example 0x19 0x00 0xff for the
value 255 instead of 0x18 0xff).
allow_non_shortest_floats: boolAccept floating point values encoded in a wider form than
necessary (for example an f32 that fits exactly in f16). The
decoded Float is re-stored in the shortest
form that preserves the value bit for bit, including NaN
payloads.
allow_oversized_bigints: boolAccept big integer tags (tag 2 / tag 3) whose payload has
leading zero bytes or fits into a u64. Leading zeros are
stripped; a value that fits is downcast to
Value::Unsigned or
Value::Negative.
allow_unsorted_map_keys: boolAccept maps whose keys are not in CBOR canonical (length and
then bytewise) order. Keys are sorted by Value
after decoding, which is equivalent to canonical order once each
value has been re-encoded in shortest form. With
allow_non_shortest_integers
off, the two orders coincide; with it on, the by-value order is
the only well-defined choice because the original byte lengths
are normalized away.
allow_duplicate_map_keys: boolAccept maps that contain the same key more than once. The last
occurrence wins, matching
Map::from_pairs.
allow_indefinite_length: boolAccept indefinite-length encodings (RFC 8949 §3.2.2) for byte
strings, text strings, arrays, and maps. The chunks of an
indefinite-length string are concatenated; an indefinite-length
array or map is read until the break code. The resulting
Value is the same that a definite-length
encoding would produce, so re-encoding emits canonical bytes.
In Format::Diagnostic input, the
flag also enables the matching RFC 8949 §8 spellings:
[_ ...], {_ ...}, and (_ chunk, ...).
Implementations§
Source§impl Strictness
impl Strictness
Trait Implementations§
Source§impl Clone for Strictness
impl Clone for Strictness
Source§fn clone(&self) -> Strictness
fn clone(&self) -> Strictness
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Strictness
impl Debug for Strictness
Source§impl Default for Strictness
impl Default for Strictness
Source§impl Hash for Strictness
impl Hash for Strictness
Source§impl PartialEq for Strictness
impl PartialEq for Strictness
Source§fn eq(&self, other: &Strictness) -> bool
fn eq(&self, other: &Strictness) -> bool
self and other values to be equal, and is used by ==.