use crate::cbor;
use crate::cbor::CborError;
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
pub type Bytes = Vec<u8>;
pub trait ToCbor: Sized {
fn to_cbor(self) -> ciborium::Value;
fn to_cbor_bytes(self) -> Result<Bytes, ToCborError> {
cbor::to_vec(&self.to_cbor()).map_err(Into::into)
}
}
pub trait ToCborMap {
fn to_cbor_map(self) -> BTreeMap<ciborium::Value, ciborium::Value>;
}
pub trait ToNamespaceMap {
fn to_ns_map(self) -> BTreeMap<String, ciborium::Value>;
}
#[derive(Debug)]
pub enum ToCborError {
CborError(CborError),
}
impl fmt::Display for ToCborError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ToCborError::CborError(err) => write!(f, "COSE error: {err}"),
}
}
}
impl Error for ToCborError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ToCborError::CborError(err) => Some(err),
}
}
}
impl From<CborError> for ToCborError {
fn from(err: CborError) -> ToCborError {
ToCborError::CborError(err)
}
}
impl<T> ToCbor for T
where
T: Into<ciborium::Value>,
{
fn to_cbor(self) -> ciborium::Value {
self.into()
}
}