rust-csv icon indicating copy to clipboard operation
rust-csv copied to clipboard

Serde Deserialize errors do not set the field index

Open RossWilliams opened this issue 9 months ago • 0 comments

A field that returns a serde deserialize error will always have the field set to None.

impl SerdeError for DeserializeError {
    fn custom<T: fmt::Display>(msg: T) -> DeserializeError {
        DeserializeError { field: None, kind: DEK::Message(msg.to_string()) }
    }
}

It does not appear we can determine the field that failed to deserialize from serde to report to the user.

One option is to modify the deserialize_string_record function to manually set the err.field in the map_err closure

D::deserialize(&mut deser).map_err(|mut err| {
        err.set_field(Some(deser.0.field.saturating_sub(1));
        Error::new(ErrorKind::Deserialize {
            pos: record.position().map(Clone::clone),
            err,
        })
    })

I am unsure if this is a valid solution or if there are edge cases that will cause the field index to not be accurate.

RossWilliams avatar Feb 26 '25 23:02 RossWilliams