Only validate table keys against fields when deserializing enum.

Issue #225
This commit is contained in:
Azriel Hoh 2018-11-17 09:37:02 +13:00
parent 5bcf6f7cb6
commit 752bf0d4a1

View file

@ -501,14 +501,21 @@ impl<'de> de::Deserializer<'de> for StrDeserializer<'de> {
struct ValueDeserializer<'a> { struct ValueDeserializer<'a> {
value: Value<'a>, value: Value<'a>,
validate_struct_keys: bool,
} }
impl<'a> ValueDeserializer<'a> { impl<'a> ValueDeserializer<'a> {
fn new(value: Value<'a>) -> ValueDeserializer<'a> { fn new(value: Value<'a>) -> ValueDeserializer<'a> {
ValueDeserializer { ValueDeserializer {
value: value, 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> { impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
@ -557,6 +564,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
} }
} }
if self.validate_struct_keys {
match &self.value.e { match &self.value.e {
&E::InlineTable(ref values) | &E::DottedTable(ref values) => { &E::InlineTable(ref values) | &E::DottedTable(ref values) => {
let extra_fields = values.iter() let extra_fields = values.iter()
@ -578,6 +586,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
} }
_ => {} _ => {}
} }
}
if name == spanned::NAME && fields == &[spanned::START, spanned::END, spanned::VALUE] { if name == spanned::NAME && fields == &[spanned::START, spanned::END, spanned::VALUE] {
let start = self.value.start; let start = self.value.start;
@ -900,7 +909,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
de::Deserializer::deserialize_struct( de::Deserializer::deserialize_struct(
ValueDeserializer::new(self.value), ValueDeserializer::new(self.value).with_struct_key_validation(),
"", // TODO: this should be the variant name "", // TODO: this should be the variant name
fields, fields,
visitor, visitor,