Skip to main content

Crate yamled

Crate yamled 

Source
Expand description

Format-preserving YAML edits: change what you name, keep every other byte.

yamled replaces, removes, and inserts the values you name in a YAML file. Comments, blank lines, quoting, block and flow style, and indentation stay as they were. It indexes the source through granit-parser spans and writes values through serde-saphyr.

§Quick start

use indoc::indoc;
use yamled::{Document, Path};

let mut ledger = Document::parse(indoc! {"
    active: A-1 # the row in progress
    queue:
      - id: A-1
        title: Write the index
"})?;

ledger.replace(&Path::root().key("active"), "A-2")?;
ledger.replace(
    &Path::root().key("queue").index(0).key("title"),
    "Write the index: v2",
)?;

assert_eq!(
    ledger.as_str(),
    indoc! {"
    active: A-2 # the row in progress
    queue:
      - id: A-1
        title: 'Write the index: v2'
"}
);

§Failed edits

Every edit parses its result. When the result would not parse, the edit returns an Error and the document keeps its old text.

§Cargo features

FeatureAddsDepends on
(none)The location index, remove, take, put, reorder, reindentgranit-parser
serde (default)replace, insert, push, insert_item, their _text forms, TextStyle, Quotesserde, vsh-serde-saphyr

A caller that needs only the line and column of a path, such as a schema error reporter, depends on yamled with default-features = false.

vsh-serde-saphyr is serde-saphyr 2.1.1 with two patches, under the library name serde_saphyr. Its deserializer builds no error report through the snippet renderer, so a program that reads values through yamled does not link it.

Structs§

Document
A YAML document that edits its own text in place.
Fragment
A sequence item taken out of a document by Document::take, with the comments it owns, ready for Document::put.
Location
A byte range in the source, with the line and column where it starts.
Node
A read-only view of one node.
Path
Where a node sits in a document, as the keys and indexes that lead to it.

Enums§

Error
Why a document could not be read or an edit could not be made. A failed edit leaves the document as it was.
Positionserde
Where Document::insert puts a new key.
Quotesserde
Which quotes a one-line string gets when it cannot be plain. A document sets it once, with Document::with_quotes, and every writer follows it. Every variant writes plain text when it reads back unchanged, and double quotes for a character only they can carry, such as a control character or a line break. A replaced node that is already quoted keeps its quotes under TextStyle::Auto.
Segment
One step from a node to a child: a mapping key or a sequence index.
Spacing
Whether a new sequence item is set off from its neighbours by a blank line, for a sequence whose own items do not settle it. A sequence whose neighbours are all separated, or all adjacent, keeps its spacing whatever this says; one with fewer than two items, or with both kinds of gap, gets this spacing. Set it with Document::with_spacing.
Style
How a node is written in the source.
TextStyleserde
The layout a string is written in: block or one line. The document’s Quotes picks the quotes a one-line string gets.