pub struct Cbor(/* private fields */);
Expand description
Wrapper around a byte slice that encodes a valid CBOR item.
For details on the format see RFC 8949.
When interpreting CBOR messages from the outside (e.g. from the network) it is
advisable to ingest those using the CborOwned::canonical
constructor.
In case the message was encoded for example using CborBuilder
it is sufficient to use the unchecked
constructor.
The Display implementation adheres to the diagnostic notation.
Implementations§
source§impl Cbor
impl Cbor
sourcepub fn unchecked(bytes: &[u8]) -> &Self
pub fn unchecked(bytes: &[u8]) -> &Self
Unconditionally cast the given byte slice as CBOR item
No checks on the integrity are made, indexing methods may panic if encoded
lengths are out of bound or when encountering invalid encodings.
If you want to carefully treat data obtained from unreliable sources, prefer
CborOwned::canonical
.
The results of CborBuilder
can safely be fed to this method.
sourcepub fn from_cow_unchecked(bytes: Cow<'_, [u8]>) -> Cow<'_, Cbor>
pub fn from_cow_unchecked(bytes: Cow<'_, [u8]>) -> Cow<'_, Cbor>
Unconditionally convert the given bytes as CBOR item
The borrowed variant is converted using unchecked
without
allocating. The owned variant is converted by either reusing the allocated vector
or storing the bytes inline (if they fit) and releasing the vector.
No checks on the integrity are made, indexing methods may panic if encoded
lengths are out of bound or when encountering invalid encodings.
If you want to carefully treat data obtained from unreliable sources, prefer
CborOwned::canonical
.
sourcepub fn checked(bytes: &[u8]) -> Result<&Self, ParseError>
pub fn checked(bytes: &[u8]) -> Result<&Self, ParseError>
Cast the given byte slice as CBOR item if the encoding is valid
sourcepub fn checked_prefix(bytes: &[u8]) -> Result<(&Self, &[u8]), ParseError>
pub fn checked_prefix(bytes: &[u8]) -> Result<(&Self, &[u8]), ParseError>
Cast the given byte slice as CBOR item if the encoding is valid, permitting more bytes to follow the item
sourcepub fn from_cow_checked(
bytes: Cow<'_, [u8]>
) -> Result<Cow<'_, Cbor>, ParseError>
pub fn from_cow_checked( bytes: Cow<'_, [u8]> ) -> Result<Cow<'_, Cbor>, ParseError>
Convert the given bytes to a CBOR item if the encoding is valid
The borrowed variant is converted using checked
without
allocating. The owned variant is converted using CborOwned::canonical
.
sourcepub fn decode(&self) -> CborValue<'_>
pub fn decode(&self) -> CborValue<'_>
Interpret the CBOR item at a higher level
While kind
gives you precise information on how the item is encoded,
this method interprets the tag-based encoding according to the standard, adding for example
big integers, decimals, and floats, or turning base64-encoded text strings into binary strings.
An iterator over the tags present on this item, from outermost to innermost
sourcepub fn tagged_item(&self) -> TaggedItem<'_>
pub fn tagged_item(&self) -> TaggedItem<'_>
The low-level encoding of this item with its tags
sourcepub fn try_null(&self) -> Result<(), TypeError>
pub fn try_null(&self) -> Result<(), TypeError>
More efficient shortcut for .decode().is_null()
with error reporting.
sourcepub fn try_bool(&self) -> Result<bool, TypeError>
pub fn try_bool(&self) -> Result<bool, TypeError>
More efficient shortcut for .decode().as_bool()
with error reporting.
sourcepub fn try_number(&self) -> Result<Number<'_>, TypeError>
pub fn try_number(&self) -> Result<Number<'_>, TypeError>
More efficient shortcut for .decode().to_number()
with error reporting.
sourcepub fn try_timestamp(&self) -> Result<Timestamp, TypeError>
pub fn try_timestamp(&self) -> Result<Timestamp, TypeError>
More efficient shortcut for .decode().as_timestamp()
with error reporting.
sourcepub fn try_bytes(&self) -> Result<Cow<'_, [u8]>, TypeError>
pub fn try_bytes(&self) -> Result<Cow<'_, [u8]>, TypeError>
More efficient shortcut for .decode().to_bytes()
with error reporting.
sourcepub fn try_str(&self) -> Result<Cow<'_, str>, TypeError>
pub fn try_str(&self) -> Result<Cow<'_, str>, TypeError>
More efficient shortcut for .decode().to_str()
with error reporting.
sourcepub fn try_array(&self) -> Result<Vec<Cow<'_, Cbor>>, TypeError>
pub fn try_array(&self) -> Result<Vec<Cow<'_, Cbor>>, TypeError>
More efficient shortcut for .decode().to_array()
with error reporting.
sourcepub fn try_dict(
&self
) -> Result<BTreeMap<Cow<'_, Cbor>, Cow<'_, Cbor>>, TypeError>
pub fn try_dict( &self ) -> Result<BTreeMap<Cow<'_, Cbor>, Cow<'_, Cbor>>, TypeError>
More efficient shortcut for .decode().to_dict()
with error reporting.
sourcepub fn index<'a, 'b>(
&'a self,
path: impl IntoIterator<Item = PathElement<'b>>
) -> Option<Cow<'a, Cbor>>
pub fn index<'a, 'b>( &'a self, path: impl IntoIterator<Item = PathElement<'b>> ) -> Option<Cow<'a, Cbor>>
Extract a value by indexing into arrays and dicts, with path elements yielded by an iterator.
Returns None if an index doesn’t exist or the indexed object is neither an array nor a dict. When the object under consideration is an array, the next path element must represent an integer number.
Providing an empty iterator will yield the current Cbor item.
Returns a borrowed Cbor unless the traversal entered a TAG_CBOR_ITEM byte string with indefinite
encoding (in which case the bytes need to be assembled into a Vec before continuing). This cannot
happen if the item being indexed stems from CborOwned::canonical
.
sourcepub fn index_borrowed<'a, 'b>(
&'a self,
path: impl IntoIterator<Item = PathElement<'b>>
) -> Option<&'a Cbor>
pub fn index_borrowed<'a, 'b>( &'a self, path: impl IntoIterator<Item = PathElement<'b>> ) -> Option<&'a Cbor>
Extract a value by indexing into arrays and dicts, with path elements yielded by an iterator.
Returns None if an index doesn’t exist or the indexed object is neither an array nor a dict. When the object under consideration is an array, the next path element must represent an integer number.
Providing an empty iterator will yield the current Cbor item.
§Panics
Panics if this CBOR item contains a TAG_CBOR_ITEM byte string that has been index into by this
path traversal. Use CborOwned::canonical
to ensure
that this cannot happen.
Trait Implementations§
source§impl PartialEq for Cbor
impl PartialEq for Cbor
source§impl PartialOrd for Cbor
impl PartialOrd for Cbor
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more