Clean up more style
This commit is contained in:
parent
68924534e2
commit
88461157f2
|
@ -9,38 +9,31 @@ macro_rules! try {
|
||||||
($e:expr) => (match $e { Some(s) => s, None => return None })
|
($e:expr) => (match $e { Some(s) => s, None => return None })
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// We redefine Array, Table and Value, because we need to keep track of
|
||||||
* We redefine Array, Table and Value, because we need to keep track of
|
// encountered table definitions, eg when parsing:
|
||||||
* encountered table definitions, eg when parsing:
|
//
|
||||||
* [a]
|
// [a]
|
||||||
* [a.b]
|
// [a.b]
|
||||||
* [a]
|
// [a]
|
||||||
* we have to error out on redefinition of [a].
|
//
|
||||||
* This bit of data is impossible to represent in the user-consumed table
|
// we have to error out on redefinition of [a]. This bit of data is difficult to
|
||||||
* without breaking compatibility, so we use one AST structure during parsing
|
// track in a side table so we just have a "stripped down" AST to work with
|
||||||
* and expose another (after running convert(...) on it) to the user.
|
// which has the relevant metadata fields in it.
|
||||||
*/
|
|
||||||
type Array = Vec<Value>;
|
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
|
||||||
// If the bool flag is true, the table was explicitly defined
|
|
||||||
// e.g. in a toml document: `[a.b] foo = "bar"`, Table `a` would be false,
|
|
||||||
// where table `b` (contained inside `a`) would be true.
|
|
||||||
struct TomlTable(BTreeMap<String, Value>, bool);
|
struct TomlTable(BTreeMap<String, Value>, bool);
|
||||||
|
|
||||||
impl TomlTable {
|
impl TomlTable {
|
||||||
fn convert(self) -> super::Table {
|
fn convert(self) -> super::Table {
|
||||||
self.0.into_iter().map(|(k,v)| (k,v.convert())).collect()
|
self.0.into_iter().map(|(k,v)| (k,v.convert())).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
|
||||||
enum Value {
|
enum Value {
|
||||||
String(String),
|
String(String),
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Datetime(String),
|
Datetime(String),
|
||||||
Array(Array),
|
Array(Vec<Value>),
|
||||||
Table(TomlTable),
|
Table(TomlTable),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,10 +50,6 @@ impl Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_table<'a>(&'a self) -> Option<&'a TomlTable> {
|
|
||||||
match *self { Value::Table(ref s) => Some(s), _ => None }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn same_type(&self, other: &Value) -> bool {
|
fn same_type(&self, other: &Value) -> bool {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&Value::String(..), &Value::String(..)) |
|
(&Value::String(..), &Value::String(..)) |
|
||||||
|
@ -267,7 +256,7 @@ impl<'a> Parser<'a> {
|
||||||
if keys.len() == 0 { return None }
|
if keys.len() == 0 { return None }
|
||||||
|
|
||||||
// Build the section table
|
// Build the section table
|
||||||
let mut table = TomlTable(BTreeMap::new(), false);
|
let mut table = TomlTable(BTreeMap::new(), true);
|
||||||
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),
|
||||||
|
@ -864,40 +853,33 @@ impl<'a> Parser<'a> {
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
let key = format!("{}", key);
|
if !into.0.contains_key(key) {
|
||||||
let mut added = false;
|
into.0.insert(key.to_owned(), Value::Table(value));
|
||||||
if !into.0.contains_key(&key) {
|
return
|
||||||
into.0.insert(key.clone(), Value::Table(TomlTable(BTreeMap::new(), true)));
|
|
||||||
added = true;
|
|
||||||
}
|
}
|
||||||
match into.0.get_mut(&key) {
|
if let Value::Table(ref mut table) = *into.0.get_mut(key).unwrap() {
|
||||||
Some(&mut Value::Table(ref mut table)) => {
|
if table.1 {
|
||||||
let any_tables = table.0.values().any(|v| v.as_table().is_some());
|
|
||||||
if !added && (!any_tables || table.1) {
|
|
||||||
self.errors.push(ParserError {
|
|
||||||
lo: key_lo,
|
|
||||||
hi: key_lo + key.len(),
|
|
||||||
desc: format!("redefinition of table `{}`", key),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (k, v) in value.0.into_iter() {
|
|
||||||
if table.0.insert(k.clone(), v).is_some() {
|
|
||||||
self.errors.push(ParserError {
|
|
||||||
lo: key_lo,
|
|
||||||
hi: key_lo + key.len(),
|
|
||||||
desc: format!("duplicate key `{}` in table", k),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(_) => {
|
|
||||||
self.errors.push(ParserError {
|
self.errors.push(ParserError {
|
||||||
lo: key_lo,
|
lo: key_lo,
|
||||||
hi: key_lo + key.len(),
|
hi: key_lo + key.len(),
|
||||||
desc: format!("duplicate key `{}` in table", key),
|
desc: format!("redefinition of table `{}`", key),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
None => {}
|
for (k, v) in value.0 {
|
||||||
|
if table.0.insert(k.clone(), v).is_some() {
|
||||||
|
self.errors.push(ParserError {
|
||||||
|
lo: key_lo,
|
||||||
|
hi: key_lo + key.len(),
|
||||||
|
desc: format!("duplicate key `{}` in table", k),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.errors.push(ParserError {
|
||||||
|
lo: key_lo,
|
||||||
|
hi: key_lo + key.len(),
|
||||||
|
desc: format!("duplicate key `{}` in table", key),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue