import_stdlib!();
use crate::{CBOR, CBORCase, Error, Map, Result, Simple, tag::Tag};
impl CBOR {
pub fn to_byte_string(data: impl AsRef<[u8]>) -> CBOR {
CBORCase::ByteString(data.as_ref().into()).into()
}
pub fn to_byte_string_from_hex(hex: impl AsRef<str>) -> CBOR {
Self::to_byte_string(hex::decode(hex.as_ref()).unwrap())
}
pub fn try_into_byte_string(self) -> Result<Vec<u8>> {
match self.into_case() {
CBORCase::ByteString(b) => Ok(b.into()),
_ => Err(Error::WrongType),
}
}
pub fn is_byte_string(&self) -> bool {
matches!(self.as_case(), CBORCase::ByteString(_))
}
pub fn into_byte_string(self) -> Option<Vec<u8>> {
self.try_into_byte_string().ok()
}
pub fn try_byte_string(&self) -> Result<Vec<u8>> {
self.clone().try_into_byte_string()
}
pub fn as_byte_string(&self) -> Option<&[u8]> {
match self.as_case() {
CBORCase::ByteString(b) => Some(b),
_ => None,
}
}
}
impl CBOR {
pub fn to_tagged_value(tag: impl Into<Tag>, item: impl Into<CBOR>) -> CBOR {
CBORCase::Tagged(tag.into(), item.into()).into()
}
pub fn try_into_tagged_value(self) -> Result<(Tag, CBOR)> {
match self.into_case() {
CBORCase::Tagged(tag, value) => Ok((tag, value)),
_ => Err(Error::WrongType),
}
}
pub fn is_tagged_value(&self) -> bool {
matches!(self.as_case(), CBORCase::Tagged(_, _))
}
pub fn as_tagged_value(&self) -> Option<(&Tag, &CBOR)> {
match self.as_case() {
CBORCase::Tagged(tag, value) => Some((tag, value)),
_ => None,
}
}
pub fn try_tagged_value(&self) -> Result<(Tag, CBOR)> {
self.clone().try_into_tagged_value()
}
pub fn try_into_expected_tagged_value(
self,
expected_tag: impl Into<Tag>,
) -> Result<CBOR> {
let (tag, value) = self.try_into_tagged_value()?;
let expected_tag = expected_tag.into();
if tag == expected_tag {
Ok(value)
} else {
Err(Error::WrongTag(expected_tag, tag))
}
}
pub fn try_expected_tagged_value(
&self,
expected_tag: impl Into<Tag>,
) -> Result<CBOR> {
self.clone().try_into_expected_tagged_value(expected_tag)
}
}
impl CBOR {
pub fn try_into_text(self) -> Result<String> {
match self.into_case() {
CBORCase::Text(t) => Ok(t),
_ => Err(Error::WrongType),
}
}
pub fn is_text(&self) -> bool {
matches!(self.as_case(), CBORCase::Text(_))
}
pub fn try_text(&self) -> Result<String> { self.clone().try_into_text() }
pub fn into_text(self) -> Option<String> { self.try_into_text().ok() }
pub fn as_text(&self) -> Option<&str> {
match self.as_case() {
CBORCase::Text(t) => Some(t),
_ => None,
}
}
}
impl CBOR {
pub fn try_into_array(self) -> Result<Vec<CBOR>> {
match self.into_case() {
CBORCase::Array(a) => Ok(a),
_ => Err(Error::WrongType),
}
}
pub fn is_array(&self) -> bool {
matches!(self.as_case(), CBORCase::Array(_))
}
pub fn try_array(&self) -> Result<Vec<CBOR>> {
self.clone().try_into_array()
}
pub fn into_array(self) -> Option<Vec<CBOR>> { self.try_into_array().ok() }
pub fn as_array(&self) -> Option<&[CBOR]> {
match self.as_case() {
CBORCase::Array(a) => Some(a),
_ => None,
}
}
}
impl CBOR {
pub fn try_into_map(self) -> Result<Map> {
match self.into_case() {
CBORCase::Map(m) => Ok(m),
_ => Err(Error::WrongType),
}
}
pub fn is_map(&self) -> bool { matches!(self.as_case(), CBORCase::Map(_)) }
pub fn try_map(&self) -> Result<Map> { self.clone().try_into_map() }
pub fn into_map(self) -> Option<Map> { self.try_into_map().ok() }
pub fn try_into_simple_value(self) -> Result<Simple> {
match self.into_case() {
CBORCase::Simple(s) => Ok(s),
_ => Err(Error::WrongType),
}
}
pub fn as_map(&self) -> Option<&Map> {
match self.as_case() {
CBORCase::Map(m) => Some(m),
_ => None,
}
}
}
impl CBOR {
pub fn r#false() -> Self { CBORCase::Simple(Simple::False).into() }
pub fn r#true() -> Self { CBORCase::Simple(Simple::True).into() }
pub fn as_bool(&self) -> Option<bool> {
match self.as_case() {
CBORCase::Simple(Simple::True) => Some(true),
CBORCase::Simple(Simple::False) => Some(false),
_ => None,
}
}
pub fn try_into_bool(self) -> Result<bool> {
match Self::as_bool(&self) {
Some(b) => Ok(b),
None => Err(Error::WrongType),
}
}
pub fn is_bool(&self) -> bool {
matches!(
self.as_case(),
CBORCase::Simple(Simple::True | Simple::False)
)
}
pub fn try_bool(&self) -> Result<bool> { self.clone().try_into_bool() }
pub fn is_true(&self) -> bool {
matches!(self.as_case(), CBORCase::Simple(Simple::True))
}
pub fn is_false(&self) -> bool {
matches!(self.as_case(), CBORCase::Simple(Simple::False))
}
}
impl CBOR {
pub fn null() -> Self { CBORCase::Simple(Simple::Null).into() }
pub fn is_null(&self) -> bool {
matches!(self.as_case(), CBORCase::Simple(Simple::Null))
}
}
impl CBOR {
pub fn is_number(&self) -> bool {
match self.as_case() {
CBORCase::Unsigned(_) | CBORCase::Negative(_) => true,
CBORCase::Simple(s) => s.is_float(),
_ => false,
}
}
pub fn is_nan(&self) -> bool {
match self.as_case() {
CBORCase::Simple(s) => s.is_nan(),
_ => false,
}
}
pub fn nan() -> Self { CBORCase::Simple(Simple::Float(f64::NAN)).into() }
}