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

Include node kind in missing fields error message #14

Merged
merged 1 commit into from
May 13, 2021
Merged
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
61 changes: 47 additions & 14 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ where
V: Visitor<'de>,
{
let _ = fields;
visitor.visit_map(NodeFieldsDeserializer { node: self })
let kind = self.kind;
match visitor.visit_map(NodeFieldsDeserializer { node: self }) {
Ok(value) => Ok(value),
Err(error) => Err(error.with_kind(kind)),
}
}
}

Expand Down Expand Up @@ -316,7 +320,11 @@ where
where
V: Visitor<'de>,
{
visitor.visit_map(self)
let kind = self.node.kind;
match visitor.visit_map(self) {
Ok(value) => Ok(value),
Err(error) => Err(error.with_kind(kind)),
}
}

fn deserialize_enum<V>(
Expand Down Expand Up @@ -367,20 +375,29 @@ where
T: Deserialize<'de>,
M: MapAccess<'de>,
{
type Error = M::Error;
type Error = FieldOfKindError<M::Error>;

fn next_key_seed<K>(&mut self, mut seed: K) -> Result<Option<K::Value>, Self::Error>
where
K: DeserializeSeed<'de>,
{
loop {
seed = match self.node.map.next_key_seed(NodeFieldSeed {
kind: self.node.kind,
seed,
})? {
seed = match self
.node
.map
.next_key_seed(NodeFieldSeed {
kind: self.node.kind,
seed,
})
.map_err(FieldOfKindError::Other)?
{
None => return Ok(None),
Some(NodeField::Inner(seed)) => {
*self.node.inner = self.node.map.next_value()?;
*self.node.inner = self
.node
.map
.next_value()
.map_err(FieldOfKindError::Other)?;
seed
}
Some(NodeField::Delegate(value)) => return Ok(Some(value)),
Expand All @@ -392,7 +409,10 @@ where
where
V: DeserializeSeed<'de>,
{
self.node.map.next_value_seed(seed)
self.node
.map
.next_value_seed(seed)
.map_err(FieldOfKindError::Other)
}
}

Expand Down Expand Up @@ -570,6 +590,9 @@ enum FieldOfKindError<E> {
field: String,
expected: &'static [&'static str],
},
MissingField {
field: &'static str,
},
Other(E),
}

Expand All @@ -596,6 +619,13 @@ where
))
}
}
FieldOfKindError::MissingField { field } => {
if let AnyKind::Kind(Kind::null) = kind {
E::missing_field(field)
} else {
E::custom(format_args!("missing field `{}` in {}", field, kind))
}
}
FieldOfKindError::Other(error) => error,
}
}
Expand All @@ -612,6 +642,10 @@ where
}
}

fn missing_field(field: &'static str) -> Self {
FieldOfKindError::MissingField { field }
}

fn custom<T>(msg: T) -> Self
where
T: Display,
Expand All @@ -635,10 +669,6 @@ where
FieldOfKindError::Other(E::unknown_variant(variant, expected))
}

fn missing_field(field: &'static str) -> Self {
FieldOfKindError::Other(E::missing_field(field))
}

fn duplicate_field(field: &'static str) -> Self {
FieldOfKindError::Other(E::duplicate_field(field))
}
Expand All @@ -650,7 +680,7 @@ where
{
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
FieldOfKindError::UnknownField { .. } => None,
FieldOfKindError::UnknownField { .. } | FieldOfKindError::MissingField { .. } => None,
FieldOfKindError::Other(error) => error.source(),
}
}
Expand All @@ -674,6 +704,9 @@ where
)
}
}
FieldOfKindError::MissingField { field } => {
write!(formatter, "missing field `{}`", field)
}
FieldOfKindError::Other(error) => Display::fmt(error, formatter),
}
}
Expand Down