Enum ciborium::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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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 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> 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,

§

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>,

§

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>,

§

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>,