Function cbor_diag::parse_bytes_partial
source · pub fn parse_bytes_partial(
bytes: impl AsRef<[u8]>
) -> Result<Option<(DataItem, usize)>>
Expand description
Parse a string containing a binary encoded CBOR data item, optionally followed by more data.
Returns one of:
Err(_)
=> a parsing error if there was an issue encounteredOk(None)
=> the end of a data item was not reachedOk(Some(_))
=> the parsed item along with how many bytes were used to parse this item
Examples
use cbor_diag::{DataItem, IntegerWidth, Tag, TextString};
assert_eq!(
cbor_diag::parse_bytes_partial(&b"\
\xd8\x20\x73\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\
\x6c\x65\x2e\x63\x6f\
"[..]).unwrap(),
None);
assert_eq!(
cbor_diag::parse_bytes_partial(&b"\
\xd8\x20\x73\x68\x74\x74\x70\x73\x3a\x2f\x2f\x65\x78\x61\x6d\x70\
\x6c\x65\x2e\x63\x6f\x6d\xff\
"[..]).unwrap(),
Some((
DataItem::Tag {
tag: Tag::URI,
bitwidth: IntegerWidth::Eight,
value: Box::new(DataItem::TextString(TextString {
data: "https://example.com".into(),
bitwidth: IntegerWidth::Zero,
})),
},
22
)));