Skip to main content

ValueIndex

Trait ValueIndex 

Source
pub trait ValueIndex {
    // Required methods
    fn index_into(self, value: &Value) -> Option<&Value>;
    fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>;
    fn index_or_insert(self, value: &mut Value) -> &mut Value;
}
Expand description

A type that can be used to index into a Value.

This trait provides methods for accessing elements within a Value by index. It is implemented for:

  • usize - for indexing into sequences
  • &str - for indexing into mappings by string key
  • String - for indexing into mappings by owned string
  • &String - for indexing into mappings by string reference

§Examples

use noyalib::{from_str, Value, ValueIndex};

let yaml = r#"
items:
  - name: first
  - name: second
config:
  host: localhost
"#;

let value: Value = from_str(yaml).unwrap();

// Using usize to index into sequences
assert_eq!(
    value
        .get("items")
        .unwrap()
        .get(0)
        .unwrap()
        .get("name")
        .unwrap()
        .as_str(),
    Some("first")
);

// Using &str to index into mappings
assert_eq!(
    value.get("config").unwrap().get("host").unwrap().as_str(),
    Some("localhost")
);

Required Methods§

Source

fn index_into(self, value: &Value) -> Option<&Value>

Index into a value, returning a reference to the element if found.

Returns None if:

  • The value is not the appropriate type for this index (e.g., indexing a mapping with usize)
  • The index/key doesn’t exist
Source

fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>

Mutably index into a value, returning a mutable reference to the element if found.

Returns None if:

  • The value is not the appropriate type for this index
  • The index/key doesn’t exist
Source

fn index_or_insert(self, value: &mut Value) -> &mut Value

Index into a value, inserting a default value if the key doesn’t exist.

This method is useful for building nested structures or ensuring a key exists.

§Behavior
  • For sequences: panics if the index is out of bounds
  • For mappings: creates a null entry if the key doesn’t exist
  • For null values: converts to an empty mapping (for string keys only)
§Panics

Panics if:

  • The value is not the appropriate type for this index
  • Indexing a sequence with an out-of-bounds index

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ValueIndex for &String

Source§

fn index_into(self, value: &Value) -> Option<&Value>

Source§

fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>

Source§

fn index_or_insert(self, value: &mut Value) -> &mut Value

Source§

impl ValueIndex for &str

Source§

fn index_into(self, value: &Value) -> Option<&Value>

Source§

fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>

Source§

fn index_or_insert(self, value: &mut Value) -> &mut Value

Source§

impl ValueIndex for String

Source§

fn index_into(self, value: &Value) -> Option<&Value>

Source§

fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>

Source§

fn index_or_insert(self, value: &mut Value) -> &mut Value

Source§

impl ValueIndex for usize

Source§

fn index_into(self, value: &Value) -> Option<&Value>

Source§

fn index_into_mut(self, value: &mut Value) -> Option<&mut Value>

Source§

fn index_or_insert(self, value: &mut Value) -> &mut Value

Implementors§