Use remove_keep_comments to keep the note above a deleted key, but not the inline comment.
cargo add oneyamluse oneyaml::{Document, path}; fn main() { let yaml = concat!( "web:\n port: 8080\n", " # Proxy settings\n", " old_proxy: off # retired\n", " proxy: ready\n", ); let mut doc = Document::parse(yaml) .expect("valid YAML"); let id = doc.id_of(&path!["web", "old_proxy"]) .expect("old_proxy entry"); let removed = doc.remove_keep_comments(id) .expect("removable entry"); print!("{doc}"); println!("Removed: {:?}", removed.comments()); }
Unzip the example, then cargo run.
Requires Rust 1.85 or later.
web: port: 8080 # Proxy settings proxy: ready Removed: ["# retired"]
The full program
The sample input is inline. The program prints the edited YAML and the comments returned with the removed entry. It also tries ordinary removal on a fresh copy of the same input. If the leading comment can’t attach to the next entry or as a footer, the keep-comments edit returns an error and changes nothing.
use oneyaml::{Document, path}; fn main() { let yaml = concat!( "web:\n port: 8080\n", " # Proxy settings\n", " old_proxy: off # retired\n", " proxy: ready\n", ); let mut doc = Document::parse(yaml) .expect("valid YAML"); let id = doc.id_of(&path!["web", "old_proxy"]) .expect("old_proxy entry"); let removed = doc.remove_keep_comments(id) .expect("removable entry"); print!("{doc}"); println!("Removed: {:?}", removed.comments()); let mut doc = Document::parse(yaml).expect("valid YAML"); let id = doc.id_of(&path!["web", "old_proxy"]) .expect("old_proxy entry"); let removed = doc.remove_node(id).expect("removable entry"); println!("Removed normally: {:?}", removed.comments()); }
cargo run prints:
web: port: 8080 # Proxy settings proxy: ready Removed: ["# retired"] Removed normally: ["# Proxy settings", "# retired"]
Variations
To edit a file, save the inline sample YAML as config.yaml. Replace the inline yaml declaration in the snippet at the top with the read below, and pass &yaml to Document::parse. Add the write after remove_keep_comments succeeds. The original file stays unchanged.
let yaml = std::fs::read_to_string("config.yaml").expect("read config.yaml"); // Parse &yaml and remove the entry as shown above. std::fs::write("updated.yaml", doc.to_string()).expect("write updated.yaml");
Related recipes
The short answer
Parse the source as a Document, look up the mapping entry with id_of, and call remove_keep_comments. In the example, # Proxy settings stays in the YAML above proxy: ready, while # retired leaves with old_proxy. The method returns a Removed, whose comments() lets you see what was taken out. For a file edit, the text to write back is doc.to_string(), not the removed entry.
How do you preserve comments when removing a YAML key?
First decide whether the comment describes the disappearing key or the section around it. The note in the example describes the whole proxy section, so it belongs in the file after old_proxy is gone. The same-line note describes only the retired setting, so it goes with it. Use remove_keep_comments when that split is what you want. Use remove_node if the leading note would be misleading above the next setting.
Pass the path to the key to id_of, as in path!["web", "old_proxy"]. You don’t need to find the token old_proxy yourself or remove the line with a string replacement. The method documentation also distinguishes leading comments written above the entry from comments inside it. Notes inside the entry go out with it, as does the same-line note.
Where do comments go when you remove the last YAML key?
There’s no next key to own the note. When an entry is last in a nested collection, the retained leading lines become a footer for that collection; when it’s last at the root, they become the document footer. The pinned examples show both cases. The note stays in the file but no longer describes a following entry.
If you remove the only entry of a collection with leading comments, or try this on an entry with leading comments in a flow collection (written with braces or brackets), there may be no valid new owner. The method’s error documentation specifies Unsupported for those cases and CommentReattaches when a note would attach somewhere other than intended. Handle the error before saving. If the comment needs to become a section note in a different location, make that a separate edit rather than assuming the library can keep it where the entry was.