aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAzriel Hoh <azriel91@gmail.com>2018-11-17 09:37:02 +1300
committerAzriel Hoh <azriel91@gmail.com>2018-11-17 13:37:24 +1300
commit438157e662d2092dd49513a9791aa75788180c35 (patch)
tree0eae1e822d930e6bf2cb2b672f3381f0763c6a54 /src
parent45ab620a59443dc6c3e6dae19bf5c09c222f21d4 (diff)
downloadmilf-rs-438157e662d2092dd49513a9791aa75788180c35.tar.gz
milf-rs-438157e662d2092dd49513a9791aa75788180c35.zip
Only validate table keys against fields when deserializing enum.
Issue #225
Diffstat (limited to 'src')
-rw-r--r--src/de.rs45
1 files changed, 27 insertions, 18 deletions
diff --git a/src/de.rs b/src/de.rs
index 869f7ae..9d5a417 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -501,14 +501,21 @@ impl<'de> de::Deserializer<'de> for StrDeserializer<'de> {
struct ValueDeserializer<'a> {
value: Value<'a>,
+ validate_struct_keys: bool,
}
impl<'a> ValueDeserializer<'a> {
fn new(value: Value<'a>) -> ValueDeserializer<'a> {
ValueDeserializer {
value: value,
+ validate_struct_keys: false,
}
}
+
+ fn with_struct_key_validation(mut self) -> Self {
+ self.validate_struct_keys = true;
+ self
+ }
}
impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
@@ -557,26 +564,28 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
}
}
- match &self.value.e {
- &E::InlineTable(ref values) | &E::DottedTable(ref values) => {
- let extra_fields = values.iter()
- .filter_map(|(ref key, ref _val)| {
- if !fields.contains(&&(**key)) {
- Some(key.clone())
- } else {
- None
- }
- })
- .collect::<Vec<Cow<'de, str>>>();
+ if self.validate_struct_keys {
+ match &self.value.e {
+ &E::InlineTable(ref values) | &E::DottedTable(ref values) => {
+ let extra_fields = values.iter()
+ .filter_map(|(ref key, ref _val)| {
+ if !fields.contains(&&(**key)) {
+ Some(key.clone())
+ } else {
+ None
+ }
+ })
+ .collect::<Vec<Cow<'de, str>>>();
- if !extra_fields.is_empty() {
- return Err(Error::from_kind(ErrorKind::UnexpectedKeys {
- keys: extra_fields.iter().map(|k| k.to_string()).collect::<Vec<_>>(),
- available: fields,
- }));
+ if !extra_fields.is_empty() {
+ return Err(Error::from_kind(ErrorKind::UnexpectedKeys {
+ keys: extra_fields.iter().map(|k| k.to_string()).collect::<Vec<_>>(),
+ available: fields,
+ }));
+ }
}
+ _ => {}
}
- _ => {}
}
if name == spanned::NAME && fields == &[spanned::START, spanned::END, spanned::VALUE] {
@@ -900,7 +909,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
V: de::Visitor<'de>,
{
de::Deserializer::deserialize_struct(
- ValueDeserializer::new(self.value),
+ ValueDeserializer::new(self.value).with_struct_key_validation(),
"", // TODO: this should be the variant name
fields,
visitor,