How to make serde with one level of indirection report the field that failed, if the indirection failed?

This program will error, but it will not tell you which field errored.

use serde::{Deserialize};
use serde_json;

struct Test {
    _foo: usize,
    _bar: Bar,

#[serde(try_from = "BarDe")]
struct Bar(usize);

struct BarDe(i32);

impl TryFrom<BarDe> for Bar {
    type Error = &'static str;
    fn try_from(_: BarDe) -> Result<Self, &'static str> {
        Err("this field is erroneous")

fn main() {
    let _: Test = serde_json::from_str(r#"{"_foo": 1, "_bar": 2}"#).unwrap();

playground link

I wonder if there is a pretty/succinct way to get the field name that failed to deseriliaze?

I thought about adding a #[track_caller] to the try_from, but that gives me code location, not a helpful field name.


Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.