Add option to enable old behavior

Cargo will use this in the interim.
This commit is contained in:
Alex Crichton 2016-05-12 11:28:32 -07:00
parent 50dfc8ac79
commit 1ed6801137

View file

@ -89,6 +89,7 @@ impl Value {
pub struct Parser<'a> { pub struct Parser<'a> {
input: &'a str, input: &'a str,
cur: str::CharIndices<'a>, cur: str::CharIndices<'a>,
require_newline_after_table: bool,
/// A list of all errors which have occurred during parsing. /// A list of all errors which have occurred during parsing.
/// ///
@ -138,6 +139,7 @@ impl<'a> Parser<'a> {
input: s, input: s,
cur: s.char_indices(), cur: s.char_indices(),
errors: Vec::new(), errors: Vec::new(),
require_newline_after_table: true,
} }
} }
@ -155,6 +157,16 @@ impl<'a> Parser<'a> {
(self.input.lines().count(), 0) (self.input.lines().count(), 0)
} }
/// Historical versions of toml-rs accidentally allowed a newline after a
/// table definition, but the TOML spec requires a newline after a table
/// definition header.
///
/// This option can be set to `false` (the default is `true`) to emulate
/// this behavior for backwards compatibility with older toml-rs versions.
pub fn set_require_newline_after_table(&mut self, require: bool) {
self.require_newline_after_table = require;
}
fn next_pos(&self) -> usize { fn next_pos(&self) -> usize {
self.cur.clone().next().map(|p| p.0).unwrap_or(self.input.len()) self.cur.clone().next().map(|p| p.0).unwrap_or(self.input.len())
} }
@ -271,6 +283,7 @@ impl<'a> Parser<'a> {
values: BTreeMap::new(), values: BTreeMap::new(),
defined: true, defined: true,
}; };
if self.require_newline_after_table {
self.ws(); self.ws();
self.comment(); self.comment();
if !self.newline() { if !self.newline() {
@ -281,6 +294,7 @@ impl<'a> Parser<'a> {
}); });
return None return None
} }
}
if !self.values(&mut table) { return None } if !self.values(&mut table) { return None }
if array { if array {
self.insert_array(&mut ret, &keys, Value::Table(table), self.insert_array(&mut ret, &keys, Value::Table(table),