Skip to main content

codec_cbor/general/
profile.rs

1// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use super::error::{error, CborError, ErrorReason};
6
7/// Received indefinite-length policy.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9#[non_exhaustive]
10pub enum LengthPolicy {
11    /// Reject all indefinite-length items.
12    DefiniteOnly,
13    /// Accept well-formed indefinite containers and chunked strings.
14    AllowIndefinite,
15}
16
17/// Serialization checks are independent of map ordering.
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19#[non_exhaustive]
20pub enum Serialization {
21    /// Preserve received widths without requiring shortest encodings.
22    Received,
23    /// Require shortest arguments and shortest exact floating-point encodings.
24    Preferred,
25}
26
27/// Explicit floating-point acceptance policy.
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29#[non_exhaustive]
30pub enum FloatPolicy {
31    /// Accept all binary16/32/64 values, including infinities and NaNs.
32    All,
33    /// Accept finite values only, including both signs of zero.
34    FiniteOnly,
35    /// Reject all floating-point values.
36    Reject,
37}
38
39/// Optional ordering constraint on complete received key encodings.
40#[derive(Clone, Copy, Debug, Eq, PartialEq)]
41#[non_exhaustive]
42pub enum MapOrdering {
43    /// Preserve any received order; semantic key uniqueness is still checked.
44    Received,
45    /// Compare encoded length first, then encoded bytes.
46    LengthFirst,
47    /// Compare complete key encodings lexicographically.
48    Bytewise,
49}
50
51/// Basic validity and selected serialization guarantees.
52///
53/// Validation checks UTF-8 and rejects equivalent map keys using RFC 8949's
54/// basic generic data model. Registered tag semantics remain opaque; tag 24
55/// has a separate explicit embedded-item validation operation.
56#[derive(Clone, Copy, Debug, Eq, PartialEq)]
57pub struct Profile {
58    pub(super) serialization: Serialization,
59    pub(super) lengths: LengthPolicy,
60    pub(super) floats: FloatPolicy,
61    pub(super) ordering: MapOrdering,
62}
63
64impl Profile {
65    /// Construct an explicit profile; preferred serialization requires definite lengths.
66    pub fn new(
67        serialization: Serialization,
68        lengths: LengthPolicy,
69        floats: FloatPolicy,
70        ordering: MapOrdering,
71    ) -> Result<Self, CborError> {
72        if serialization == Serialization::Preferred && lengths != LengthPolicy::DefiniteOnly {
73            return Err(error(ErrorReason::UnsupportedPolicy));
74        }
75        Ok(Self {
76            serialization,
77            lengths,
78            floats,
79            ordering,
80        })
81    }
82
83    /// Basic validity with received widths, indefinite support, and unsorted unique maps.
84    #[must_use]
85    pub const fn received() -> Self {
86        Self {
87            serialization: Serialization::Received,
88            lengths: LengthPolicy::AllowIndefinite,
89            floats: FloatPolicy::All,
90            ordering: MapOrdering::Received,
91        }
92    }
93
94    /// Preferred widths and definite lengths, with unsorted unique maps.
95    #[must_use]
96    pub const fn preferred() -> Self {
97        Self {
98            serialization: Serialization::Preferred,
99            lengths: LengthPolicy::DefiniteOnly,
100            floats: FloatPolicy::All,
101            ordering: MapOrdering::Received,
102        }
103    }
104
105    /// Select floating-point acceptance without changing other guarantees.
106    #[must_use]
107    pub const fn with_floats(mut self, policy: FloatPolicy) -> Self {
108        self.floats = policy;
109        self
110    }
111
112    /// Select map ordering; encoding never sorts caller data automatically.
113    #[must_use]
114    pub const fn with_map_ordering(mut self, ordering: MapOrdering) -> Self {
115        self.ordering = ordering;
116        self
117    }
118
119    /// Inspect the received length policy.
120    #[must_use]
121    pub const fn lengths(self) -> LengthPolicy {
122        self.lengths
123    }
124
125    /// Inspect serialization guarantees.
126    #[must_use]
127    pub const fn serialization(self) -> Serialization {
128        self.serialization
129    }
130
131    /// Inspect floating-point acceptance.
132    #[must_use]
133    pub const fn floats(self) -> FloatPolicy {
134        self.floats
135    }
136
137    /// Inspect the independently selected map-order constraint.
138    #[must_use]
139    pub const fn map_ordering(self) -> MapOrdering {
140        self.ordering
141    }
142}