pub enum Value {
Null,
Bool(bool),
Int(i64),
UInt(u64),
Float(f64),
String(String),
Array(Vec<Value>),
Map(BTreeMap<String, Value>),
}
Variants§
Null
Bool(bool)
Int(i64)
UInt(u64)
Float(f64)
String(String)
Array(Vec<Value>)
Map(BTreeMap<String, Value>)
Implementations§
Source§impl Value
impl Value
pub fn get<I: Index>(&self, index: I) -> Option<&Value>
pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Value>
pub fn is_object(&self) -> bool
pub fn as_object(&self) -> Option<&BTreeMap<String, Value>>
pub fn as_object_mut(&mut self) -> Option<&mut BTreeMap<String, Value>>
pub fn is_array(&self) -> bool
pub fn as_array(&self) -> Option<&Vec<Value>>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>
pub fn is_string(&self) -> bool
pub fn as_str(&self) -> Option<&str>
pub fn is_number(&self) -> bool
pub fn is_i64(&self) -> bool
pub fn is_u64(&self) -> bool
pub fn is_f64(&self) -> bool
pub fn as_i64(&self) -> Option<i64>
pub fn as_u64(&self) -> Option<u64>
pub fn as_f64(&self) -> Option<f64>
pub fn is_boolean(&self) -> bool
pub fn as_bool(&self) -> Option<bool>
pub fn is_null(&self) -> bool
Sourcepub fn as_null(&self) -> Option<()>
pub fn as_null(&self) -> Option<()>
If the Value
is a Null, returns (). Returns None otherwise.
let mut t = BTreeMap::new();
t.insert("a".into(), Value::Null);
t.insert("b".into(), false.into());
let v: Value = t.into();
assert_eq!(v["a"].as_null(), Some(()));
// The boolean `false` is not null.
assert_eq!(v["b"].as_null(), None);
Sourcepub fn pointer(&self, pointer: &str) -> Option<&Value>
pub fn pointer(&self, pointer: &str) -> Option<&Value>
Looks up a value by Pointer.
Pointer defines a string syntax for identifying a specific value
A Pointer is a Unicode string with the reference tokens separated by /
.
Inside tokens /
is replaced by ~1
and ~
is replaced by ~0
. The
addressed value is returned and if there is no such value None
is
returned.
For more information read RFC6901.
§Examples
use std::collections::BTreeMap;
use tracing_formatters::value::Value;
let mut tree2 = BTreeMap::new();
tree2.insert("y".to_string(),Value::Array(vec!["z".into(), "zz".into()]));
let mut tree1 = BTreeMap::new();
tree1.insert("x".to_string(),Value::Map(tree2));
let data = Value::Map(tree1);
assert_eq!(data.pointer("/x/y/1").unwrap(), &Value::from("zz"));
assert_eq!(data.pointer("/a/b/c"), None);
Sourcepub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value>
pub fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value>
Looks up a value by a Pointer and returns a mutable reference to that value.
the Pointer defines a string syntax for identifying a specific value
A Pointer is a Unicode string with the reference tokens separated by /
.
Inside tokens /
is replaced by ~1
and ~
is replaced by ~0
. The
addressed value is returned and if there is no such value None
is
returned.
§Example of Use
use std::collections::BTreeMap;
use tracing_formatters::value::Value;
let mut tree = BTreeMap::new();
tree.insert("x".to_string(), Value::Float(1.0));
tree.insert("y".to_string(), Value::Float(2.0));
let mut value: Value = Value::Map(tree);
// Check value using read-only pointer
assert_eq!(value.pointer("/x"), Some(&Value::Float(1.0)));
// Change value with direct assignment
*value.pointer_mut("/x").unwrap() = Value::Float(1.5);
// Check that new value was written
assert_eq!(value.pointer("/x"), Some(&Value::Float(1.5)));
// Or change the value only if it exists
value.pointer_mut("/x").map(|v| *v = Value::Float(1.5));
// "Steal" ownership of a value. Can replace with any valid Value.
let old_x = value.pointer_mut("/x").map(Value::take).unwrap();
assert_eq!(old_x, Value::Float(1.5));
assert_eq!(value.pointer("/x").unwrap(), &Value::Null);
pub fn take(&mut self) -> Value
Trait Implementations§
impl Eq 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.