//! CBOR codec marker and optional [`minicbor`] integration. //! //! The [`Cbor`] marker type is always available and can be used in type //! signatures without any feature flags. Enable the `minicbor` feature to get //! [`Encode`](super::Encode)/[`Decode`](super::Decode) implementations via //! [`minicbor`]. /// CBOR codec marker type. /// /// This type is always available regardless of feature flags, so you can /// reference it in type signatures from `no_std` crates. Enable the /// `minicbor` feature for [`Encode`](super::Encode)/[`Decode`](super::Decode) /// implementations. /// /// This codec does **not** implement [`Canonical`](super::Canonical) because /// minicbor does not explicitly guarantee deterministic encoding per RFC 8949 ยง4.2. /// If you need canonical CBOR, define your own codec type and audit the encoding. /// /// # Example /// /// ``` /// # #[cfg(all(feature = "minicbor", feature = "sha2"))] /// # { /// use evidence::digest::{Digest, sha2::Sha256}; /// use evidence::codec::cbor::Cbor; /// use minicbor::{Encode, Decode}; /// /// #[derive(Encode, Decode)] /// struct MyData { /// #[n(0)] id: u32, /// #[n(1)] name: String, /// } /// /// let data = MyData { id: 42, name: "test".into() }; /// let digest: Digest = Digest::hash(&data); /// # } /// ``` #[derive(Debug, Clone, Copy)] pub enum Cbor {} /// Error returned when CBOR encoding fails. #[cfg(feature = "minicbor")] #[derive(Debug, Clone)] pub struct CborEncodeError(alloc::string::String); #[cfg(feature = "minicbor")] impl core::fmt::Display for CborEncodeError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "CBOR encode error: {}", self.0) } } #[cfg(feature = "minicbor")] impl> super::Encode for Cbor { type Error = CborEncodeError; fn encode(value: &T) -> Result, Self::Error> { minicbor::to_vec(value).map_err(|e| CborEncodeError(alloc::string::ToString::to_string(&e))) } } /// Error returned when CBOR decoding fails. #[cfg(feature = "minicbor")] #[derive(Debug, Clone)] pub struct CborDecodeError(alloc::string::String); #[cfg(feature = "minicbor")] impl core::fmt::Display for CborDecodeError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "CBOR decode error: {}", self.0) } } #[cfg(feature = "minicbor")] impl minicbor::Decode<'a, ()>> super::Decode for Cbor { type Error = CborDecodeError; fn decode(bytes: &[u8]) -> Result { minicbor::decode(bytes).map_err(|e| CborDecodeError(alloc::string::ToString::to_string(&e))) } }