ciborium

Enum Value

Source
#[non_exhaustive]
pub enum Value { Integer(Integer), Bytes(Vec<u8>), Float(f64), Text(String), Bool(bool), Null, Tag(u64, Box<Value>), Array(Vec<Value>), Map(Vec<(Value, Value)>), }
Expand description

A representation of a dynamic CBOR value that can handled dynamically

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Integer(Integer)

An integer

§

Bytes(Vec<u8>)

Bytes

§

Float(f64)

A float

§

Text(String)

A string

§

Bool(bool)

A boolean

§

Null

Null

§

Tag(u64, Box<Value>)

Tag

§

Array(Vec<Value>)

An array

§

Map(Vec<(Value, Value)>)

A map

Implementations§

Source§

impl Value

Source

pub fn deserialized<'de, T: Deserialize<'de>>(&self) -> Result<T, Error>

Deserializes the Value into an object

Source§

impl Value

Source

pub fn serialized<T: ?Sized + Serialize>(value: &T) -> Result<Self, Error>

Serializes an object into a Value

Source§

impl Value

Source

pub fn is_integer(&self) -> bool

Returns true if the Value is an Integer. Returns false otherwise.

let value = Value::Integer(17.into());

assert!(value.is_integer());
Source

pub fn as_integer(&self) -> Option<Integer>

If the Value is a Integer, returns a reference to the associated Integer data. Returns None otherwise.

let value = Value::Integer(17.into());

// We can read the number
assert_eq!(17, value.as_integer().unwrap().try_into().unwrap());
Source

pub fn into_integer(self) -> Result<Integer, Self>

If the Value is a Integer, returns a the associated Integer data as Ok. Returns Err(Self) otherwise.

let value = Value::Integer(17.into());
assert_eq!(value.into_integer(), Ok(Integer::from(17)));

let value = Value::Bool(true);
assert_eq!(value.into_integer(), Err(Value::Bool(true)));
Source

pub fn is_bytes(&self) -> bool

Returns true if the Value is a Bytes. Returns false otherwise.

let value = Value::Bytes(vec![104, 101, 108, 108, 111]);

assert!(value.is_bytes());
Source

pub fn as_bytes(&self) -> Option<&Vec<u8>>

If the Value is a Bytes, returns a reference to the associated bytes vector. Returns None otherwise.

let value = Value::Bytes(vec![104, 101, 108, 108, 111]);

assert_eq!(std::str::from_utf8(value.as_bytes().unwrap()).unwrap(), "hello");
Source

pub fn as_bytes_mut(&mut self) -> Option<&mut Vec<u8>>

If the Value is a Bytes, returns a mutable reference to the associated bytes vector. Returns None otherwise.

let mut value = Value::Bytes(vec![104, 101, 108, 108, 111]);
value.as_bytes_mut().unwrap().clear();

assert_eq!(value, Value::Bytes(vec![]));
Source

pub fn into_bytes(self) -> Result<Vec<u8>, Self>

If the Value is a Bytes, returns a the associated Vec<u8> data as Ok. Returns Err(Self) otherwise.

let value = Value::Bytes(vec![104, 101, 108, 108, 111]);
assert_eq!(value.into_bytes(), Ok(vec![104, 101, 108, 108, 111]));

let value = Value::Bool(true);
assert_eq!(value.into_bytes(), Err(Value::Bool(true)));
Source

pub fn is_float(&self) -> bool

Returns true if the Value is a Float. Returns false otherwise.

let value = Value::Float(17.0.into());

assert!(value.is_float());
Source

pub fn as_float(&self) -> Option<f64>

If the Value is a Float, returns a reference to the associated float data. Returns None otherwise.

let value = Value::Float(17.0.into());

// We can read the float number
assert_eq!(value.as_float().unwrap(), 17.0_f64);
Source

pub fn into_float(self) -> Result<f64, Self>

If the Value is a Float, returns a the associated f64 data as Ok. Returns Err(Self) otherwise.

let value = Value::Float(17.);
assert_eq!(value.into_float(), Ok(17.));

let value = Value::Bool(true);
assert_eq!(value.into_float(), Err(Value::Bool(true)));
Source

pub fn is_text(&self) -> bool

