#[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
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
impl Value
Sourcepub fn deserialized<'de, T: Deserialize<'de>>(&self) -> Result<T, Error>
pub fn deserialized<'de, T: Deserialize<'de>>(&self) -> Result<T, Error>
Deserializes the Value
into an object
Source§impl Value
impl Value
Sourcepub fn is_integer(&self) -> bool
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());
Sourcepub fn as_integer(&self) -> Option<Integer>
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());
Sourcepub fn into_integer(self) -> Result<Integer, Self>
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)));
Sourcepub fn is_bytes(&self) -> bool
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());
Sourcepub fn as_bytes(&self) -> Option<&Vec<u8>>
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");
Sourcepub fn as_bytes_mut(&mut self) -> Option<&mut Vec<u8>>
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![]));
Sourcepub fn into_bytes(self) -> Result<Vec<u8>, Self>
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)));
Sourcepub fn is_float(&self) -> bool
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());
Sourcepub fn as_float(&self) -> Option<f64>
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);
Sourcepub fn into_float(self) -> Result<f64, Self>
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)));
Sourcepub fn is_text(&self) -> bool
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());
Sourcepub fn as_text(&self) -> Option<&str>
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");
Sourcepub fn as_text_mut(&mut self) -> Option<&mut String>
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(""));
Sourcepub fn into_text(self) -> Result<String, Self>
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)));
Sourcepub fn is_bool(&self) -> bool
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());
Sourcepub fn as_bool(&self) -> Option<bool>
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);
Sourcepub fn into_bool(self) -> Result<bool, Self>
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.)));
Sourcepub fn is_null(&self) -> bool
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());
Sourcepub fn is_tag(&self) -> bool
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());
Sourcepub fn as_tag(&self) -> Option<(u64, &Value)>
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]));
Sourcepub fn as_tag_mut(&mut self) -> Option<(&mut u64, &mut Value)>
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![]));
Sourcepub fn into_tag(self) -> Result<(u64, Box<Value>), Self>
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)));
Sourcepub fn is_array(&self) -> bool
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());
Sourcepub fn as_array(&self) -> Option<&Vec<Value>>
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);
Sourcepub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>
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![]));
Sourcepub fn into_array(self) -> Result<Vec<Value>, Self>
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)));
Sourcepub fn is_map(&self) -> bool
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());
Sourcepub fn as_map(&self) -> Option<&Vec<(Value, Value)>>
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")))
);
Sourcepub fn as_map_mut(&mut self) -> Option<&mut Vec<(Value, Value)>>
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);
Sourcepub fn into_map(self) -> Result<Vec<(Value, Value)>, Self>
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<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<'a> From<&'a Value> for Unexpected<'a>
impl<'a> From<&'a Value> for Unexpected<'a>
Source§impl From<CanonicalValue> for Value
impl From<CanonicalValue> for Value
Source§fn from(v: CanonicalValue) -> Self
fn from(v: CanonicalValue) -> Self
Source§impl From<Value> for CanonicalValue
impl From<Value> for CanonicalValue
Source§impl PartialOrd for Value
impl PartialOrd for Value
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
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 bytesBytes
: 32 bytesFloat
: 16 bytesText
: 32 bytesBool
: 9 bytesNull
: 0 bytesTag
: 24 bytesArray
: 32 bytesMap
: 24 bytes