From f5d14af5a668ed2c2f2ce35244c3934370d57978 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 08:59:46 +1300 Subject: [PATCH 01/11] Implement deserialization for unit and struct variants. Issue #225 --- examples/enum_external.rs | 40 +++++++++++++++ src/de.rs | 101 +++++++++++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 examples/enum_external.rs diff --git a/examples/enum_external.rs b/examples/enum_external.rs new file mode 100644 index 0000000..1feff77 --- /dev/null +++ b/examples/enum_external.rs @@ -0,0 +1,40 @@ +//! An example showing off the usage of `Deserialize` to automatically decode +//! TOML into a Rust `struct`, with enums. + +#![deny(warnings)] + +extern crate toml; +#[macro_use] +extern crate serde_derive; + +/// This is what we're going to decode into. +#[derive(Debug, Deserialize)] +struct Config { + plain: MyEnum, + // tuple: MyEnum, + #[serde(rename = "struct")] + structv: MyEnum, + my_enum: Vec, +} + +#[derive(Debug, Deserialize)] +enum MyEnum { + Plain, + Tuple(i64, bool), + Struct { value: i64 }, +} + +fn main() { + let toml_str = r#" + plain = "Plain" + # tuple = { 0 = 123, 1 = true } + struct = { Struct = { value = 123 } } + my_enum = [ + { Plain = {} }, + # { Tuple = { 0 = 123, 1 = true } }, + { Struct = { value = 123 } } + ]"#; + + let decoded: Config = toml::from_str(toml_str).unwrap(); + println!("{:#?}", decoded); +} diff --git a/src/de.rs b/src/de.rs index 491ae47..5de4f6a 100644 --- a/src/de.rs +++ b/src/de.rs @@ -585,7 +585,27 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { { match self.value.e { E::String(val) => visitor.visit_enum(val.into_deserializer()), - _ => Err(Error::from_kind(ErrorKind::ExpectedString)) + E::InlineTable(values) | E::DottedTable(values) => { + if values.len() != 1 { + Err(Error::from_kind(ErrorKind::Wanted { + expected: "exactly 1 element", + found: if values.is_empty() { + "zero elements" + } else { + "more than 1 element" + }, + })) + } else { + visitor.visit_enum(InlineTableDeserializer { + values: values.into_iter(), + next_value: None, + }) + } + } + e @ _ => Err(Error::from_kind(ErrorKind::Wanted { + expected: "string or table", + found: e.type_name(), + })), } } @@ -724,6 +744,70 @@ impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { } } +impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { + type Error = Error; + type Variant = Self; + + fn variant_seed(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> + where + V: de::DeserializeSeed<'de>, + { + let (key, value) = match self.values.next() { + Some(pair) => pair, + None => { + return Err(Error::from_kind(ErrorKind::Wanted { + expected: "table with exactly 1 entry", + found: "empty map", + })) + } + }; + self.next_value = Some(value); + + seed.deserialize(StrDeserializer::new(key)) + .map(|val| (val, self)) + } +} + +impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { + type Error = Error; + + fn unit_variant(self) -> Result<(), Self::Error> { + // TODO: Error handling if there are entries + Ok(()) + } + + fn newtype_variant_seed(self, seed: T) -> Result + where + T: de::DeserializeSeed<'de>, + { + seed.deserialize(ValueDeserializer::new( + self.next_value.expect("Expected value"), + )) + } + + fn tuple_variant(self, _len: usize, _visitor: V) -> Result + where + V: de::Visitor<'de>, + { + unimplemented!() + } + + fn struct_variant( + self, + fields: &'static [&'static str], + visitor: V, + ) -> Result + where + V: de::Visitor<'de>, + { + de::Deserializer::deserialize_struct( + ValueDeserializer::new(self.next_value.expect("Expected value")), + "", // TODO: this should be the variant name + fields, + visitor, + ) + } +} impl<'a> Deserializer<'a> { /// Creates a new deserializer which will be deserializing the string @@ -1510,6 +1594,21 @@ enum E<'a> { DottedTable(Vec<(Cow<'a, str>, Value<'a>)>), } +impl<'a> E<'a> { + fn type_name(&self) -> &'static str { + match *self { + E::String(..) => "string", + E::Integer(..) => "integer", + E::Float(..) => "float", + E::Boolean(..) => "boolean", + E::Datetime(..) => "datetime", + E::Array(..) => "array", + E::InlineTable(..) => "inline table", + E::DottedTable(..) => "dotted table", + } + } +} + impl<'a> Value<'a> { fn same_type(&self, other: &Value<'a>) -> bool { match (&self.e, &other.e) { From 9a69c17a4ef9bdc4485ca0f637f2fc033573675f Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 09:16:03 +1300 Subject: [PATCH 02/11] Added `NewType` variant to example. --- examples/enum_external.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/enum_external.rs b/examples/enum_external.rs index 1feff77..095dca5 100644 --- a/examples/enum_external.rs +++ b/examples/enum_external.rs @@ -14,6 +14,7 @@ struct Config { // tuple: MyEnum, #[serde(rename = "struct")] structv: MyEnum, + newtype: MyEnum, my_enum: Vec, } @@ -21,6 +22,7 @@ struct Config { enum MyEnum { Plain, Tuple(i64, bool), + NewType(String), Struct { value: i64 }, } @@ -29,9 +31,11 @@ fn main() { plain = "Plain" # tuple = { 0 = 123, 1 = true } struct = { Struct = { value = 123 } } + newtype = { NewType = "value" } my_enum = [ { Plain = {} }, # { Tuple = { 0 = 123, 1 = true } }, + { NewType = "value" }, { Struct = { value = 123 } } ]"#; From 4f28c2f6397fb7084f9fe01d7cdf658a4aa01630 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 11:36:46 +1300 Subject: [PATCH 03/11] Implemented enum tuple deserialization. Issue #225 --- examples/enum_external.rs | 8 +++-- src/de.rs | 69 +++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/examples/enum_external.rs b/examples/enum_external.rs index 095dca5..e2ac4f8 100644 --- a/examples/enum_external.rs +++ b/examples/enum_external.rs @@ -11,7 +11,8 @@ extern crate serde_derive; #[derive(Debug, Deserialize)] struct Config { plain: MyEnum, - // tuple: MyEnum, + plain_table: MyEnum, + tuple: MyEnum, #[serde(rename = "struct")] structv: MyEnum, newtype: MyEnum, @@ -29,12 +30,13 @@ enum MyEnum { fn main() { let toml_str = r#" plain = "Plain" - # tuple = { 0 = 123, 1 = true } + plain_table = { Plain = {} } + tuple = { Tuple = { 0 = 123, 1 = true } } struct = { Struct = { value = 123 } } newtype = { NewType = "value" } my_enum = [ { Plain = {} }, - # { Tuple = { 0 = 123, 1 = true } }, + { Tuple = { 0 = 123, 1 = true } }, { NewType = "value" }, { Struct = { value = 123 } } ]"#; diff --git a/src/de.rs b/src/de.rs index 5de4f6a..4f5a5f2 100644 --- a/src/de.rs +++ b/src/de.rs @@ -162,6 +162,12 @@ enum ErrorKind { /// A struct was expected but something else was found ExpectedString, + /// A tuple with a certain number of elements was expected but something else was found + ExpectedTuple(usize), + + /// An empty table was expected but entries were found + ExpectedEmptyTable, + /// Dotted key attempted to extend something that is not a table. DottedKeyInvalidType, @@ -757,7 +763,7 @@ impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { None => { return Err(Error::from_kind(ErrorKind::Wanted { expected: "table with exactly 1 entry", - found: "empty map", + found: "empty table", })) } }; @@ -772,8 +778,19 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { type Error = Error; fn unit_variant(self) -> Result<(), Self::Error> { - // TODO: Error handling if there are entries - Ok(()) + match self.next_value.expect("Expected value").e { + E::InlineTable(values) | E::DottedTable(values) => { + if values.len() == 0 { + Ok(()) + } else { + Err(Error::from_kind(ErrorKind::ExpectedEmptyTable)) + } + } + e @ _ => Err(Error::from_kind(ErrorKind::Wanted { + expected: "table", + found: e.type_name(), + })), + } } fn newtype_variant_seed(self, seed: T) -> Result @@ -785,11 +802,45 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { )) } - fn tuple_variant(self, _len: usize, _visitor: V) -> Result + fn tuple_variant(self, len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { - unimplemented!() + let next_value = self.next_value.expect("Expected value"); + match next_value.e { + E::InlineTable(values) | E::DottedTable(values) => { + let tuple_values = values + .into_iter() + .enumerate() + .filter_map(|(index, (key, value))| { + // TODO: Is this expensive? + if key == format!("{}", index) { + Some(value) + } else { + // TODO: Err() + unimplemented!() + } + }) + .collect::>(); // TODO: is this expensive? + + if tuple_values.len() == len { + de::Deserializer::deserialize_seq( + ValueDeserializer::new(Value { + e: E::Array(tuple_values), + start: next_value.start, + end: next_value.end, + }), + visitor, + ) + } else { + Err(Error::from_kind(ErrorKind::ExpectedTuple(len))) + } + } + e @ _ => Err(Error::from_kind(ErrorKind::Wanted { + expected: "table", + found: e.type_name(), + })), + } } fn struct_variant( @@ -1470,7 +1521,11 @@ impl fmt::Display for Error { ErrorKind::MultilineStringKey => "multiline strings are not allowed for key".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, ErrorKind::ExpectedString => "expected string".fmt(f)?, - ErrorKind::DottedKeyInvalidType => "dotted key attempted to extend non-table type".fmt(f)?, + ErrorKind::ExpectedTuple(l) => write!(f, "expected tuple with length {}", l)?, + ErrorKind::ExpectedEmptyTable => "expected empty table".fmt(f)?, + ErrorKind::DottedKeyInvalidType => { + "dotted key attempted to extend non-table type".fmt(f)? + } ErrorKind::__Nonexhaustive => panic!(), } @@ -1515,6 +1570,8 @@ impl error::Error for Error { ErrorKind::MultilineStringKey => "invalid multiline string for key", ErrorKind::Custom => "a custom error", ErrorKind::ExpectedString => "expected string", + ErrorKind::ExpectedTuple(_) => "expected tuple", + ErrorKind::ExpectedEmptyTable => "expected empty table", ErrorKind::DottedKeyInvalidType => "dotted key invalid type", ErrorKind::__Nonexhaustive => panic!(), } From 7777c1452f48e0c7ae840ce9531fd99ad72d9a6e Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Sat, 20 Oct 2018 16:24:16 +1300 Subject: [PATCH 04/11] Better error handling when parsing tuples. Issue #225 --- src/de.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/de.rs b/src/de.rs index 4f5a5f2..810b85b 100644 --- a/src/de.rs +++ b/src/de.rs @@ -162,9 +162,19 @@ enum ErrorKind { /// A struct was expected but something else was found ExpectedString, - /// A tuple with a certain number of elements was expected but something else was found + /// A tuple with a certain number of elements was expected but something + /// else was found. ExpectedTuple(usize), + /// Expected table keys to be in increasing tuple index order, but something + /// else was found. + ExpectedTupleIndex { + /// Expected index. + expected: usize, + /// Key that was specified. + found: String, + }, + /// An empty table was expected but entries were found ExpectedEmptyTable, @@ -812,16 +822,24 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { let tuple_values = values .into_iter() .enumerate() - .filter_map(|(index, (key, value))| { - // TODO: Is this expensive? - if key == format!("{}", index) { - Some(value) - } else { - // TODO: Err() - unimplemented!() - } + .map(|(index, (key, value))| match key.parse::() { + Ok(key_index) if key_index == index => Ok(value), + Ok(_) | Err(_) => Err(Error::from_kind(ErrorKind::ExpectedTupleIndex { + expected: index, + found: key.to_string(), + })), }) - .collect::>(); // TODO: is this expensive? + // Fold all values into a `Vec`, or return the first error. + .fold(Ok(Vec::with_capacity(len)), |result, value_result| { + result.and_then(|mut tuple_values| match value_result { + Ok(value) => { + tuple_values.push(value); + Ok(tuple_values) + } + // `Result` to `Result, Self::Error>` + Err(e) => Err(e), + }) + })?; if tuple_values.len() == len { de::Deserializer::deserialize_seq( @@ -1521,7 +1539,11 @@ impl fmt::Display for Error { ErrorKind::MultilineStringKey => "multiline strings are not allowed for key".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, ErrorKind::ExpectedString => "expected string".fmt(f)?, - ErrorKind::ExpectedTuple(l) => write!(f, "expected tuple with length {}", l)?, + ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}", l)?, + ErrorKind::ExpectedTupleIndex { + expected, + ref found, + } => write!(f, "expected table key `{}`, but was `{}`", expected, found)?, ErrorKind::ExpectedEmptyTable => "expected empty table".fmt(f)?, ErrorKind::DottedKeyInvalidType => { "dotted key attempted to extend non-table type".fmt(f)? @@ -1570,7 +1592,8 @@ impl error::Error for Error { ErrorKind::MultilineStringKey => "invalid multiline string for key", ErrorKind::Custom => "a custom error", ErrorKind::ExpectedString => "expected string", - ErrorKind::ExpectedTuple(_) => "expected tuple", + ErrorKind::ExpectedTuple(_) => "expected table length", + ErrorKind::ExpectedTupleIndex { .. } => "expected table key", ErrorKind::ExpectedEmptyTable => "expected empty table", ErrorKind::DottedKeyInvalidType => "dotted key invalid type", ErrorKind::__Nonexhaustive => panic!(), From a4928a47a89ffd0bc437c30021d7e790ec8af188 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Mon, 22 Oct 2018 09:40:16 +1300 Subject: [PATCH 05/11] Implemented deserialization for external enums. Issue #225 --- src/de.rs | 246 ++++++++++++++++++++--------- tests/enum_external_deserialize.rs | 153 ++++++++++++++++++ 2 files changed, 328 insertions(+), 71 deletions(-) create mode 100644 tests/enum_external_deserialize.rs diff --git a/src/de.rs b/src/de.rs index 810b85b..7fd1387 100644 --- a/src/de.rs +++ b/src/de.rs @@ -159,9 +159,6 @@ enum ErrorKind { /// type. Custom, - /// A struct was expected but something else was found - ExpectedString, - /// A tuple with a certain number of elements was expected but something /// else was found. ExpectedTuple(usize), @@ -198,47 +195,8 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { fn deserialize_any(self, visitor: V) -> Result where V: de::Visitor<'de>, { - let mut tables = Vec::new(); - let mut cur_table = Table { - at: 0, - header: Vec::new(), - values: None, - array: false, - }; - while let Some(line) = self.line()? { - match line { - Line::Table { at, mut header, array } => { - if !cur_table.header.is_empty() || cur_table.values.is_some() { - tables.push(cur_table); - } - cur_table = Table { - at: at, - header: Vec::new(), - values: Some(Vec::new()), - array: array, - }; - loop { - let part = header.next().map_err(|e| { - self.token_error(e) - }); - match part? { - Some(part) => cur_table.header.push(part), - None => break, - } - } - } - Line::KeyValue(key, value) => { - if cur_table.values.is_none() { - cur_table.values = Some(Vec::new()); - } - self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?; - } - } - } - if !cur_table.header.is_empty() || cur_table.values.is_some() { - tables.push(cur_table); - } + let mut tables = self.tables()?; visitor.visit_map(MapVisitor { values: Vec::new().into_iter(), @@ -253,6 +211,7 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { }) } + // Called when the type to deserialize is an enum, as opposed to a field in the type. fn deserialize_enum( self, _name: &'static str, @@ -261,15 +220,34 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { ) -> Result where V: de::Visitor<'de> { - if let Some(next) = self.next()? { - match next { - (_, Token::String { val, .. }) => { - visitor.visit_enum(val.into_deserializer()) - }, - _ => Err(Error::from_kind(ErrorKind::ExpectedString)) + let (value, name) = self.string_or_table()?; + match value.e { + E::String(val) => visitor.visit_enum(val.into_deserializer()), + E::InlineTable(values) => { + if values.len() != 1 { + Err(Error::from_kind(ErrorKind::Wanted { + expected: "exactly 1 element", + found: if values.is_empty() { + "zero elements" + } else { + "more than 1 element" + }, + })) + } else { + visitor.visit_enum(InlineTableDeserializer { + values: values.into_iter(), + next_value: None, + }) + } } - } else { - Err(Error::from_kind(ErrorKind::UnexpectedEof)) + E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer { + name: name.expect("Expected table header to be passed."), + value: value, + }), + e @ _ => Err(Error::from_kind(ErrorKind::Wanted { + expected: "string or table", + found: e.type_name(), + })), } } @@ -601,7 +579,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { { match self.value.e { E::String(val) => visitor.visit_enum(val.into_deserializer()), - E::InlineTable(values) | E::DottedTable(values) => { + E::InlineTable(values) => { if values.len() != 1 { Err(Error::from_kind(ErrorKind::Wanted { expected: "exactly 1 element", @@ -619,7 +597,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { } } e @ _ => Err(Error::from_kind(ErrorKind::Wanted { - expected: "string or table", + expected: "string or inline table", found: e.type_name(), })), } @@ -733,6 +711,25 @@ impl<'de> de::Deserializer<'de> for DatetimeFieldDeserializer { } } +struct DottedTableDeserializer<'a> { + name: Cow<'a, str>, + value: Value<'a>, +} + +impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> { + type Error = Error; + type Variant = TableEnumDeserializer<'de>; + + fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> + where + V: de::DeserializeSeed<'de>, + { + let (name, value) = (self.name, self.value); + seed.deserialize(StrDeserializer::new(name)) + .map(|val| (val, TableEnumDeserializer { value: value })) + } +} + struct InlineTableDeserializer<'a> { values: vec::IntoIter<(Cow<'a, str>, Value<'a>)>, next_value: Option>, @@ -762,7 +759,7 @@ impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { type Error = Error; - type Variant = Self; + type Variant = TableEnumDeserializer<'de>; fn variant_seed(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where @@ -777,18 +774,22 @@ impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { })) } }; - self.next_value = Some(value); seed.deserialize(StrDeserializer::new(key)) - .map(|val| (val, self)) + .map(|val| (val, TableEnumDeserializer { value: value })) } } -impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { +/// Deserializes table values into enum variants. +struct TableEnumDeserializer<'a> { + value: Value<'a>, +} + +impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { type Error = Error; fn unit_variant(self) -> Result<(), Self::Error> { - match self.next_value.expect("Expected value").e { + match self.value.e { E::InlineTable(values) | E::DottedTable(values) => { if values.len() == 0 { Ok(()) @@ -807,17 +808,14 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { where T: de::DeserializeSeed<'de>, { - seed.deserialize(ValueDeserializer::new( - self.next_value.expect("Expected value"), - )) + seed.deserialize(ValueDeserializer::new(self.value)) } fn tuple_variant(self, len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { - let next_value = self.next_value.expect("Expected value"); - match next_value.e { + match self.value.e { E::InlineTable(values) | E::DottedTable(values) => { let tuple_values = values .into_iter() @@ -845,8 +843,8 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { de::Deserializer::deserialize_seq( ValueDeserializer::new(Value { e: E::Array(tuple_values), - start: next_value.start, - end: next_value.end, + start: self.value.start, + end: self.value.end, }), visitor, ) @@ -870,7 +868,7 @@ impl<'de> de::VariantAccess<'de> for InlineTableDeserializer<'de> { V: de::Visitor<'de>, { de::Deserializer::deserialize_struct( - ValueDeserializer::new(self.next_value.expect("Expected value")), + ValueDeserializer::new(self.value), "", // TODO: this should be the variant name fields, visitor, @@ -907,6 +905,53 @@ impl<'a> Deserializer<'a> { self.require_newline_after_table = require; } + fn tables(&mut self) -> Result>, Error> { + let mut tables = Vec::new(); + let mut cur_table = Table { + at: 0, + header: Vec::new(), + values: None, + array: false, + }; + + while let Some(line) = self.line()? { + match line { + Line::Table { + at, + mut header, + array, + } => { + if !cur_table.header.is_empty() || cur_table.values.is_some() { + tables.push(cur_table); + } + cur_table = Table { + at: at, + header: Vec::new(), + values: Some(Vec::new()), + array: array, + }; + loop { + let part = header.next().map_err(|e| self.token_error(e)); + match part? { + Some(part) => cur_table.header.push(part), + None => break, + } + } + } + Line::KeyValue(key, value) => { + if cur_table.values.is_none() { + cur_table.values = Some(Vec::new()); + } + self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?; + } + } + } + if !cur_table.header.is_empty() || cur_table.values.is_some() { + tables.push(cur_table); + } + Ok(tables) + } + fn line(&mut self) -> Result>, Error> { loop { self.eat_whitespace()?; @@ -1029,7 +1074,56 @@ impl<'a> Deserializer<'a> { } } - fn number(&mut self, Span { start, end}: Span, s: &'a str) -> Result, Error> { + /// Returns a string or table value type. + /// + /// Used to deserialize enums. Unit enums may be represented as a string or a table, all other + /// structures (tuple, newtype, struct) must be represented as a table. + fn string_or_table(&mut self) -> Result<(Value<'a>, Option>), Error> { + match self.peek()? { + Some((_, Token::LeftBracket)) => { + let tables = self.tables()?; + if tables.len() != 1 { + return Err(Error::from_kind(ErrorKind::Wanted { + expected: "exactly 1 table", + found: if tables.is_empty() { + "zero tables" + } else { + "more than 1 table" + }, + })); + } + + let table = tables + .into_iter() + .next() + .expect("Expected exactly one table"); + let header = table + .header + .last() + .expect("Expected at least one header value for table."); + + let start = table.at; + let end = table + .values + .as_ref() + .and_then(|values| values.last()) + .map(|&(_, ref val)| val.end) + .unwrap_or_else(|| header.len()); + Ok(( + Value { + e: E::DottedTable(table.values.unwrap_or_else(Vec::new)), + start: start, + end: end, + }, + Some(header.clone()), + )) + } + Some(_) => self.value().map(|val| (val, None)), + None => Err(self.eof()), + } + } + + fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result, Error> { let to_integer = |f| Value { e: E::Integer(f), start: start, end: end }; if s.starts_with("0x") { self.integer(&s[2..], 16).map(to_integer) @@ -1324,6 +1418,18 @@ impl<'a> Deserializer<'a> { Ok(result) } + /// Stores a value in the appropriate hierachical structure positioned based on the dotted key. + /// + /// Given the following definition: `multi.part.key = "value"`, `multi` and `part` are + /// intermediate parts which are mapped to the relevant fields in the deserialized type's data + /// hierarchy. + /// + /// # Parameters + /// + /// * `key_parts`: Each segment of the dotted key, e.g. `part.one` maps to + /// `vec![Cow::Borrowed("part"), Cow::Borrowed("one")].` + /// * `value`: The parsed value. + /// * `values`: The `Vec` to store the value in. fn add_dotted_key( &self, mut key_parts: Vec>, @@ -1345,12 +1451,12 @@ impl<'a> Deserializer<'a> { None => {} } // The start/end value is somewhat misleading here. - let inline_table = Value { + let table_values = Value { e: E::DottedTable(Vec::new()), start: value.start, end: value.end, }; - values.push((key, inline_table)); + values.push((key, table_values)); let last_i = values.len() - 1; if let (_, Value { e: E::DottedTable(ref mut v), .. }) = values[last_i] { self.add_dotted_key(key_parts, value, v)?; @@ -1538,7 +1644,6 @@ impl fmt::Display for Error { ErrorKind::EmptyTableKey => "empty table key found".fmt(f)?, ErrorKind::MultilineStringKey => "multiline strings are not allowed for key".fmt(f)?, ErrorKind::Custom => self.inner.message.fmt(f)?, - ErrorKind::ExpectedString => "expected string".fmt(f)?, ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}", l)?, ErrorKind::ExpectedTupleIndex { expected, @@ -1591,7 +1696,6 @@ impl error::Error for Error { ErrorKind::EmptyTableKey => "empty table key found", ErrorKind::MultilineStringKey => "invalid multiline string for key", ErrorKind::Custom => "a custom error", - ErrorKind::ExpectedString => "expected string", ErrorKind::ExpectedTuple(_) => "expected table length", ErrorKind::ExpectedTupleIndex { .. } => "expected table key", ErrorKind::ExpectedEmptyTable => "expected empty table", diff --git a/tests/enum_external_deserialize.rs b/tests/enum_external_deserialize.rs new file mode 100644 index 0000000..76e7a4c --- /dev/null +++ b/tests/enum_external_deserialize.rs @@ -0,0 +1,153 @@ +#[macro_use] +extern crate serde_derive; +extern crate toml; + +#[derive(Debug, Deserialize, PartialEq)] +enum TheEnum { + Plain, + Tuple(i64, bool), + NewType(String), + Struct { value: i64 }, +} + +#[derive(Debug, Deserialize, PartialEq)] +struct Val { + val: TheEnum, +} + +#[derive(Debug, Deserialize, PartialEq)] +struct Multi { + enums: Vec, +} + +mod enum_unit { + use super::*; + + #[test] + fn from_str() { + assert_eq!(TheEnum::Plain, toml::from_str("\"Plain\"").unwrap()); + } + + #[test] + fn from_inline_table() { + assert_eq!(TheEnum::Plain, toml::from_str("{ Plain = {} }").unwrap()); + assert_eq!( + Val { + val: TheEnum::Plain + }, + toml::from_str("val = { Plain = {} }").unwrap() + ); + } + + #[test] + fn from_dotted_table() { + assert_eq!(TheEnum::Plain, toml::from_str("[Plain]\n").unwrap()); + } +} + +mod enum_tuple { + use super::*; + + #[test] + fn from_inline_table() { + assert_eq!( + TheEnum::Tuple(-123, true), + toml::from_str("{ Tuple = { 0 = -123, 1 = true } }").unwrap() + ); + assert_eq!( + Val { + val: TheEnum::Tuple(-123, true) + }, + toml::from_str("val = { Tuple = { 0 = -123, 1 = true } }").unwrap() + ); + } + + #[test] + fn from_dotted_table() { + assert_eq!( + TheEnum::Tuple(-123, true), + toml::from_str( + r#"[Tuple] + 0 = -123 + 1 = true + "# + ) + .unwrap() + ); + } +} + +mod enum_newtype { + use super::*; + + #[test] + fn from_inline_table() { + assert_eq!( + TheEnum::NewType("value".to_string()), + toml::from_str(r#"{ NewType = "value" }"#).unwrap() + ); + assert_eq!( + Val { + val: TheEnum::NewType("value".to_string()), + }, + toml::from_str(r#"val = { NewType = "value" }"#).unwrap() + ); + } +} + +mod enum_struct { + use super::*; + + #[test] + fn from_inline_table() { + assert_eq!( + TheEnum::Struct { value: -123 }, + toml::from_str("{ Struct = { value = -123 } }").unwrap() + ); + assert_eq!( + Val { + val: TheEnum::Struct { value: -123 } + }, + toml::from_str("val = { Struct = { value = -123 } }").unwrap() + ); + } + + #[test] + fn from_dotted_table() { + assert_eq!( + TheEnum::Struct { value: -123 }, + toml::from_str( + r#"[Struct] + value = -123 + "# + ) + .unwrap() + ); + } +} + +mod enum_array { + use super::*; + + #[test] + fn from_inline_tables() { + let toml_str = r#" + enums = [ + { Plain = {} }, + { Tuple = { 0 = -123, 1 = true } }, + { NewType = "value" }, + { Struct = { value = -123 } } + ]"#; + assert_eq!( + Multi { + enums: vec![ + TheEnum::Plain, + TheEnum::Tuple(-123, true), + TheEnum::NewType("value".to_string()), + TheEnum::Struct { value: -123 }, + ] + }, + toml::from_str(toml_str).unwrap() + ); + } +} From a0295b330845fe4b19b8ce1e8e8e4a0beb695d0d Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Mon, 22 Oct 2018 10:33:20 +1300 Subject: [PATCH 06/11] Maintain compatibility with Rust 1.15 Issue #225 --- src/de.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de.rs b/src/de.rs index 7fd1387..da30699 100644 --- a/src/de.rs +++ b/src/de.rs @@ -829,7 +829,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { }) // Fold all values into a `Vec`, or return the first error. .fold(Ok(Vec::with_capacity(len)), |result, value_result| { - result.and_then(|mut tuple_values| match value_result { + result.and_then(move |mut tuple_values| match value_result { Ok(value) => { tuple_values.push(value); Ok(tuple_values) From 7862f04d08d512e6ca4c2b1c1182876bd4143c05 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Mon, 12 Nov 2018 09:00:47 +1300 Subject: [PATCH 07/11] Added ignored tests for deserializing enums from dotted table. Issue #225 --- tests/enum_external_deserialize.rs | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/enum_external_deserialize.rs b/tests/enum_external_deserialize.rs index 76e7a4c..3309ac6 100644 --- a/tests/enum_external_deserialize.rs +++ b/tests/enum_external_deserialize.rs @@ -93,6 +93,26 @@ mod enum_newtype { toml::from_str(r#"val = { NewType = "value" }"#).unwrap() ); } + + #[test] + #[ignore = "Unimplemented: https://github.com/alexcrichton/toml-rs/pull/264#issuecomment-431707209"] + fn from_dotted_table() { + assert_eq!( + TheEnum::NewType("value".to_string()), + toml::from_str(r#"NewType = "value""#).unwrap() + ); + assert_eq!( + Val { + val: TheEnum::NewType("value".to_string()), + }, + toml::from_str( + r#"[val] + NewType = "value" + "# + ) + .unwrap() + ); + } } mod enum_struct { @@ -150,4 +170,32 @@ mod enum_array { toml::from_str(toml_str).unwrap() ); } + + #[test] + #[ignore = "Unimplemented: https://github.com/alexcrichton/toml-rs/pull/264#issuecomment-431707209"] + fn from_dotted_table() { + let toml_str = r#"[[enums]] + Plain = {} + + [[enums]] + Tuple = { 0 = -123, 1 = true } + + [[enums]] + NewType = "value" + + [[enums]] + Struct = { value = -123 } + "#; + assert_eq!( + Multi { + enums: vec![ + TheEnum::Plain, + TheEnum::Tuple(-123, true), + TheEnum::NewType("value".to_string()), + TheEnum::Struct { value: -123 }, + ] + }, + toml::from_str(toml_str).unwrap() + ); + } } From 1656e2e51d72063c83b8ee6ccb1fd660146054c5 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Mon, 12 Nov 2018 10:09:30 +1300 Subject: [PATCH 08/11] Error when deserializing struct if encountering unknown fields. Issue #225 --- src/de.rs | 41 ++++++++++++++++++++++++++++++ tests/enum_external_deserialize.rs | 40 +++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/de.rs b/src/de.rs index da30699..c7e8b34 100644 --- a/src/de.rs +++ b/src/de.rs @@ -178,6 +178,16 @@ enum ErrorKind { /// Dotted key attempted to extend something that is not a table. DottedKeyInvalidType, + /// An unexpected key was encountered. + /// + /// Used when deserializing a struct with a limited set of fields. + UnexpectedKeys { + /// The unexpected keys. + keys: Vec, + /// Keys that may be specified. + available: &'static [&'static str], + }, + #[doc(hidden)] __Nonexhaustive, } @@ -547,6 +557,28 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { } } + match &self.value.e { + E::InlineTable(values) | E::DottedTable(values) => { + let extra_fields = values.iter() + .filter_map(|(key, _val)| { + if !fields.contains(&&(**key)) { + Some(key.clone()) + } else { + None + } + }) + .collect::>>(); + + if !extra_fields.is_empty() { + return Err(Error::from_kind(ErrorKind::UnexpectedKeys { + keys: extra_fields.iter().map(|k| k.to_string()).collect::>(), + available: fields, + })); + } + } + _ => {} + } + if name == spanned::NAME && fields == &[spanned::START, spanned::END, spanned::VALUE] { let start = self.value.start; let end = self.value.end; @@ -1653,6 +1685,14 @@ impl fmt::Display for Error { ErrorKind::DottedKeyInvalidType => { "dotted key attempted to extend non-table type".fmt(f)? } + ErrorKind::UnexpectedKeys { ref keys, available } => { + write!( + f, + "unexpected keys in table: `{:?}`, available keys: `{:?}`", + keys, + available + )? + } ErrorKind::__Nonexhaustive => panic!(), } @@ -1700,6 +1740,7 @@ impl error::Error for Error { ErrorKind::ExpectedTupleIndex { .. } => "expected table key", ErrorKind::ExpectedEmptyTable => "expected empty table", ErrorKind::DottedKeyInvalidType => "dotted key invalid type", + ErrorKind::UnexpectedKeys { .. } => "unexpected keys in table", ErrorKind::__Nonexhaustive => panic!(), } } diff --git a/tests/enum_external_deserialize.rs b/tests/enum_external_deserialize.rs index 3309ac6..e35f06a 100644 --- a/tests/enum_external_deserialize.rs +++ b/tests/enum_external_deserialize.rs @@ -20,6 +20,46 @@ struct Multi { enums: Vec, } +#[test] +fn invalid_variant_returns_error_with_good_message_string() { + let error = toml::from_str::("\"NonExistent\"") + .expect_err("Expected deserialization to fail."); + + assert_eq!( + error.to_string(), + "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`" + ); +} + +#[test] +fn invalid_variant_returns_error_with_good_message_inline_table() { + let error = toml::from_str::("{ NonExistent = {} }") + .expect_err("Expected deserialization to fail."); + assert_eq!( + error.to_string(), + "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`" + ); +} + +#[test] +fn extra_field_returns_expected_empty_table_error() { + let error = toml::from_str::("{ Plain = { extra_field = 404 } }") + .expect_err("Expected deserialization to fail."); + + assert_eq!(error.to_string(), "expected empty table"); +} + +#[test] +fn extra_field_returns_expected_empty_table_error_struct_variant() { + let error = toml::from_str::("{ Struct = { value = 123, extra_0 = 0, extra_1 = 1 } }") + .expect_err("Expected deserialization to fail."); + + assert_eq!( + error.to_string(), + r#"unexpected keys in table: `["extra_0", "extra_1"]`, available keys: `["value"]`"# + ); +} + mod enum_unit { use super::*; From 5bcf6f7cb64469339527a0f10563006c31854682 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Sat, 17 Nov 2018 09:18:43 +1300 Subject: [PATCH 09/11] Maintain compatibility with Rust 1.15 Issue #225 --- src/de.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de.rs b/src/de.rs index c7e8b34..869f7ae 100644 --- a/src/de.rs +++ b/src/de.rs @@ -558,9 +558,9 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { } match &self.value.e { - E::InlineTable(values) | E::DottedTable(values) => { + &E::InlineTable(ref values) | &E::DottedTable(ref values) => { let extra_fields = values.iter() - .filter_map(|(key, _val)| { + .filter_map(|(ref key, ref _val)| { if !fields.contains(&&(**key)) { Some(key.clone()) } else { From 752bf0d4a1ff154dfb03a2bf7d2ff21359c2b2e2 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Sat, 17 Nov 2018 09:37:02 +1300 Subject: [PATCH 10/11] Only validate table keys against fields when deserializing enum. Issue #225 --- src/de.rs | 45 +++++++++++++++++++++++++++------------------ 1 file 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::>>(); + 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::>>(); - if !extra_fields.is_empty() { - return Err(Error::from_kind(ErrorKind::UnexpectedKeys { - keys: extra_fields.iter().map(|k| k.to_string()).collect::>(), - available: fields, - })); + if !extra_fields.is_empty() { + return Err(Error::from_kind(ErrorKind::UnexpectedKeys { + keys: extra_fields.iter().map(|k| k.to_string()).collect::>(), + 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, From cd6373a9abd0e9a147d9b17e4f93aae5ae14e567 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Sat, 17 Nov 2018 13:47:23 +1300 Subject: [PATCH 11/11] Maintain backward compatibility with Rust 1.15, take 3. --- src/de.rs | 3 ++- tests/enum_external_deserialize.rs | 11 ++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/de.rs b/src/de.rs index 9d5a417..9b58606 100644 --- a/src/de.rs +++ b/src/de.rs @@ -568,7 +568,8 @@ 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)| { + .filter_map(|key_value| { + let (ref key, ref _val) = *key_value; if !fields.contains(&&(**key)) { Some(key.clone()) } else { diff --git a/tests/enum_external_deserialize.rs b/tests/enum_external_deserialize.rs index e35f06a..3c38580 100644 --- a/tests/enum_external_deserialize.rs +++ b/tests/enum_external_deserialize.rs @@ -22,8 +22,7 @@ struct Multi { #[test] fn invalid_variant_returns_error_with_good_message_string() { - let error = toml::from_str::("\"NonExistent\"") - .expect_err("Expected deserialization to fail."); + let error = toml::from_str::("\"NonExistent\"").unwrap_err(); assert_eq!( error.to_string(), @@ -33,8 +32,7 @@ fn invalid_variant_returns_error_with_good_message_string() { #[test] fn invalid_variant_returns_error_with_good_message_inline_table() { - let error = toml::from_str::("{ NonExistent = {} }") - .expect_err("Expected deserialization to fail."); + let error = toml::from_str::("{ NonExistent = {} }").unwrap_err(); assert_eq!( error.to_string(), "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`" @@ -43,8 +41,7 @@ fn invalid_variant_returns_error_with_good_message_inline_table() { #[test] fn extra_field_returns_expected_empty_table_error() { - let error = toml::from_str::("{ Plain = { extra_field = 404 } }") - .expect_err("Expected deserialization to fail."); + let error = toml::from_str::("{ Plain = { extra_field = 404 } }").unwrap_err(); assert_eq!(error.to_string(), "expected empty table"); } @@ -52,7 +49,7 @@ fn extra_field_returns_expected_empty_table_error() { #[test] fn extra_field_returns_expected_empty_table_error_struct_variant() { let error = toml::from_str::("{ Struct = { value = 123, extra_0 = 0, extra_1 = 1 } }") - .expect_err("Expected deserialization to fail."); + .unwrap_err(); assert_eq!( error.to_string(),