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/// Schema-generated policies used by the native compatibility facade.
8pub use codec_proto::generated::proto::reallyme::codec::v1::{
9    CodecGeneralCborFloatPolicy as FloatPolicy, CodecGeneralCborLengthPolicy as LengthPolicy,
10    CodecGeneralCborMapOrdering as MapOrdering, CodecGeneralCborSerialization as Serialization,
11};
12
13/// Basic validity and selected serialization guarantees.
14///
15/// Validation checks UTF-8 and rejects equivalent map keys using RFC 8949's
16/// basic generic data model. Registered tag semantics remain opaque; tag 24
17/// has a separate explicit embedded-item validation operation.
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub struct Profile {
20    pub(super) serialization: Serialization,
21    pub(super) lengths: LengthPolicy,
22    pub(super) floats: FloatPolicy,
23    pub(super) ordering: MapOrdering,
24}
25
26impl Profile {
27    pub(super) fn ensure_supported(self) -> Result<(), CborError> {
28        Self::new(self.serialization, self.lengths, self.floats, self.ordering).map(|_| ())
29    }
30
31    /// Construct an explicit profile; preferred serialization requires definite lengths.
32    pub fn new(
33        serialization: Serialization,
34        lengths: LengthPolicy,
35        floats: FloatPolicy,
36        ordering: MapOrdering,
37    ) -> Result<Self, CborError> {
38        ensure_lengths_supported(lengths)?;
39        if !matches!(
40            serialization,
41            Serialization::Received | Serialization::Preferred
42        ) || !matches!(
43            floats,
44            FloatPolicy::All | FloatPolicy::FiniteOnly | FloatPolicy::Reject
45        ) || !matches!(
46            ordering,
47            MapOrdering::Received | MapOrdering::LengthFirst | MapOrdering::Bytewise
48        ) {
49            return Err(error(ErrorReason::UnsupportedPolicy));
50        }
51        if serialization == Serialization::Preferred && lengths != LengthPolicy::DefiniteOnly {
52            return Err(error(ErrorReason::UnsupportedPolicy));
53        }
54        Ok(Self {
55            serialization,
56            lengths,
57            floats,
58            ordering,
59        })
60    }
61
62    /// Basic validity with received widths, indefinite support, and unsorted unique maps.
63    #[must_use]
64    pub const fn received() -> Self {
65        Self {
66            serialization: Serialization::Received,
67            lengths: LengthPolicy::AllowIndefinite,
68            floats: FloatPolicy::All,
69            ordering: MapOrdering::Received,
70        }
71    }
72
73    /// Preferred widths and definite lengths, with unsorted unique maps.
74    #[must_use]
75    pub const fn preferred() -> Self {
76        Self {
77            serialization: Serialization::Preferred,
78            lengths: LengthPolicy::DefiniteOnly,
79            floats: FloatPolicy::All,
80            ordering: MapOrdering::Received,
81        }
82    }
83
84    /// Select floating-point acceptance without changing other guarantees.
85    #[must_use]
86    pub const fn with_floats(mut self, policy: FloatPolicy) -> Self {
87        self.floats = policy;
88        self
89    }
90
91    /// Select map ordering; encoding never sorts caller data automatically.
92    #[must_use]
93    pub const fn with_map_ordering(mut self, ordering: MapOrdering) -> Self {
94        self.ordering = ordering;
95        self
96    }
97
98    /// Inspect the received length policy.
99    #[must_use]
100    pub const fn lengths(self) -> LengthPolicy {
101        self.lengths
102    }
103
104    /// Inspect serialization guarantees.
105    #[must_use]
106    pub const fn serialization(self) -> Serialization {
107        self.serialization
108    }
109
110    /// Inspect floating-point acceptance.
111    #[must_use]
112    pub const fn floats(self) -> FloatPolicy {
113        self.floats
114    }
115
116    /// Inspect the independently selected map-order constraint.
117    #[must_use]
118    pub const fn map_ordering(self) -> MapOrdering {
119        self.ordering
120    }
121}
122
123pub(super) fn ensure_lengths_supported(value: LengthPolicy) -> Result<(), CborError> {
124    match value {
125        LengthPolicy::DefiniteOnly | LengthPolicy::AllowIndefinite => Ok(()),
126        _ => Err(error(ErrorReason::UnsupportedPolicy)),
127    }
128}