serde-yaml icon indicating copy to clipboard operation
serde-yaml copied to clipboard

Support emitting spans when deserializing structures

Open roblabla opened this issue 4 years ago • 0 comments

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.

roblabla avatar Nov 30 '20 15:11 roblabla

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.

dtolnay avatar Aug 14 '22 01:08 dtolnay