1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::ops::Deref;
use std::io::{Read};
use std::collections::HashMap;
use reader::{Reader, Event, Item};
use super::{Entry, Statement, Value, Array};
use {Result as Res};

/// A table of entries.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Table(HashMap<String, Entry>);

fn insert(map: &mut HashMap<String, Entry>, key: String, value: Entry) {
	if !map.contains_key(&key) {
		map.insert(key, value);
		return;
	}

	if let Some(&mut Entry::Array(ref mut array)) = map.get_mut(&key) {
		array.push(value);
		return;
	}

	let mut array = Array::from(map.remove(&key).unwrap());
	array.push(value);

	map.insert(key, array.into());
}

impl Table {
	/// Load a table from the given `Reader`.
	pub fn load<R: Read>(reader: &mut Reader<R>) -> Res<Table> {
		let mut map = HashMap::new();

		loop {
			match try!(reader.event()) {
				Event::Entry(Item::Statement(..), _) =>
					(),

				Event::Entry(Item::Value(key), Item::Statement(value)) =>
					insert(&mut map, key, Statement::from(value).into()),

				Event::Entry(Item::Value(key), Item::Value(value)) =>
					insert(&mut map, key, Value::from(value).into()),

				Event::GroupStart(name) =>
					insert(&mut map, name, try!(Table::load(reader)).into()),

				Event::GroupEnd | Event::End =>
					break
			}
		}

		return Ok(Table(map));
	}
}

impl Into<Entry> for Table {
	fn into(self) -> Entry {
		Entry::Table(self)
	}
}

impl Deref for Table {
	type Target = HashMap<String, Entry>;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}