Skip to main content

Module path

Module path 

Source
Expand description

Path tracking for YAML structure locations.

This module provides the Path type for tracking the location within a YAML document structure. It’s primarily used for providing meaningful error messages that indicate exactly where in the YAML structure an error occurred.

§Examples

use noyalib::Path;

// Represent the path: root -> "dependencies" -> "serde" -> "version"
let root = Path::Root;
let deps = Path::Map {
    parent: &root,
    key: "dependencies",
};
let serde = Path::Map {
    parent: &deps,
    key: "serde",
};
let version = Path::Map {
    parent: &serde,
    key: "version",
};

assert_eq!(version.to_string(), "dependencies.serde.version");

§Query paths

The path strings read by Value::get_path, Value::query, the borrowed reads, and every path-taking cst::Document method share one grammar: . separates mapping keys, [n] indexes a sequence, * and [*] match every child, and .. descends recursively. A key that itself contains one of those characters is written as a bracket-quoted segment, ["a.b"] or ['a[0]'], inside which \ escapes the next character. quote_key spells one such segment, push_key appends a key to a path in whichever form reads back as that key, and join_keys builds a whole path from literal keys:

use noyalib::path::{join_keys, quote_key};

assert_eq!(quote_key("app.kubernetes.io/name"), r#"["app.kubernetes.io/name"]"#);
assert_eq!(
    join_keys(["labels", "app.kubernetes.io/name"]),
    r#"labels["app.kubernetes.io/name"]"#,
);
assert_eq!(join_keys(["server", "port"]), "server.port");

Enums§

Path
Represents a path to a location within a YAML document structure.

Functions§

join_keys
Build a path from literal mapping keys, one push_key per key, so every key reads back as itself whatever it contains.
push_key
Append the mapping key key to path: in dot notation when the grammar reads the plain spelling back as that key, otherwise as the segment quote_key spells. A quoted segment needs no separator.
quote_key
Spell key as one bracket-quoted path segment, ["key"], that Value::get_path and every path-taking cst::Document method read back as exactly that mapping key. " and \ inside the key are escaped.