---
title: "Recover from YAML syntax errors in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/parse-invalid-yaml-for-an-editor/
description: "Keep malformed YAML unchanged in a Rust editor, show read-only syntax error nodes, and reject the same input when parsing strictly."
version: "1.2.0"
---

# 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.

[crates.io](https://crates.io/crates/oneyaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust) · [Docs](https://recursiveai.org/oneyaml/rust/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/rust/cookbook/parse-invalid-yaml-for-an-editor/parse-invalid-yaml-for-an-editor.zip)

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

```shell
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](https://recursiveai.org/oneyaml/rust/cookbook/parse-invalid-yaml-for-an-editor/parse-invalid-yaml-for-an-editor.zip), 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));
```

## Related recipes

- [Show YAML errors with line and column](https://recursiveai.org/oneyaml/rust/cookbook/errors-with-line-and-column/)
- [Edit YAML and keep its comments](https://recursiveai.org/oneyaml/rust/cookbook/edit-yaml-keep-comments/)

## 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](https://recursiveai.org/oneyaml/rust/docs/guide/) 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](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/tests/document.rs#L217-L245) 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.
