aboutsummaryrefslogtreecommitdiff
path: root/src/encoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoder')
-rw-r--r--src/encoder/mod.rs45
-rw-r--r--src/encoder/serde.rs164
2 files changed, 182 insertions, 27 deletions
diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs
index 304bac6..fb00a47 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>),
@@ -112,17 +113,27 @@ impl Encoder {
}
}
+ #[cfg(feature = "rustc-serialize")]
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!(),
}
}
+ #[cfg(feature = "rustc-serialize")]
fn table<F>(&mut self, f: F) -> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error>
{
@@ -145,6 +156,32 @@ impl Encoder {
}
}
+ #[cfg(feature = "serde")]
+ fn table_begin(&mut self) -> Result<Self, Error> {
+ match self.state {
+ State::NextMapKey => Err(Error::InvalidMapKeyLocation),
+ _ => Ok(mem::replace(self, Encoder::new()))
+ }
+ }
+
+ #[cfg(feature = "serde")]
+ 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..fcccc3d 100644
--- a/src/encoder/serde.rs
+++ b/src/encoder/serde.rs
@@ -1,9 +1,16 @@
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 StructState = Self;
+ type StructVariantState = Self;
+ type SeqState = State;
+ type TupleState = State;
+ type TupleStructState = State;
+ type TupleVariantState = State;
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
self.emit_value(Value::Boolean(v))
@@ -14,12 +21,42 @@ impl ser::Serializer for Encoder {
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
self.serialize_i64(v as i64)
}
+ fn serialize_isize(&mut self, v: isize) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_usize(&mut self, v: usize) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
+ self.serialize_i64(v as i64)
+ }
+ fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
+ self.serialize_f64(v as f64)
+ }
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
self.emit_value(Value::Float(v))
}
fn serialize_str(&mut self, value: &str) -> Result<(), Error> {
self.emit_value(Value::String(value.to_string()))
}
+ fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<(), Error> {
+ Ok(())
+ }
fn serialize_unit(&mut self) -> Result<(), Error> {
Ok(())
}
@@ -34,33 +71,99 @@ impl ser::Serializer for Encoder {
{
value.serialize(self)
}
- fn serialize_seq<V>(&mut self, mut visitor: V) -> Result<(), Error>
- where V: ser::SeqVisitor
+ fn serialize_bytes(&mut self, v: &[u8]) -> Result<(), Error> {
+ let mut state = try!(self.serialize_seq(Some(v.len())));
+ for c in v {
+ try!(self.serialize_seq_elt(&mut state, c));
+ }
+ self.serialize_seq_end(state)
+ }
+ fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<State, Error> {
+ self.serialize_seq(Some(len))
+ }
+ fn serialize_seq(&mut self, _len: Option<usize>) -> Result<State, Error> {
+ self.seq_begin()
+ }
+ fn serialize_seq_elt<T>(&mut self, _state: &mut State, value: T) -> Result<(), Error>
+ where T: ser::Serialize
{
- self.seq(|me| {
- while try!(visitor.visit(me)).is_some() {}
- Ok(())
- })
+ value.serialize(self)
}
- fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
+ fn serialize_seq_end(&mut self, state: State) -> Result<(), Error> {
+ self.seq_end(state)
+ }
+ fn serialize_tuple(&mut self, len: usize) -> Result<State, Error> {
+ self.serialize_seq(Some(len))
+ }
+ fn serialize_tuple_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
where T: ser::Serialize
{
+ self.serialize_seq_elt(state, value)
+ }
+ fn serialize_tuple_end(&mut self, state: State) -> Result<(), Error> {
+ self.serialize_seq_end(state)
+ }
+ fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<State, Error> {
+ self.serialize_seq(Some(len))
+ }
+ fn serialize_tuple_struct_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
+ where T: ser::Serialize
+ {
+ self.serialize_seq_elt(state, value)
+ }
+ fn serialize_tuple_struct_end(&mut self, state: State) -> Result<(), Error> {
+ self.serialize_seq_end(state)
+ }
+ fn serialize_tuple_variant(&mut self, _name: &'static str, _id: usize, _variant: &'static str, len: usize) -> Result<State, Error> {
+ self.serialize_seq(Some(len))
+ }
+ fn serialize_tuple_variant_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
+ where T: ser::Serialize
+ {
+ self.serialize_seq_elt(state, value)
+ }
+ fn serialize_tuple_variant_end(&mut self, state: State) -> Result<(), Error> {
+ self.serialize_seq_end(state)
+ }
+ fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
+ self.table_begin()
+ }
+ fn serialize_map_key<K>(&mut self, _state: &mut Encoder, key: K) -> Result<(), Error>
+ where K: ser::Serialize
+ {
+ self.table_key(|me| key.serialize(me))
+ }
+ fn serialize_map_value<V>(&mut self, _state: &mut Encoder, value: V) -> Result<(), Error>
+ where V: ser::Serialize
+ {
value.serialize(self)
}
- fn serialize_map<V>(&mut self, mut visitor: V) -> Result<(), Error>
- where V: ser::MapVisitor
+ fn serialize_map_end(&mut self, state: Self) -> Result<(), Error> {
+ self.table_end(state)
+ }
+ fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<Self, Error> {
+ self.serialize_map(Some(len))
+ }
+ fn serialize_struct_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
+ where V: ser::Serialize
{
- self.table(|me| {
- while try!(visitor.visit(me)).is_some() {}
- Ok(())
- })
+ try!(self.serialize_map_key(state, key));
+ self.serialize_map_value(state, value)
+ }
+ fn serialize_struct_end(&mut self, state: Self) -> Result<(), Error> {
+ self.serialize_map_end(state)
+ }
+ fn serialize_struct_variant(&mut self, _name: &'static str, _id: usize, _variant: &'static str, len: usize) -> Result<Self, Error> {
+ self.serialize_map(Some(len))
}
- fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
- where K: ser::Serialize, V: ser::Serialize
+ fn serialize_struct_variant_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
+ where V: ser::Serialize
{
- try!(self.table_key(|me| key.serialize(me)));
- try!(value.serialize(self));
- Ok(())
+ try!(self.serialize_map_key(state, key));
+ self.serialize_map_value(state, value)
+ }
+ fn serialize_struct_variant_end(&mut self, state: Self) -> Result<(), Error> {
+ self.serialize_map_end(state)
}
fn serialize_newtype_struct<T>(&mut self,
_name: &'static str,
@@ -80,6 +183,14 @@ impl ser::Serializer for Encoder {
// Don't serialize the newtype struct variant in a tuple.
value.serialize(self)
}
+ fn serialize_unit_variant(&mut self,
+ _name: &'static str,
+ _variant_index: usize,
+ _variant: &'static str,
+ ) -> Result<(), Self::Error>
+ {
+ Ok(())
+ }
}
impl ser::Serialize for Value {
@@ -93,12 +204,19 @@ 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 mut state = try!(e.serialize_seq(Some(a.len())));
+ for el in a.iter() {
+ try!(e.serialize_seq_elt(&mut state, el));
+ }
+ e.serialize_seq_end(state)
}
Value::Table(ref t) => {
- e.serialize_map(ser::impls::MapIteratorVisitor::new(t.iter(),
- Some(t.len())))
+ let mut state = try!(e.serialize_map(Some(t.len())));
+ for (k, v) in t.iter() {
+ try!(e.serialize_map_key(&mut state, k));
+ try!(e.serialize_map_value(&mut state, v));
+ }
+ e.serialize_map_end(state)
}
}
}