Returns true if the Value is a Text. Returns false otherwise.

let value = Value::Text(String::from("hello"));

assert!(value.is_text());
Source

pub fn as_text(&self) -> Option<&str>

If the Value is a Text, returns a reference to the associated String data. Returns None otherwise.

let value = Value::Text(String::from("hello"));

// We can read the String
assert_eq!(value.as_text().unwrap(), "hello");
Source

pub fn as_text_mut(&mut self) -> Option<&mut String>

If the Value is a Text, returns a mutable reference to the associated String data. Returns None otherwise.

let mut value = Value::Text(String::from("hello"));
value.as_text_mut().unwrap().clear();

assert_eq!(value.as_text().unwrap(), &String::from(""));
Source

pub fn into_text(self) -> Result<String, Self>

If the Value is a String, returns a the associated String data as Ok. Returns Err(Self) otherwise.

let value = Value::Text(String::from("hello"));
assert_eq!(value.into_text().as_deref(), Ok("hello"));

let value = Value::Bool(true);
assert_eq!(value.into_text(), Err(Value::Bool(true)));
Source

pub fn is_bool(&self) -> bool

Returns true if the Value is a Bool. Returns false otherwise.

let value = Value::Bool(false);

assert!(value.is_bool());
Source

pub fn as_bool(&self) -> Option<bool>

If the Value is a Bool, returns a copy of the associated boolean value. Returns None otherwise.

let value = Value::Bool(false);

assert_eq!(value.as_bool().unwrap(), false);
Source

pub fn into_bool(self) -> Result<bool, Self>

If the Value is a Bool, returns a the associated bool data as Ok. Returns Err(Self) otherwise.

let value = Value::Bool(false);
assert_eq!(value.into_bool(), Ok(false));

let value = Value::Float(17.);
assert_eq!(value.into_bool(), Err(Value::Float(17.)));
Source

pub fn is_null(&self) -> bool

Returns true if the Value is a Null. Returns false otherwise.

let value = Value::Null;

assert!(value.is_null());
Source

pub fn is_tag(&self) -> bool

Returns true if the Value is a Tag. Returns false otherwise.

let value = Value::Tag(61, Box::from(Value::Null));

assert!(value.is_tag());
Source

pub fn as_tag(&self) -> Option<(u64, &Value)>

If the Value is a Tag, returns the associated tag value and a reference to the tag Value. Returns None otherwise.

let value = Value::Tag(61, Box::from(Value::Bytes(vec![104, 101, 108, 108, 111])));

let (tag, data) = value.as_tag().unwrap();
assert_eq!(tag, 61);
assert_eq!(data, &Value::Bytes(vec![104, 101, 108, 108, 111]));
Source

pub fn as_tag_mut(&mut self) -> Option<(&mut u64, &mut Value)>

If the Value is a Tag, returns the associated tag value and a mutable reference to the tag Value. Returns None otherwise.

let mut value = Value::Tag(61, Box::from(Value::Bytes(vec![104, 101, 108, 108, 111])));

let (tag, mut data) = value.as_tag_mut().unwrap();
data.as_bytes_mut().unwrap().clear();
assert_eq!(tag, &61);
assert_eq!(data, &Value::Bytes(vec![]));
Source

pub fn into_tag(self) -> Result<(u64, Box<Value>), Self>

If the Value is a Tag, returns a the associated pair of u64 and Box<value> data as Ok. Returns Err(Self) otherwise.

let value = Value::Tag(7, Box::new(Value::Float(12.)));
assert_eq!(value.into_tag(), Ok((7, Box::new(Value::Float(12.)))));

let value = Value::Bool(true);
assert_eq!(value.into_tag(), Err(Value::Bool(true)));
Source

pub fn is_array(&self) -> bool

Returns true if the Value is an Array. Returns false otherwise.

let value = Value::Array(
    vec![
        Value::Text(String::from("foo")),
        Value::Text(String::from("bar"))
    ]
);

assert!(value.is_array());
Source

pub fn as_array(&self) -> Option<&Vec<Value>>

If the Value is an Array, returns a reference to the associated vector. Returns None otherwise.

