Quote app.name in the lookup path.
cargo add oneyamluse oneyaml::Document; fn main() -> Result<(), oneyaml::Error> { let yaml = "labels: {app.name: web}\n"; let doc = Document::parse(yaml)?; let path = r#"labels."app.name""#; let node = doc.get_path(path); let name = node.and_then(|n| n.as_str()); println!("{name:?}"); Ok(()) }
The example is the full program below: unzip it and cargo run. To run only the code above, paste it into src/main.rs of a new cargo new example project and cargo run.
The literal dotted key gives:
Some("web")Full program
The download uses Rust 1.85 or later and reads this config.yaml:
labels: {app.name: web}use oneyaml::Document; fn main() -> Result<(), Box<dyn std::error::Error>> { let yaml = std::fs::read_to_string("config.yaml")?; let doc = Document::parse(&yaml)?; let node = doc.get_path(r#"labels."app.name""#); let name = node.and_then(|node| node.as_str()); println!("{name:?}"); Ok(()) }
cargo run prints:
Some("web")The lookup returns None if the path is malformed or names no node; as_str also returns None for a node that isn’t a string.
Variations
Use a key from a variable. Replace the let node line. path! takes the key as one segment; it prints the same Some("web") without manual escaping.
let key = "app.name"; let node = doc.get_path(&oneyaml::path!["labels", key]);
Related recipes
The short answer
Use get_path with double quotes around the segment that contains a literal dot. With oneyaml-rust, the path labels."app.name" takes two steps: the labels mapping, then its app.name key. Rust’s raw string syntax (r#"…"#) keeps the inner quotes in the path without backslashes in your Rust code.
How do you look up a YAML key that has a dot in its name?
The quotes belong in the lookup path, not necessarily in the YAML. That distinction matters when both forms exist under the same parent:
labels: app.name: web app: name: worker
The path with a quoted segment reads web; the unquoted labels.app.name reads worker. The file has both keys, and neither lookup falls back to the other. If you quote app.name in the YAML but leave its lookup path unquoted, the path still takes three steps. See the path syntax at version 1.2.0.
For a key your program gets at runtime, supply the parts with path! as in the variation. Don’t insert the key into a dot-separated string: a dot in the key would make another step, and a quote could make the path malformed. In a string path, the same quoting rule applies to a key with a space or square bracket. To select a list item after a dotted key, put [0] outside the segment’s closing quote; [0] in the quotes would be part of the key. Path strings match string keys. If a mapping uses an integer as a key, use NodeRef::get on the parent with the integer key instead.