aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2016-07-14 15:28:51 +0200
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2016-07-14 15:28:51 +0200
commit2626cf77cb9bd397a26dac1c662e731f03e258be (patch)
tree9cfdae8fe647ecb7762915b9a8d000299be0e088
parentfb8050d2d556dd613971f15e7bd73cfd43acad82 (diff)
downloadmilf-rs-2626cf77cb9bd397a26dac1c662e731f03e258be.tar.gz
milf-rs-2626cf77cb9bd397a26dac1c662e731f03e258be.zip
adjust for seq/map ser revamp
-rw-r--r--src/encoder/mod.rs41
-rw-r--r--src/encoder/serde.rs42
2 files changed, 60 insertions, 23 deletions
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<Value>),
@@ -115,8 +116,16 @@ impl Encoder {
fn seq<F>(&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<State, Error> {
+ 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<Self, Error> {
+ 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<F>(&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<V>(&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<usize>) -> Result<State, Error> {
+ self.seq_begin()
}
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
where T: ser::Serialize
{
value.serialize(self)
}
- fn serialize_map<V>(&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<usize>, state: State) -> Result<(), Error> {
+ self.seq_end(state)
+ }
+ fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
+ self.table_begin()
}
fn serialize_map_elt<K, V>(&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<usize>, state: Self) -> Result<(), Error> {
+ self.table_end(state)
+ }
fn serialize_newtype_struct<T>(&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)
}
}
}