Get span information while deserializing with serde?

I'm curious if anyone has ever needed to get span information when deserializing via serde, and if so, what approaches are available?

For a non-serde example showing how span information can be used, imagine you are checking that the links in a Markdown file are valid and come across a potentially undefined link. While you could just print "Warning: Potential incomplete link" and let the user figure it out, it would be much more useful to print something like this:

warning: Potential incomplete link
   ┌─ development/roadmap.md:19:5
   │
19 │   - [x] Comments
   │     ^^^ Did you forget to define a URL for `x`?
   │
   = hint: declare the link's URL. For example: `[x]: http://example.com/`

The way we get these nice errors is by using the span, a struct containing the start and end location of an items, then use the surrounding lines and some clever formatting to print human-friendly error messages.

1 Like

I believe serde_path_to_error gets you partway there.