Enum CBORCase

Source
pub enum CBORCase {
    Unsigned(u64),
    Negative(u64),
    ByteString(ByteString),
    Text(String),
    Array(Vec<CBOR>),
    Map(Map),
    Tagged(Tag, CBOR),
    Simple(Simple),
}
Expand description

An enum representing all possible CBOR data types.

CBORCase is the core enum that represents all possible CBOR data types according to RFC 8949 and the dCBOR specification. Each variant corresponds to one of the eight major types in CBOR.

This enum is not typically used directly by users of the library. Instead, it’s wrapped by the reference-counted CBOR type, which provides a more ergonomic API.

§Major Types

CBOR defines eight major types, numbered 0 through 7:

Major TypeNameDescription
0Unsigned integerA non-negative integer
1Negative integerA negative integer
2Byte stringA sequence of bytes
3Text stringA UTF-8 string
4ArrayA sequence of data items
5MapA collection of key-value pairs
6Tagged valueA data item with a semantic tag
7Simple valueA simple value like true, false, null, or float

§dCBOR Constraints

According to the dCBOR specification, deterministic encoding adds several constraints:

  • Maps must have lexicographically ordered keys
  • Numeric values must use the smallest possible encoding
  • Floats with integer values are reduced to integers
  • All NaN values are canonicalized to a single representation
  • Strings must be in Unicode Normalization Form C (NFC)

§Example

use dcbor::prelude::*;
use dcbor::{CBORCase, Simple};

// Create a CBOR value using the CBORCase enum
let case = CBORCase::Array(vec![
    CBORCase::Unsigned(1).into(),
    CBORCase::Text("hello".to_string()).into(),
    CBORCase::Simple(Simple::True).into()
]);

// Wrap in the CBOR type for easier handling
let cbor = CBOR::from(case);
assert_eq!(cbor.diagnostic(), "[1, \"hello\", true]");

Variants§

§

Unsigned(u64)

Unsigned integer (major type 0).

Represents a non-negative integer from 0 to 2^64-1.

§

Negative(u64)

Negative integer (major type 1).

Actual value is -1 - n, allowing representation of negative integers from -1 to -2^64.

§

ByteString(ByteString)

Byte string (major type 2).

Represents a sequence of bytes. In dCBOR, byte strings must use the most compact representation possible.

§

Text(String)

UTF-8 string (major type 3).

Represents a UTF-8 encoded string. In dCBOR, text strings must be in Unicode Normalization Form C (NFC).

§

Array(Vec<CBOR>)

Array (major type 4).

Represents a sequence of CBOR data items. dCBOR does not support indefinite-length arrays.

§

Map(Map)

Map (major type 5).

Represents a collection of key-value pairs. In dCBOR, map keys must be in lexicographic order, and duplicate keys are not allowed.

§

Tagged(Tag, CBOR)

Tagged value (major type 6).

Represents a data item with a semantic tag. The tag provides additional information about how to interpret the data.

§

Simple(Simple)

Simple value (major type 7).

Represents simple values like true, false, null, and floating-point numbers. In dCBOR, only a limited set of simple values are allowed.

Trait Implementations§

Source§

impl Clone for CBORCase

Source§

fn clone(&self) -> CBORCase

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CBORCase

Source§

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

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

impl From<CBORCase> for CBOR

Source§

fn from(case: CBORCase) -> Self

Converts to this type from the input type.
Source§

impl Hash for CBORCase

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for CBORCase

Source§

fn eq(&self, other: &CBORCase) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for CBORCase

Source§

impl StructuralPartialEq for CBORCase

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.