let value = Value::Array(
    vec![
        Value::Text(String::from("foo")),
        Value::Text(String::from("bar"))
    ]
);

// The length of `value` is 2 elements.
assert_eq!(value.as_array().unwrap().len(), 2);
Source

pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>

If the Value is an Array, returns a mutable reference to the associated vector. Returns None otherwise.

let mut value = Value::Array(
    vec![
        Value::Text(String::from("foo")),
        Value::Text(String::from("bar"))
    ]
);

value.as_array_mut().unwrap().clear();
assert_eq!(value, Value::Array(vec![]));
Source

pub fn into_array(self) -> Result<Vec<Value>, Self>

If the Value is a Array, returns a the associated Vec<Value> data as Ok. Returns Err(Self) otherwise.

let mut value = Value::Array(
    vec![
        Value::Integer(17.into()),
        Value::Float(18.),
    ]
);
assert_eq!(value.into_array(), Ok(vec![Value::Integer(17.into()), Value::Float(18.)]));

let value = Value::Bool(true);
assert_eq!(value.into_array(), Err(Value::Bool(true)));
Source

pub fn is_map(&self) -> bool

Returns true if the Value is a Map. Returns false otherwise.

let value = Value::Map(
    vec![
        (Value::Text(String::from("foo")), Value::Text(String::from("bar")))
    ]
);

assert!(value.is_map());
Source

pub fn as_map(&self) -> Option<&Vec<(Value, Value)>>

If the Value is a Map, returns a reference to the associated Map data. Returns None otherwise.

let value = Value::Map(
    vec![
        (Value::Text(String::from("foo")), Value::Text(String::from("bar")))
    ]
);

// The length of data is 1 entry (1 key/value pair).
assert_eq!(value.as_map().unwrap().len(), 1);

// The content of the first element is what we expect
assert_eq!(
    value.as_map().unwrap().get(0).unwrap(),
    &(Value::Text(String::from("foo")), Value::Text(String::from("bar")))
);
Source

pub fn as_map_mut(&mut self) -> Option<&mut Vec<(Value, Value)>>

If the Value is a Map, returns a mutable reference to the associated Map Data. Returns None otherwise.

let mut value = Value::Map(
    vec![
        (Value::Text(String::from("foo")), Value::Text(String::from("bar")))
    ]
);

value.as_map_mut().unwrap().clear();
assert_eq!(value, Value::Map(vec![]));
assert_eq!(value.as_map().unwrap().len(), 0);
Source

pub fn into_map(self) -> Result<Vec<(Value, Value)>, Self>

If the Value is a Map, returns a the associated Vec<(Value, Value)> data as Ok. Returns Err(Self) otherwise.

let mut value = Value::Map(
    vec![
        (Value::Text(String::from("key")), Value::Float(18.)),
    ]
);
assert_eq!(value.into_map(), Ok(vec![(Value::Text(String::from("key")), Value::Float(18.))]));

let value = Value::Bool(true);
assert_eq!(value.into_map(), Err(Value::Bool(true)));

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&[(Value, Value)]> for Value

Source§

fn from(value: &[(Value, Value)]) -> Self

Converts to this type from the input type.
Source§

impl From<&[Value]> for Value

Source§

fn from(value: &[Value]) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8]> for Value

Source§

fn from(value: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a Value> for Unexpected<'a>

Source§

fn from(value: &'a Value) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<CanonicalValue> for Value

Source§

fn from(v: CanonicalValue) -> Self

Converts to this type from the input type.
Source§

impl From<Integer> for Value

Source§

fn from(value: Integer) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for CanonicalValue

Source§

fn from(v: Value) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<(Value, Value)>> for Value

Source§

fn from(value: Vec<(Value, Value)>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(value: Vec<Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Value

Source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Value

Source§

fn from(value: char) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Value

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Value

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> 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 PartialOrd for Value

Source§

fn partial_cmp(&self, other: &Value) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Value

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 32 bytes

Size for each variant:

  • Integer: 32 bytes
  • Bytes: 32 bytes
  • Float: 16 bytes
  • Text: 32 bytes
  • Bool: 9 bytes
  • Null: 0 bytes
  • Tag: 24 bytes
  • Array: 32 bytes
  • Map: 24 bytes