From d02c02d995630ebfa234858fe2410c7fafa65cda Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 8 Feb 2017 21:36:38 -0800 Subject: [PATCH] Implement Deserializer::set_require_newline_after_table --- src/de.rs | 32 ++++++++++++++++++++++++++------ tests/backcompat.rs | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/backcompat.rs diff --git a/src/de.rs b/src/de.rs index 7cb0410..1406f35 100644 --- a/src/de.rs +++ b/src/de.rs @@ -581,7 +581,7 @@ impl<'a> Deserializer<'a> { Deserializer { tokens: Tokenizer::new(input), input: input, - require_newline_after_table: false, + require_newline_after_table: true, } } @@ -626,8 +626,22 @@ impl<'a> Deserializer<'a> { let start = self.tokens.current(); self.expect(Token::LeftBracket)?; let array = self.eat(Token::LeftBracket)?; - let ret = Header::new(self.tokens.clone(), array); - self.tokens.skip_to_newline(); + let ret = Header::new(self.tokens.clone(), + array, + self.require_newline_after_table); + if self.require_newline_after_table { + self.tokens.skip_to_newline(); + } else { + loop { + match self.next()? { + Some(Token::RightBracket) | + Some(Token::Newline) | + None => break, + _ => {} + } + } + self.eat_whitespace()?; + } Ok(Line::Table { at: start, header: ret, array: array }) } @@ -1134,15 +1148,19 @@ enum Line<'a> { struct Header<'a> { first: bool, array: bool, + require_newline_after_table: bool, tokens: Tokenizer<'a>, } impl<'a> Header<'a> { - fn new(tokens: Tokenizer<'a>, array: bool) -> Header<'a> { + fn new(tokens: Tokenizer<'a>, + array: bool, + require_newline_after_table: bool) -> Header<'a> { Header { first: true, array: array, tokens: tokens, + require_newline_after_table: require_newline_after_table, } } @@ -1160,8 +1178,10 @@ impl<'a> Header<'a> { } self.tokens.eat_whitespace()?; - if !self.tokens.eat_comment()? { - self.tokens.eat_newline_or_eof()?; + if self.require_newline_after_table { + if !self.tokens.eat_comment()? { + self.tokens.eat_newline_or_eof()?; + } } Ok(None) } diff --git a/tests/backcompat.rs b/tests/backcompat.rs new file mode 100644 index 0000000..33dbfa5 --- /dev/null +++ b/tests/backcompat.rs @@ -0,0 +1,14 @@ +extern crate toml; +extern crate serde; + +use serde::de::Deserialize; + +#[test] +fn main() { + assert!("[a] foo = 1".parse::().is_err()); + + let mut d = toml::de::Deserializer::new("[a] foo = 1"); + d.set_require_newline_after_table(false); + let value = toml::Value::deserialize(&mut d).unwrap(); + assert_eq!(value["a"]["foo"].as_integer(), Some(1)); +}