Skip to main content

Module path

Module path 

Source
Expand description

Path-based access to YAML documents.

Provides convenient dot-separated path syntax for accessing nested YAML values like "server.host" or "database.primary.port".

Operations: try_get_path, try_set_path, try_remove_path. The try_ variants return Result<_, PathError> so malformed paths, empty paths, missing keys, and container-type mismatches are visible rather than silently swallowed.

The legacy get_path / set_path / remove_path methods still exist as thin wrappers that discard PathError; they are deprecated and will emit warnings.

§Example

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

let yaml = Document::from_str("server:\n  host: localhost\n  port: 8080\n").unwrap();

// Get nested values
let host = yaml.try_get_path("server.host").ok();

// Set nested values (creates intermediate mappings)
yaml.try_set_path("database.primary.host", "db.example.com").unwrap();

// Remove nested values
yaml.try_remove_path("server.port").unwrap();

§Aliases

When a path segment lands on an alias (*name), reads resolve it to the anchored value by default, matching get_resolved and merged:

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

let doc = Document::from_str("shared: &shared\n  timeout: 30\nservice: *shared\n").unwrap();
assert_eq!(doc.try_get_path("service.timeout").unwrap().to_string(), "30");

Writes are different: writing through an alias changes the anchored node, and so changes every other alias pointing at it. That is refused by default. Pass an AliasPolicy to try_set_path_with to choose deliberately between updating the shared anchor (Follow) and replacing the alias with an independent copy (Expand).

All operations preserve formatting, comments, and whitespace.

Enums§

AliasPolicy
What path traversal should do when a segment lands on an alias (*name) that it needs to descend through.
PathError
Error returned by try_get_path / try_set_path / try_remove_path.
PathParseError
Error from try_parse_path.
PathSegment
Represents a segment in a YAML path.

Constants§

MAX_INDEX_GROWTH
The most null placeholders try_set_path will insert to reach an out-of-range sequence index.

Traits§

YamlPath
Trait for YAML types that support path-based access.

Functions§

parse_pathDeprecated
Parse a path string into components.
try_parse_path
Parse a path string into components.