Trait Dig

Source
pub trait Dig {
    // Required method
    fn value_for_name(&self, name: &str) -> Option<&Self>;

    // Provided method
    fn dig(&self, selector: impl AsRef<str>) -> Option<&Self> { ... }
}
Expand description

Used to “dig through” recursive data structures to extract named values using a selector string. Selectors are sequential names separated by an ASCII ‘.’ character, optionally prefixed with a ‘$’ root segment.

§Examples

When the serde_json feature is enabled, Value implements Dig:

let value = json!({
    "foo": {
        "bar": {
            "baz": "hello there"
        }
    }
});

let expected = Value::String(String::from("hello there"));

assert_eq!(value.dig("foo.bar.baz"), Some(&expected));

Required Methods§

Source

fn value_for_name(&self, name: &str) -> Option<&Self>

Retrieves a datum identified by the given name segment, or none.

Provided Methods§

Source

fn dig(&self, selector: impl AsRef<str>) -> Option<&Self>

Fetches the data within self identified by the given selector.

Selector strings have a lightweight syntax resembling basic JSON-Path selectors - chains of name segments, separated by ASCII period characters (.). As in JSON Path, selectors can be absolute (i.e. prefixed with a sigil, like $.) or relative.

Returns an optional result, containing a reference to the named data if found, and none if not.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Dig for Value

Source§

fn value_for_name(&self, name: &str) -> Option<&Self>

Implementors§