Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting of the serde csv serializer. #317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{error::Error as StdError, fmt, iter, num, str};

use serde::{
de::value::BorrowedBytesDeserializer,
de::{
Expand All @@ -9,6 +7,7 @@ use serde::{
},
serde_if_integer128,
};
use std::{error::Error as StdError, fmt, iter, num, str};

use crate::{
byte_record::{ByteRecord, ByteRecordIter},
Expand All @@ -22,11 +21,14 @@ pub fn deserialize_string_record<'de, D: Deserialize<'de>>(
record: &'de StringRecord,
headers: Option<&'de StringRecord>,
) -> Result<D, Error> {
let mut deser = DeRecordWrap(DeStringRecord {
it: record.iter().peekable(),
headers: headers.map(|r| r.iter()),
field: 0,
});
let mut deser = DeRecordWrap(
DeStringRecord {
it: record.iter().peekable(),
headers: headers.map(|r| r.iter()),
field: 0,
},
None,
);
D::deserialize(&mut deser).map_err(|err| {
Error::new(ErrorKind::Deserialize {
pos: record.position().map(Clone::clone),
Expand All @@ -39,11 +41,14 @@ pub fn deserialize_byte_record<'de, D: Deserialize<'de>>(
record: &'de ByteRecord,
headers: Option<&'de ByteRecord>,
) -> Result<D, Error> {
let mut deser = DeRecordWrap(DeByteRecord {
it: record.iter().peekable(),
headers: headers.map(|r| r.iter()),
field: 0,
});
let mut deser = DeRecordWrap(
DeByteRecord {
it: record.iter().peekable(),
headers: headers.map(|r| r.iter()),
field: 0,
},
None,
);
D::deserialize(&mut deser).map_err(|err| {
Error::new(ErrorKind::Deserialize {
pos: record.position().map(Clone::clone),
Expand Down Expand Up @@ -99,7 +104,7 @@ trait DeRecord<'r> {
) -> Result<V::Value, DeserializeError>;
}

struct DeRecordWrap<T>(T);
struct DeRecordWrap<T>(T, Option<Vec<u8>>); // inner, fieldName

impl<'r, T: DeRecord<'r>> DeRecord<'r> for DeRecordWrap<T> {
#[inline]
Expand Down Expand Up @@ -638,14 +643,33 @@ impl<'a, 'de: 'a, T: DeRecord<'de>> MapAccess<'de>
None => return Ok(None),
Some(field) => field,
};
self.1 = Some(field.to_owned());
seed.deserialize(BorrowedBytesDeserializer::new(field)).map(Some)
}

fn next_value_seed<K: DeserializeSeed<'de>>(
&mut self,
seed: K,
) -> Result<K::Value, Self::Error> {
seed.deserialize(&mut **self)
let field_value = self.peek_field();
seed.deserialize(&mut **self).map_err(|e| {
// enhance error with field name and field value
let DeserializeError { field, kind } = e;
DeserializeError {
field,
kind: DeserializeErrorKind::Message(format!(
"{}. (Field '{}' has a value '{}')",
kind.to_string(),
self.1
.clone()
.map_or("".to_owned(), |x| String::from_utf8(x)
.unwrap()),
field_value.map_or("n/a".to_owned(), |d| {
String::from_utf8(d.to_owned()).unwrap()
})
)),
}
})
}
}

Expand Down