Recursive AI

Replace comments above a YAML key in Rust

oneyaml is a free, open-source YAML library for Rust from Recursive AI. MIT or Apache-2.0, no unsafe code.

Use set_leading to replace the comment above port without changing the value or the # TLS note beside it.

Terminal
cargo add oneyaml

Requires Rust 1.85 or later.

RUST
use oneyaml::Document;
 
fn main() {
    let yaml = "# Old port\nport: 443 # TLS\n";
    let mut doc = Document::parse(yaml)
        .expect("valid YAML");
    doc.get_mut("port").expect("port")
        .set_leading(["HTTPS port"])
        .expect("replace comment");
    print!("{doc}");
}

Unzip the example, then cargo run.

cargo run prints:

Text
# HTTPS port
port: 443 # TLS

If the new comment would attach to a different entry, the edit returns EditError::CommentReattaches and leaves the YAML unchanged.

Variations

To remove just the comments above the key, replace the get_mut chain with the one below. It prints port: 443 # TLS.

RUST
doc.get_mut("port").expect("port")
    .set_leading(oneyaml::NO_COMMENTS)
    .expect("remove leading comments");

The short answer

Use doc.get_mut("port") to select the entry and pass the new comment text to set_leading without the # prefix. It replaces all of the entry’s leading comments, not just the first one. To edit config.yaml, save the inline sample in that file and run the program from the same directory. Replace the let yaml line with the read below, pass &yaml to Document::parse, and replace print!("{doc}") with the write. The edited YAML goes to updated.yaml; the original stays unchanged.

RUST
let yaml = std::fs::read_to_string("config.yaml").expect("read config.yaml");
// Parse &yaml and replace the comment as shown above.
std::fs::write("updated.yaml", doc.to_string()).expect("write updated.yaml");

How do you write multiple comment lines above a YAML key?

Pass one string per line. Replace ["HTTPS port"] with ["HTTPS port", "Use TLS"], and the output has two # lines immediately above port. When the key is nested, set_leading indents the new lines to match the entry and uses the document’s line ending. There’s no need to put indentation, #, or a line break into each string.

This is a replacement, so include every leading line you want after the edit, even one that was already there. Blank lines left by the old comments are kept. For a note on the same line as a value, use the entry’s trailing-comment method instead.

How do you update a YAML comment without changing the key or value?

Edit the parsed Document, rather than searching for the old text across the whole file. The same words may appear under another key or be part of a value. In the example, the entry selected by get_mut("port") is the only entry whose leading comments are replaced, so 443 and # TLS stay in place.

If a comment near the top of a file doesn’t get replaced, it may belong to the document header instead of the first key. For example, the attachment rules distinguish a header separated by a blank line from a leading comment immediately above an entry.

All oneyaml Rust recipes