Skip to main content

YamlPath

Trait YamlPath 

Source
pub trait YamlPath {
    // Required methods
    fn get_path(&self, path: &str) -> Option<YamlNode>;
    fn set_path(&self, path: &str, value: impl AsYaml);
    fn remove_path(&self, path: &str) -> bool;
}
Expand description

Trait for YAML types that support path-based access.

Path syntax uses dots (.) as separators to navigate nested mappings. For example, "server.database.host" accesses:

server:
  database:
    host: value

Required Methods§

Source

fn get_path(&self, path: &str) -> Option<YamlNode>

Get a value at a nested path.

§Arguments
  • path - Dot-separated path like "server.host" or "db.primary.port"
§Returns

Some(YamlNode) if the path exists, None otherwise.

§Examples
use yaml_edit::{Document, path::YamlPath};
use std::str::FromStr;

let yaml = Document::from_str("server:\n  host: localhost\n").unwrap();
let host = yaml.get_path("server.host");
assert!(host.is_some());
Source

fn set_path(&self, path: &str, value: impl AsYaml)

Set a value at a nested path, creating intermediate mappings if needed.

§Arguments
  • path - Dot-separated path like "server.host"
  • value - Value to set (can be any type implementing AsYaml)
§Examples
use yaml_edit::{Document, path::YamlPath};
use std::str::FromStr;

let yaml = Document::from_str("name: test\n").unwrap();
yaml.set_path("server.host", "localhost");
yaml.set_path("server.port", 8080);
Source

fn remove_path(&self, path: &str) -> bool

Remove a value at a nested path.

§Arguments
  • path - Dot-separated path to the value to remove
§Returns

true if the value was found and removed, false otherwise.

§Examples
use yaml_edit::{Document, path::YamlPath};
use std::str::FromStr;

let yaml = Document::from_str("server:\n  host: localhost\n  port: 8080\n").unwrap();
assert_eq!(yaml.remove_path("server.port"), true);
assert_eq!(yaml.remove_path("server.missing"), false);

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.

Implementors§