Error

Enum Error 

Source
pub enum Error {
Show 16 variants Underrun, UnsupportedHeaderValue(u8), NonCanonicalNumeric, InvalidSimpleValue, InvalidString(Utf8Error), NonCanonicalString, UnusedData(usize), MisorderedMapKey, DuplicateMapKey, MissingMapKey, OutOfRange, WrongType, WrongTag(Tag, Tag), InvalidUtf8(Utf8Error), InvalidDate(String), Custom(String),
}
Expand description

A comprehensive set of errors that can occur during CBOR encoding and decoding operations, with special focus on enforcing the deterministic encoding rules specified in the dCBOR specification.

The dCBOR implementation validates all encoded CBOR against the deterministic encoding requirements of RFC 8949 §4.2.1, plus additional constraints defined in the dCBOR application profile. These errors represent all the possible validation failures and decoding issues that can arise.

§Examples

use dcbor::prelude::*;

// Handling the WrongType error when trying to convert a different type
let cbor_string = CBOR::from("hello");
let result = cbor_string.try_into_byte_string();

assert!(result.is_err());
if let Err(e) = result {
    assert!(e.to_string().contains("not the expected type"));
}

Variants§

§

Underrun

The CBOR data ended prematurely during decoding, before a complete CBOR item could be decoded. This typically happens when a CBOR item’s structure indicates more data than is actually present.

§

UnsupportedHeaderValue(u8)

An unsupported or invalid value was encountered in a CBOR header byte. The parameter contains the unsupported header byte value. This can occur when decoding CBOR that uses unsupported features or is malformed.

§

NonCanonicalNumeric

A CBOR numeric value was encoded in a non-canonical form, violating the deterministic encoding requirement of dCBOR (per Section 2.3 of the dCBOR specification).

This error is triggered when:

  • An integer is not encoded in its shortest possible form
  • A floating point value that could be represented as an integer was not reduced
  • A NaN value was not encoded in its canonical form (f97e00)
§

InvalidSimpleValue

An invalid CBOR simple value was encountered during decoding.

Per Section 2.4 of the dCBOR specification, only false, true, null, and floating point values are valid simple values in dCBOR. All other major type 7 values are invalid.

§

InvalidString(Utf8Error)

A CBOR text string was not valid UTF-8. The parameter contains the specific UTF-8 error.

All CBOR text strings (major type 3) must be valid UTF-8 per RFC 8949.

§

NonCanonicalString

A CBOR text string was not encoded in Unicode Canonical Normalization Form C (NFC).

Per Section 2.5 of the dCBOR specification, all text strings must be in NFC form, and decoders must reject any encoded text strings that are not in NFC.

§

UnusedData(usize)

The decoded CBOR item didn’t consume all input data. The parameter contains the number of unused bytes.

This error occurs when decoding functions expect exactly one CBOR item but the input contains additional data after a valid CBOR item.

§

MisorderedMapKey

The keys in a decoded CBOR map were not in canonical lexicographic order of their encoding.

Per the CDE specification and Section 2.1 of dCBOR, map keys must be in ascending lexicographic order of their encoded representation for deterministic encoding.

§

DuplicateMapKey

A decoded CBOR map contains duplicate keys, which is invalid.

Per Section 2.2 of the dCBOR specification, CBOR maps must not contain duplicate keys, and decoders must reject encoded maps with duplicate keys.

§

MissingMapKey

A requested key was not found in a CBOR map during data extraction.

§

OutOfRange

A CBOR numeric value could not be represented in the specified target numeric type.

This occurs when attempting to convert a CBOR number to a Rust numeric type that is too small to represent the value without loss of precision.

§

WrongType

The CBOR value is not of the expected type for a conversion or operation.

This occurs when attempting to convert a CBOR value to a type that doesn’t match the actual CBOR item’s type (e.g., trying to convert a string to an integer).

§

WrongTag(Tag, Tag)

The CBOR tagged value had a different tag than expected. The first parameter is the expected tag, and the second is the actual tag found.

§

InvalidUtf8(Utf8Error)

Invalid UTF‑8 in a text string.

§

InvalidDate(String)

Invalid ISO 8601 date format.

§

Custom(String)

Custom error message.

Implementations§

Source§

impl Error

Source

pub fn msg(s: impl Into<String>) -> Self

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<&str> for Error

Source§

fn from(error: &str) -> Self

Converts a string slice into a CBORError::Custom variant.

Source§

impl From<String> for Error

Source§

fn from(error: String) -> Self

Converts a String into a CBORError::Custom variant.

Source§

impl From<Utf8Error> for Error

Source§

fn from(source: Utf8Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.