use bytes::Bytes;
use crate::{CBOR, CBORError, CBORCase};
pub trait CBORDecodable: TryFrom<CBOR> + 'static {
fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> where Self: Sized;
fn from_cbor_data(cbor_data: &[u8]) -> anyhow::Result<Self> where Self: Sized {
Self::from_cbor(&CBOR::from_data(cbor_data).map_err(anyhow::Error::msg)?)
}
}
impl CBORDecodable for CBOR {
fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
Ok(cbor.clone())
}
}
impl CBORDecodable for Bytes {
fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
match cbor.case() {
CBORCase::ByteString(b) => Ok(b.clone()),
_ => Err(anyhow::Error::msg(CBORError::WrongType))?
}
}
}