aboutsummaryrefslogtreecommitdiff
path: root/src/encoder
diff options
context:
space:
mode:
authorAndrzej Janik <vosen@vosen.pl>2015-06-04 20:23:46 +0200
committerAndrzej Janik <vosen@vosen.pl>2015-06-04 20:23:46 +0200
commit2fcd829b1d9c70d0981411b4f4adca9124985b54 (patch)
tree1487d67764c99542b6fc7df30c60dc89719dd370 /src/encoder
parent971700148b58df921f070015febd8706c66edded (diff)
downloadmilf-rs-2fcd829b1d9c70d0981411b4f4adca9124985b54.tar.gz
milf-rs-2fcd829b1d9c70d0981411b4f4adca9124985b54.zip
Disallow table redefinitions
Diffstat (limited to 'src/encoder')
-rw-r--r--src/encoder/mod.rs8
-rw-r--r--src/encoder/rustc_serialize.rs11
2 files changed, 11 insertions, 8 deletions
diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs
index 21185f4..ea8ef6a 100644
--- a/src/encoder/mod.rs
+++ b/src/encoder/mod.rs
@@ -31,7 +31,7 @@ use {Value, Table};
/// let mut e = Encoder::new();
/// my_struct.encode(&mut e).unwrap();
///
-/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4)))
+/// assert_eq!(e.toml.0.get(&"foo".to_string()), Some(&Value::Integer(4)))
/// # }
/// ```
pub struct Encoder {
@@ -73,12 +73,12 @@ enum State {
impl Encoder {
/// Constructs a new encoder which will emit to the given output stream.
pub fn new() -> Encoder {
- Encoder { state: State::Start, toml: BTreeMap::new() }
+ Encoder { state: State::Start, toml: Table(BTreeMap::new(), false) }
}
fn emit_value(&mut self, v: Value) -> Result<(), Error> {
match mem::replace(&mut self.state, State::Start) {
- State::NextKey(key) => { self.toml.insert(key, v); Ok(()) }
+ State::NextKey(key) => { self.toml.0.insert(key, v); Ok(()) }
State::NextArray(mut vec) => {
// TODO: validate types
vec.push(v);
@@ -122,7 +122,7 @@ impl Encoder {
State::NextKey(key) => {
let mut nested = Encoder::new();
try!(f(&mut nested));
- self.toml.insert(key, Value::Table(nested.toml));
+ self.toml.0.insert(key, Value::Table(nested.toml));
Ok(())
}
State::NextArray(mut arr) => {
diff --git a/src/encoder/rustc_serialize.rs b/src/encoder/rustc_serialize.rs
index ab5e90f..830eb5e 100644
--- a/src/encoder/rustc_serialize.rs
+++ b/src/encoder/rustc_serialize.rs
@@ -193,8 +193,8 @@ impl rustc_serialize::Encodable for Value {
})
}
Value::Table(ref t) => {
- e.emit_map(t.len(), |e| {
- for (i, (key, value)) in t.iter().enumerate() {
+ e.emit_map(t.0.len(), |e| {
+ for (i, (key, value)) in t.0.iter().enumerate() {
try!(e.emit_map_elt_key(i, |e| e.emit_str(key)));
try!(e.emit_map_elt_val(i, |e| value.encode(e)));
}
@@ -212,6 +212,7 @@ mod tests {
use {Encoder, Decoder, DecodeError};
use Value;
+ use Table as TomlTable;
use Value::{Table, Integer, Array, Float};
macro_rules! encode( ($t:expr) => ({
@@ -228,7 +229,7 @@ mod tests {
macro_rules! map( ($($k:ident, $v:expr),*) => ({
let mut _m = BTreeMap::new();
$(_m.insert(stringify!($k).to_string(), $v);)*
- _m
+ TomlTable::new(_m)
}) );
#[test]
@@ -577,7 +578,9 @@ mod tests {
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)]
struct Foo { a: BTreeMap<String, String> }
- let v = Foo { a: map! { a, "foo".to_string() } };
+ let mut v = Foo { a: BTreeMap::new() };
+ v.a.insert("a".to_string(), "foo".to_string());
+
let mut d = Decoder::new(Table(map! {
a, Table(map! {
a, Value::String("foo".to_string())