Migrate to a TreeMap for determinism
This commit is contained in:
parent
bec6b768ec
commit
b4a4ed72d7
|
@ -1,5 +1,5 @@
|
||||||
use std::char;
|
use std::char;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{TreeMap, HashSet};
|
||||||
use std::num::FromStrRadix;
|
use std::num::FromStrRadix;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ impl<'a> Parser<'a> {
|
||||||
/// If an error occurs, the `errors` field of this parser can be consulted
|
/// If an error occurs, the `errors` field of this parser can be consulted
|
||||||
/// to determine the cause of the parse failure.
|
/// to determine the cause of the parse failure.
|
||||||
pub fn parse(&mut self) -> Option<Table> {
|
pub fn parse(&mut self) -> Option<Table> {
|
||||||
let mut ret = HashMap::new();
|
let mut ret = TreeMap::new();
|
||||||
loop {
|
loop {
|
||||||
self.ws();
|
self.ws();
|
||||||
match self.cur.clone().next() {
|
match self.cur.clone().next() {
|
||||||
|
@ -172,7 +172,7 @@ impl<'a> Parser<'a> {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut table = HashMap::new();
|
let mut table = TreeMap::new();
|
||||||
if !self.values(&mut table) { return None }
|
if !self.values(&mut table) { return None }
|
||||||
if array {
|
if array {
|
||||||
self.insert_array(&mut ret, section, Table(table), start)
|
self.insert_array(&mut ret, section, Table(table), start)
|
||||||
|
@ -559,7 +559,7 @@ impl<'a> Parser<'a> {
|
||||||
let tmp = cur;
|
let tmp = cur;
|
||||||
|
|
||||||
if tmp.contains_key(&part) {
|
if tmp.contains_key(&part) {
|
||||||
match *tmp.get_mut(&part) {
|
match *tmp.find_mut(&part).unwrap() {
|
||||||
Table(ref mut table) => {
|
Table(ref mut table) => {
|
||||||
cur = table;
|
cur = table;
|
||||||
continue
|
continue
|
||||||
|
@ -592,8 +592,8 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize an empty table as part of this sub-key
|
// Initialize an empty table as part of this sub-key
|
||||||
tmp.insert(part.clone(), Table(HashMap::new()));
|
tmp.insert(part.clone(), Table(TreeMap::new()));
|
||||||
match *tmp.get_mut(&part) {
|
match *tmp.find_mut(&part).unwrap() {
|
||||||
Table(ref mut inner) => cur = inner,
|
Table(ref mut inner) => cur = inner,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
@ -618,7 +618,7 @@ impl<'a> Parser<'a> {
|
||||||
};
|
};
|
||||||
let key = key.to_string();
|
let key = key.to_string();
|
||||||
if !into.contains_key(&key) {
|
if !into.contains_key(&key) {
|
||||||
into.insert(key.clone(), Table(HashMap::new()));
|
into.insert(key.clone(), Table(TreeMap::new()));
|
||||||
}
|
}
|
||||||
match into.find_mut(&key) {
|
match into.find_mut(&key) {
|
||||||
Some(&Table(ref mut table)) => {
|
Some(&Table(ref mut table)) => {
|
||||||
|
@ -653,7 +653,7 @@ impl<'a> Parser<'a> {
|
||||||
if !into.contains_key(&key) {
|
if !into.contains_key(&key) {
|
||||||
into.insert(key.clone(), Array(Vec::new()));
|
into.insert(key.clone(), Array(Vec::new()));
|
||||||
}
|
}
|
||||||
match *into.get_mut(&key) {
|
match *into.find_mut(&key).unwrap() {
|
||||||
Array(ref mut vec) => {
|
Array(ref mut vec) => {
|
||||||
match vec.as_slice().head() {
|
match vec.as_slice().head() {
|
||||||
Some(ref v) if !v.same_type(&value) => {
|
Some(ref v) if !v.same_type(&value) => {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::TreeMap;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ pub fn encode_str<T: serialize::Encodable<Encoder, Error>>(t: &T) -> String {
|
||||||
impl Encoder {
|
impl Encoder {
|
||||||
/// Constructs a new encoder which will emit to the given output stream.
|
/// Constructs a new encoder which will emit to the given output stream.
|
||||||
pub fn new() -> Encoder {
|
pub fn new() -> Encoder {
|
||||||
Encoder { state: Start, toml: HashMap::new() }
|
Encoder { state: Start, toml: TreeMap::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_value(&mut self, v: Value) -> Result<(), Error> {
|
fn emit_value(&mut self, v: Value) -> Result<(), Error> {
|
||||||
|
@ -667,8 +667,8 @@ impl serialize::Decoder<DecodeError> for Decoder {
|
||||||
{
|
{
|
||||||
match self.toml {
|
match self.toml {
|
||||||
Some(Table(ref table)) => {
|
Some(Table(ref table)) => {
|
||||||
match table.keys().skip(idx).next() {
|
match table.iter().skip(idx).next() {
|
||||||
Some(key) => {
|
Some((key, _)) => {
|
||||||
f(&mut self.sub_decoder(Some(String(key.to_string())),
|
f(&mut self.sub_decoder(Some(String(key.to_string())),
|
||||||
key.as_slice()))
|
key.as_slice()))
|
||||||
}
|
}
|
||||||
|
@ -684,10 +684,10 @@ impl serialize::Decoder<DecodeError> for Decoder {
|
||||||
{
|
{
|
||||||
match self.toml {
|
match self.toml {
|
||||||
Some(Table(ref table)) => {
|
Some(Table(ref table)) => {
|
||||||
match table.values().skip(idx).next() {
|
match table.iter().skip(idx).next() {
|
||||||
Some(key) => {
|
Some((_, value)) => {
|
||||||
// XXX: this shouldn't clone
|
// XXX: this shouldn't clone
|
||||||
f(&mut self.sub_decoder(Some(key.clone()), ""))
|
f(&mut self.sub_decoder(Some(value.clone()), ""))
|
||||||
}
|
}
|
||||||
None => Err(self.err(ExpectedMapElement(idx))),
|
None => Err(self.err(ExpectedMapElement(idx))),
|
||||||
}
|
}
|
||||||
|
@ -743,7 +743,7 @@ impl fmt::Show for DecodeError {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{TreeMap, HashSet};
|
||||||
use serialize::{Encodable, Decodable};
|
use serialize::{Encodable, Decodable};
|
||||||
|
|
||||||
use super::{Encoder, Decoder, DecodeError};
|
use super::{Encoder, Decoder, DecodeError};
|
||||||
|
@ -761,7 +761,7 @@ mod tests {
|
||||||
}) )
|
}) )
|
||||||
|
|
||||||
macro_rules! map( ($($k:ident: $v:expr),*) => ({
|
macro_rules! map( ($($k:ident: $v:expr),*) => ({
|
||||||
let mut _m = HashMap::new();
|
let mut _m = TreeMap::new();
|
||||||
$(_m.insert(stringify!($k).to_string(), $v);)*
|
$(_m.insert(stringify!($k).to_string(), $v);)*
|
||||||
_m
|
_m
|
||||||
}) )
|
}) )
|
||||||
|
@ -785,7 +785,7 @@ mod tests {
|
||||||
assert_eq!(encode!(v), map! { a_b: Integer(2) });
|
assert_eq!(encode!(v), map! { a_b: Integer(2) });
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
|
|
||||||
let mut m = HashMap::new();
|
let mut m = TreeMap::new();
|
||||||
m.insert("a-b".to_string(), Integer(2));
|
m.insert("a-b".to_string(), Integer(2));
|
||||||
assert_eq!(v, decode!(Table(encode!(v))));
|
assert_eq!(v, decode!(Table(encode!(v))));
|
||||||
}
|
}
|
||||||
|
@ -884,13 +884,13 @@ mod tests {
|
||||||
fn hashmap() {
|
fn hashmap() {
|
||||||
#[deriving(Encodable, Decodable, PartialEq, Show)]
|
#[deriving(Encodable, Decodable, PartialEq, Show)]
|
||||||
struct Foo {
|
struct Foo {
|
||||||
map: HashMap<String, int>,
|
map: TreeMap<String, int>,
|
||||||
set: HashSet<char>,
|
set: HashSet<char>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let v = Foo {
|
let v = Foo {
|
||||||
map: {
|
map: {
|
||||||
let mut m = HashMap::new();
|
let mut m = TreeMap::new();
|
||||||
m.insert("foo".to_string(), 10);
|
m.insert("foo".to_string(), 10);
|
||||||
m.insert("bar".to_string(), 4);
|
m.insert("bar".to_string(), 4);
|
||||||
m
|
m
|
||||||
|
@ -1084,7 +1084,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn unused_fields4() {
|
fn unused_fields4() {
|
||||||
#[deriving(Encodable, Decodable, PartialEq, Show)]
|
#[deriving(Encodable, Decodable, PartialEq, Show)]
|
||||||
struct Foo { a: HashMap<String, String> }
|
struct Foo { a: TreeMap<String, String> }
|
||||||
|
|
||||||
let v = Foo { a: map! { a: "foo".to_string() } };
|
let v = Foo { a: map! { a: "foo".to_string() } };
|
||||||
let mut d = Decoder::new(Table(map! {
|
let mut d = Decoder::new(Table(map! {
|
||||||
|
|
|
@ -101,10 +101,10 @@ impl<'a, 'b> Printer<'a, 'b> {
|
||||||
#[allow(warnings)]
|
#[allow(warnings)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table};
|
use {Value, String, Integer, Float, Boolean, Datetime, Array, Table};
|
||||||
use std::collections::HashMap;
|
use std::collections::TreeMap;
|
||||||
|
|
||||||
macro_rules! map( ($($k:expr: $v:expr),*) => ({
|
macro_rules! map( ($($k:expr: $v:expr),*) => ({
|
||||||
let mut _m = HashMap::new();
|
let mut _m = TreeMap::new();
|
||||||
$(_m.insert($k.to_string(), $v);)*
|
$(_m.insert($k.to_string(), $v);)*
|
||||||
_m
|
_m
|
||||||
}) )
|
}) )
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
|
|
||||||
extern crate serialize;
|
extern crate serialize;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::TreeMap;
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
|
|
||||||
pub use parser::{Parser, Error};
|
pub use parser::{Parser, Error};
|
||||||
|
@ -70,7 +70,7 @@ pub enum Value {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Array = Vec<Value>;
|
pub type Array = Vec<Value>;
|
||||||
pub type Table = HashMap<String, Value>;
|
pub type Table = TreeMap<String, Value>;
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
/// Tests whether this and another value have the same type.
|
/// Tests whether this and another value have the same type.
|
||||||
|
@ -176,7 +176,7 @@ impl Value {
|
||||||
for key in path.split('.') {
|
for key in path.split('.') {
|
||||||
match cur_value {
|
match cur_value {
|
||||||
&Table(ref hm) => {
|
&Table(ref hm) => {
|
||||||
match hm.find_equiv(&key) {
|
match hm.find_with(|k| key.cmp(&k.as_slice())) {
|
||||||
Some(v) => cur_value = v,
|
Some(v) => cur_value = v,
|
||||||
_ => return None
|
_ => return None
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue