diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 304bac6..0f13b81 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -35,7 +35,7 @@ use {Value, Table}; /// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4))) /// # } /// ``` -#[derive(Default)] +#[derive(Default, Debug)] pub struct Encoder { /// Output TOML that is emitted. The current version of this encoder forces /// the top-level representation of a structure to be a table. @@ -66,8 +66,9 @@ pub enum Error { Custom(String), } -#[derive(PartialEq)] -enum State { +#[derive(PartialEq, Debug)] +#[doc(hidden)] +pub enum State { Start, NextKey(String), NextArray(Vec), @@ -115,8 +116,16 @@ impl Encoder { fn seq(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error> { - let old = mem::replace(&mut self.state, State::NextArray(Vec::new())); + let old = try!(self.seq_begin()); try!(f(self)); + self.seq_end(old) + } + + fn seq_begin(&mut self) -> Result { + Ok(mem::replace(&mut self.state, State::NextArray(Vec::new()))) + } + + fn seq_end(&mut self, old: State) -> Result<(), Error> { match mem::replace(&mut self.state, old) { State::NextArray(v) => self.emit_value(Value::Array(v)), _ => unreachable!(), @@ -145,6 +154,30 @@ impl Encoder { } } + fn table_begin(&mut self) -> Result { + match self.state { + State::NextMapKey => Err(Error::InvalidMapKeyLocation), + _ => Ok(mem::replace(self, Encoder::new())) + } + } + + fn table_end(&mut self, mut state: Self) -> Result<(), Error> { + match state.state { + State::NextKey(key) => { + mem::swap(&mut self.toml, &mut state.toml); + self.toml.insert(key, Value::Table(state.toml)); + }, + State::NextArray(mut arr) => { + mem::swap(&mut self.toml, &mut state.toml); + arr.push(Value::Table(state.toml)); + self.state = State::NextArray(arr); + }, + State::Start => {}, + State::NextMapKey => unreachable!(), + } + Ok(()) + } + fn table_key(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error> { diff --git a/src/encoder/serde.rs b/src/encoder/serde.rs index a782cd2..49a6e2d 100644 --- a/src/encoder/serde.rs +++ b/src/encoder/serde.rs @@ -1,9 +1,11 @@ use serde::ser; use Value; -use super::{Encoder, Error}; +use super::{Encoder, Error, State}; impl ser::Serializer for Encoder { type Error = Error; + type MapState = Self; + type SeqState = State; fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { self.emit_value(Value::Boolean(v)) @@ -34,26 +36,19 @@ impl ser::Serializer for Encoder { { value.serialize(self) } - fn serialize_seq(&mut self, mut visitor: V) -> Result<(), Error> - where V: ser::SeqVisitor - { - self.seq(|me| { - while try!(visitor.visit(me)).is_some() {} - Ok(()) - }) + fn serialize_seq(&mut self, _len: Option) -> Result { + self.seq_begin() } fn serialize_seq_elt(&mut self, value: T) -> Result<(), Error> where T: ser::Serialize { value.serialize(self) } - fn serialize_map(&mut self, mut visitor: V) -> Result<(), Error> - where V: ser::MapVisitor - { - self.table(|me| { - while try!(visitor.visit(me)).is_some() {} - Ok(()) - }) + fn serialize_seq_end(&mut self, _len: Option, state: State) -> Result<(), Error> { + self.seq_end(state) + } + fn serialize_map(&mut self, _len: Option) -> Result { + self.table_begin() } fn serialize_map_elt(&mut self, key: K, value: V) -> Result<(), Error> where K: ser::Serialize, V: ser::Serialize @@ -62,6 +57,9 @@ impl ser::Serializer for Encoder { try!(value.serialize(self)); Ok(()) } + fn serialize_map_end(&mut self, _len: Option, state: Self) -> Result<(), Error> { + self.table_end(state) + } fn serialize_newtype_struct(&mut self, _name: &'static str, value: T) -> Result<(), Self::Error> @@ -93,12 +91,18 @@ impl ser::Serialize for Value { Value::Boolean(b) => e.serialize_bool(b), Value::Datetime(ref s) => e.serialize_str(s), Value::Array(ref a) => { - e.serialize_seq(ser::impls::SeqIteratorVisitor::new(a.iter(), - Some(a.len()))) + let state = try!(e.serialize_seq(Some(a.len()))); + for el in a.iter() { + try!(e.serialize_seq_elt(el)); + } + e.serialize_seq_end(Some(a.len()), state) } Value::Table(ref t) => { - e.serialize_map(ser::impls::MapIteratorVisitor::new(t.iter(), - Some(t.len()))) + let state = try!(e.serialize_map(Some(t.len()))); + for (k, v) in t.iter() { + try!(e.serialize_map_elt(k, v)); + } + e.serialize_map_end(Some(t.len()), state) } } }