Enum Value

Source
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

Source

pub fn get<I: Index>(&self, index: I) -> Option<&Value>

Source

pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Value>

Source

pub fn is_object(&self) -> bool

Source

pub fn as_object(&self) -> Option<&BTreeMap<String, Value>>

Source

pub fn as_object_mut(&mut self) -> Option<&mut BTreeMap<String, Value>>

Source

pub fn is_array(&self) -> bool

Source

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

Source

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

Source

pub fn is_string(&self) -> bool

Source

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

Source

pub fn is_number(&self) -> bool

Source

pub fn is_i64(&self) -> bool

Source

pub fn is_u64(&self) -> bool

Source

pub fn is_f64(&self) -> bool

Source

pub fn as_i64(&self) -> Option<i64>

Source

pub fn as_u64(&self) -> Option<u64>

Source

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

Source

pub fn is_boolean(&self) -> bool

Source

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

Source

pub fn is_null(&self) -> bool

Source

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);
Source

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);
Source

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);
Source

pub fn take(&mut self) -> Value

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

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

impl Default for Value

Source§

fn default() -> Value

Returns the “default value” for a type. Read more
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<BTreeMap<String, Value>> for Value

Source§

fn from(t: BTreeMap<String, Value>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl<I> Index<I> for Value
where I: Index,

Source§

type Output = Value

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Value

Performs the indexing (container[index]) operation. Read more
Source§

impl<I> IndexMut<I> for Value
where I: Index,

Source§

fn index_mut(&mut self, index: I) -> &mut Value

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Self) -> 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 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> 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,