ToCborMap

Trait ToCborMap 

Source
pub trait ToCborMap: Sealed {
    // Provided methods
    fn serialize_into<W>(self, writer: W) -> Result<(), Error<W::Error>>
       where Self: Sized,
             W: Write,
             W::Error: Debug { ... }
    fn deserialize_from<R>(reader: R) -> Result<Self, Error<R::Error>>
       where Self: Sized,
             R: Read,
             R::Error: Debug { ... }
    fn to_ciborium_value(&self) -> Value { ... }
}
Expand description

Provides methods to serialize a type into a CBOR map bytestring and back.

This provides methods to serialize_into and deserialize_from CBOR, which is the recommended way to serialize and deserialize any types implementing ToCborMap in this crate. While other methods are provided as well, it’s recommended for clients of this library not to use them, as they are mostly intended for internal use and as such may have an unstable API.

§Example

The following showcases how to serialize a type implementing ToCborMap using the example of an AuthServerRequestCreationHint:

let hint = AuthServerRequestCreationHint::default();
let mut serialized: Vec<u8> = Vec::new();
hint.serialize_into(&mut serialized)?;

From the serialized bytestring, just call deserialize_from on the struct you want to deserialize into:

let deserialized = AuthServerRequestCreationHint::deserialize_from(serialized.as_slice())?;
assert_eq!(hint, deserialized);

Provided Methods§

Source

fn serialize_into<W>(self, writer: W) -> Result<(), Error<W::Error>>
where Self: Sized, W: Write, W::Error: Debug,

Serializes this type as a CBOR map bytestring into the given writer.

§Example

The following showcases how to serialize a type implementing ToCborMap using the example of an AuthServerRequestCreationHint:

let hint = AuthServerRequestCreationHint::default();
let mut serialized: Vec<u8> = Vec::new();
hint.serialize_into(&mut serialized)?;
assert_eq!(serialized, vec![0xA0]);  // 0xA0 == Empty CBOR map.
§Errors
  • When serialization of this value failed, e.g. due to malformed input.
  • When the output couldn’t be put inside the given writer.
Source

fn deserialize_from<R>(reader: R) -> Result<Self, Error<R::Error>>
where Self: Sized, R: Read, R::Error: Debug,

Deserializes from the given reader — which is expected to be an instance of this type, represented as a CBOR map bytestring — into an instance of this type.

§Example

Assuming serialized holds an empty CBOR map, we expect it to deserialize to the default value of a type (this obviously only holds for types which implement Default.) Here, we show this using AuthServerRequestCreationHint as an example:

let serialized = vec![0xA0];
let deserialized = AuthServerRequestCreationHint::deserialize_from(serialized.as_slice())?;
assert_eq!(deserialized, AuthServerRequestCreationHint::default());
§Errors
  • When deserialization of the bytestring failed, e.g. when the given reader does not contain a valid CBOR map or deserializes to a different type than this one.
  • When the input couldn’t be read from the given reader.
Source

fn to_ciborium_value(&self) -> Value

Converts this type to a CBOR serializable Value using to_cbor_map.

§Panics
  • When the integers in the map from to_cbor_map are too high to fit into a Value::Integer.
  • When a CBOR map value can’t be serialized.

Note that both of these would imply a programming mistake on account of dcaf-rs, not its users.

§Example

For example, to serialize a proof-of-possession key into a Value so we can then represent it inside a ClaimsSet (to use it in an access token):

let key = ProofOfPossessionKey::KeyId(vec![0xDC, 0xAF]);
let claims = ClaimsSetBuilder::new()
    .claim(CwtClaimName::Cnf, key.to_ciborium_value())
    .build();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§