adjust for seq/map ser revamp
This commit is contained in:
parent
fb8050d2d5
commit
183646b118
|
@ -35,7 +35,7 @@ use {Value, Table};
|
||||||
/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4)))
|
/// assert_eq!(e.toml.get(&"foo".to_string()), Some(&Value::Integer(4)))
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Default)]
|
#[derive(Default, Debug)]
|
||||||
pub struct Encoder {
|
pub struct Encoder {
|
||||||
/// Output TOML that is emitted. The current version of this encoder forces
|
/// Output TOML that is emitted. The current version of this encoder forces
|
||||||
/// the top-level representation of a structure to be a table.
|
/// the top-level representation of a structure to be a table.
|
||||||
|
@ -66,8 +66,9 @@ pub enum Error {
|
||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum State {
|
#[doc(hidden)]
|
||||||
|
pub enum State {
|
||||||
Start,
|
Start,
|
||||||
NextKey(String),
|
NextKey(String),
|
||||||
NextArray(Vec<Value>),
|
NextArray(Vec<Value>),
|
||||||
|
@ -115,8 +116,16 @@ impl Encoder {
|
||||||
fn seq<F>(&mut self, f: F) -> Result<(), Error>
|
fn seq<F>(&mut self, f: F) -> Result<(), Error>
|
||||||
where F: FnOnce(&mut Encoder) -> 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));
|
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) {
|
match mem::replace(&mut self.state, old) {
|
||||||
State::NextArray(v) => self.emit_value(Value::Array(v)),
|
State::NextArray(v) => self.emit_value(Value::Array(v)),
|
||||||
_ => unreachable!(),
|
_ => 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>
|
fn table_key<F>(&mut self, f: F) -> Result<(), Error>
|
||||||
where F: FnOnce(&mut Encoder) -> Result<(), Error>
|
where F: FnOnce(&mut Encoder) -> Result<(), Error>
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
use serde::ser;
|
use serde::ser;
|
||||||
use Value;
|
use Value;
|
||||||
use super::{Encoder, Error};
|
use super::{Encoder, Error, State};
|
||||||
|
|
||||||
impl ser::Serializer for Encoder {
|
impl ser::Serializer for Encoder {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
type MapState = Self;
|
||||||
|
type SeqState = State;
|
||||||
|
|
||||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||||
self.emit_value(Value::Boolean(v))
|
self.emit_value(Value::Boolean(v))
|
||||||
|
@ -34,26 +36,19 @@ impl ser::Serializer for Encoder {
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
fn serialize_seq<V>(&mut self, mut visitor: V) -> Result<(), Error>
|
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<State, Error> {
|
||||||
where V: ser::SeqVisitor
|
self.seq_begin()
|
||||||
{
|
|
||||||
self.seq(|me| {
|
|
||||||
while try!(visitor.visit(me)).is_some() {}
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
|
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
|
||||||
where T: ser::Serialize
|
where T: ser::Serialize
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
fn serialize_map<V>(&mut self, mut visitor: V) -> Result<(), Error>
|
fn serialize_seq_end(&mut self, _len: Option<usize>, state: State) -> Result<(), Error> {
|
||||||
where V: ser::MapVisitor
|
self.seq_end(state)
|
||||||
{
|
}
|
||||||
self.table(|me| {
|
fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
|
||||||
while try!(visitor.visit(me)).is_some() {}
|
self.table_begin()
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
|
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
|
||||||
where K: ser::Serialize, V: ser::Serialize
|
where K: ser::Serialize, V: ser::Serialize
|
||||||
|
@ -62,6 +57,9 @@ impl ser::Serializer for Encoder {
|
||||||
try!(value.serialize(self));
|
try!(value.serialize(self));
|
||||||
Ok(())
|
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,
|
fn serialize_newtype_struct<T>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
value: T) -> Result<(), Self::Error>
|
value: T) -> Result<(), Self::Error>
|
||||||
|
@ -93,12 +91,18 @@ impl ser::Serialize for Value {
|
||||||
Value::Boolean(b) => e.serialize_bool(b),
|
Value::Boolean(b) => e.serialize_bool(b),
|
||||||
Value::Datetime(ref s) => e.serialize_str(s),
|
Value::Datetime(ref s) => e.serialize_str(s),
|
||||||
Value::Array(ref a) => {
|
Value::Array(ref a) => {
|
||||||
e.serialize_seq(ser::impls::SeqIteratorVisitor::new(a.iter(),
|
let state = try!(e.serialize_seq(Some(a.len())));
|
||||||
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) => {
|
Value::Table(ref t) => {
|
||||||
e.serialize_map(ser::impls::MapIteratorVisitor::new(t.iter(),
|
let state = try!(e.serialize_map(Some(t.len())));
|
||||||
Some(t.len())))
|
for (k, v) in t.iter() {
|
||||||
|
try!(e.serialize_map_elt(k, v));
|
||||||
|
}
|
||||||
|
e.serialize_map_end(Some(t.len()), state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue