Recursive AI

Recover from YAML syntax errors in Rust

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

parse_recovering keeps broken YAML as written and marks the broken parts as error nodes without repairing them.

Terminal
cargo add oneyaml

Requires Rust 1.85 or later.

RUST
use oneyaml::Document as Doc;
 
fn main() {
    let source = "name: 'api\n";
    let parsed = Doc::parse_recovering(source);
    let doc = parsed.value();
    for node in doc.errors() {
        let kind = node.kind();
        let text = node.text();
        println!("{kind:?}: {text}");
    }
    let unchanged = doc.to_string() == source;
    println!("Unchanged: {unchanged}");
    let strict = Doc::parse(source).is_ok();
    println!("Strict: {strict}");
}

Unzip the example, then cargo run.

Text
Error: 'api
Unchanged: true
Strict: false

The opening quote before api has no closing quote. The recovered document keeps that text and exposes it as an error node. Strict parsing returns an error instead of a document.

Variations

To draw the diagnostics with locations and source excerpts, add this after parse_recovering:

RUST
print!("{}", parsed.render_diagnostics(source));

The short answer

Use Document::parse_recovering on the current editor buffer. It returns a Parsed<Document> even if the YAML is incomplete. Read parsed.value().errors() to find the broken regions and parsed.diagnostics() for messages. doc.to_string() returns the original text in this example; the parser doesn’t insert the missing quote. For an application that needs valid configuration, use Document::parse or the typed from_str path instead.

How do you check YAML syntax while someone is typing?

An editor needs to see the unfinished input, not just tell the user it’s invalid. Parse each snapshot of the buffer in recovery mode and display the diagnostics for that snapshot. Keep the snapshot tied to those diagnostics so a later edit doesn’t move the underline onto unrelated text. The recovery API describes broken regions as read-only error nodes; keep raw text edits in the editor rather than trying to change a broken node through a structured YAML edit.

Use parsed.has_errors() to distinguish an error from a warning. Diagnostics can include both, and an error can cut short the parser’s checks, so a recovered buffer isn’t a complete list of what must be fixed. When the user adds the closing quote, parse again against the new text.

Can you deserialize YAML after recovering from a syntax error?

Not while the error remains. The pinned tests check that Document::parse, typed deserialization, and doc.to_value() reject the same broken sequence. Recovery is a way to inspect the source, not to substitute valid data for the broken region. Also keep syntax separate from configuration checks: a syntactically valid document may still fail to deserialize into your application’s types. Use strict, typed parsing where your application actually loads the config.

All oneyaml Rust recipes