Something went wrong. Try again.
Typelevel cryptographic algo tracking crates.io/crates/evidence
typelevel cryptography util
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182//! 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<MyData, Sha256, Cbor> = 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<T: minicbor::Encode<()>> super::Encode<T> for Cbor { type Error = CborEncodeError;
fn encode(value: &T) -> Result<alloc::vec::Vec<u8>, 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<T: for<'a> minicbor::Decode<'a, ()>> super::Decode<T> for Cbor { type Error = CborDecodeError;
fn decode(bytes: &[u8]) -> Result<T, Self::Error> { minicbor::decode(bytes).map_err(|e| CborDecodeError(alloc::string::ToString::to_string(&e))) }}