Enum ciborium::value::Value [−][src]
#[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)
Tuple Fields
0: Integer
An integer
Bytes(Vec<u8>)
Bytes
Float(f64)
Tuple Fields
0: f64
A float
Text(String)
Tuple Fields
0: String
A string
Bool(bool)
Tuple Fields
0: bool
A boolean
Null
Null
Tag(u64, Box<Value>)
Tag
Array(Vec<Value>)
An array
Map(Vec<(Value, Value)>)
A map
Implementations
Deserializes the Value
into an object
Returns true if the Value
is an Integer
. Returns false otherwise.
let value = Value::Integer(17.into());
assert!(value.is_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());
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());
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");
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![]));
Returns true if the Value
is a Float
. Returns false otherwise.
let value = Value::Float(17.0.into());
assert!(value.is_float());
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);
Returns true if the Value
is a Text
. Returns false otherwise.
let value = Value::Text(String::from("hello"));
assert!(value.is_text());
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");
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(""));
Returns true if the Value
is a Bool
. Returns false otherwise.
let value = Value::Bool(false);
assert!(value.is_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);
Returns true if the Value
is a Null
. Returns false otherwise.
let value = Value::Null;
assert!(value.is_null());
Returns true if the Value
is a Tag
. Returns false otherwise.
let value = Value::Tag(61, Box::from(Value::Null));
assert!(value.is_tag());
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]));
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![]));
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());
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);
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![]));
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());
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")))
);
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);
Trait Implementations
Deserialize this value from the given Serde deserializer. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for Value
impl UnwindSafe for Value
Blanket Implementations
Mutably borrows from an owned value. Read more