use crate::{CBOR, CBORError, CBORDecodable, CBORTagged, CBORCase};
use anyhow::bail;
pub trait CBORTaggedDecodable: CBORDecodable + CBORTagged {
fn from_untagged_cbor(cbor: &CBOR) -> anyhow::Result<Self> where Self: Sized;
fn from_tagged_cbor(cbor: &CBOR) -> anyhow::Result<Self> where Self: Sized {
match cbor.case() {
CBORCase::Tagged(tag, item) => {
if *tag == Self::CBOR_TAG {
Self::from_untagged_cbor(item)
} else {
bail!(CBORError::WrongTag(Self::CBOR_TAG, tag.clone()))
}
},
_ => bail!(CBORError::WrongType)
}
}
fn from_tagged_cbor_data(data: &[u8]) -> anyhow::Result<Self> where Self: Sized {
Self::from_tagged_cbor(&CBOR::from_data(data)?)
}
fn from_untagged_cbor_data(data: &[u8]) -> anyhow::Result<Self> where Self: Sized {
Self::from_untagged_cbor(&CBOR::from_data(data)?)
}
}