dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
965 stars 165 forks source link

Support emitting spans when deserializing structures #181

Closed roblabla closed 2 years ago

roblabla commented 4 years ago

Serde-toml has quite the interesting feature: they support emitting spans for the underlying object it returns. For instance,

#[macro_use]
extern crate serde_derive;

extern crate toml;
use toml::spanned::Spanned;

#[derive(Deserialize)]
struct Udoprog {
    s: Spanned<String>,
}

fn main() {
    let t = "s = \"udoprog\"\n";

    let u: Udoprog = toml::from_str(t).unwrap();

    assert_eq!(u.s.start, 4);
    assert_eq!(u.s.end, 13);
}

This allows users of serde-toml to provide their own high-level diagnostics. For instance, it could be combined with a crate like codespan to provide diagnostics fairly similar to rustc', without having to write our own parsers.

It'd be nice for serde-yaml to provide a similar feature.

dtolnay commented 2 years ago

I would prefer not to have this in this library, but it would be reasonable for someone else to implement a more fully featured YAML library which implements this.