aboutsummaryrefslogtreecommitdiff
path: root/src/encoder
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoder')
-rw-r--r--src/encoder/mod.rs67
-rw-r--r--src/encoder/rustc_serialize.rs32
-rw-r--r--src/encoder/serde.rs162
3 files changed, 175 insertions, 86 deletions
diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs
index fb00a47..d3aba1b 100644
--- a/src/encoder/mod.rs
+++ b/src/encoder/mod.rs
@@ -66,9 +66,14 @@ pub enum Error {
Custom(String),
}
+/// Internal state of the encoder when encoding transitions
+#[derive(Debug)]
+pub struct EncoderState {
+ inner: State,
+}
+
#[derive(PartialEq, Debug)]
-#[doc(hidden)]
-pub enum State {
+enum State {
Start,
NextKey(String),
NextArray(Vec<Value>),
@@ -113,15 +118,6 @@ impl Encoder {
}
}
- #[cfg(feature = "rustc-serialize")]
- fn seq<F>(&mut self, f: F) -> Result<(), Error>
- where F: FnOnce(&mut Encoder) -> Result<(), Error>
- {
- 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())))
}
@@ -133,55 +129,6 @@ impl Encoder {
}
}
- #[cfg(feature = "rustc-serialize")]
- fn table<F>(&mut self, f: F) -> Result<(), Error>
- where F: FnOnce(&mut Encoder) -> Result<(), Error>
- {
- match mem::replace(&mut self.state, State::Start) {
- State::NextKey(key) => {
- let mut nested = Encoder::new();
- try!(f(&mut nested));
- self.toml.insert(key, Value::Table(nested.toml));
- Ok(())
- }
- State::NextArray(mut arr) => {
- let mut nested = Encoder::new();
- try!(f(&mut nested));
- arr.push(Value::Table(nested.toml));
- self.state = State::NextArray(arr);
- Ok(())
- }
- State::Start => f(self),
- State::NextMapKey => Err(Error::InvalidMapKeyLocation),
- }
- }
-
- #[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/rustc_serialize.rs b/src/encoder/rustc_serialize.rs
index 6dce66a..0eda740 100644
--- a/src/encoder/rustc_serialize.rs
+++ b/src/encoder/rustc_serialize.rs
@@ -5,6 +5,38 @@ use Value;
use super::{Encoder, Error, State};
use super::Error::*;
+impl Encoder {
+ fn table<F>(&mut self, f: F) -> Result<(), Error>
+ where F: FnOnce(&mut Encoder) -> Result<(), Error>
+ {
+ match mem::replace(&mut self.state, State::Start) {
+ State::NextKey(key) => {
+ let mut nested = Encoder::new();
+ try!(f(&mut nested));
+ self.toml.insert(key, Value::Table(nested.toml));
+ Ok(())
+ }
+ State::NextArray(mut arr) => {
+ let mut nested = Encoder::new();
+ try!(f(&mut nested));
+ arr.push(Value::Table(nested.toml));
+ self.state = State::NextArray(arr);
+ Ok(())
+ }
+ State::Start => f(self),
+ State::NextMapKey => Err(Error::InvalidMapKeyLocation),
+ }
+ }
+
+ fn seq<F>(&mut self, f: F) -> Result<(), Error>
+ where F: FnOnce(&mut Encoder) -> Result<(), Error>
+ {
+ let old = try!(self.seq_begin());
+ try!(f(self));
+ self.seq_end(old)
+ }
+}
+
impl rustc_serialize::Encoder for Encoder {
type Error = Error;
diff --git a/src/encoder/serde.rs b/src/encoder/serde.rs
index fcccc3d..997bc37 100644
--- a/src/encoder/serde.rs
+++ b/src/encoder/serde.rs
@@ -1,76 +1,125 @@
+use std::mem;
+
use serde::ser;
use Value;
-use super::{Encoder, Error, State};
+use super::{Encoder, Error, EncoderState, State};
+
+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(())
+ }
+}
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;
+ type SeqState = EncoderState;
+ type TupleState = EncoderState;
+ type TupleStructState = EncoderState;
+ type TupleVariantState = EncoderState;
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
self.emit_value(Value::Boolean(v))
}
+
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
self.emit_value(Value::Integer(v))
}
+
+ // TODO: checked casts
+
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(())
}
+
fn serialize_none(&mut self) -> Result<(), Error> {
self.emit_none()
}
+
fn serialize_char(&mut self, c: char) -> Result<(), Error> {
self.serialize_str(&c.to_string())
}
+
fn serialize_some<V>(&mut self, value: V) -> Result<(), Error>
where V: ser::Serialize
{
value.serialize(self)
}
+
fn serialize_bytes(&mut self, v: &[u8]) -> Result<(), Error> {
let mut state = try!(self.serialize_seq(Some(v.len())));
for c in v {
@@ -78,93 +127,152 @@ impl ser::Serializer for Encoder {
}
self.serialize_seq_end(state)
}
- fn serialize_seq_fixed_size(&mut self, len: usize) -> Result<State, Error> {
+
+ fn serialize_seq_fixed_size(&mut self, len: usize)
+ -> Result<EncoderState, Error> {
self.serialize_seq(Some(len))
}
- fn serialize_seq(&mut self, _len: Option<usize>) -> Result<State, Error> {
- self.seq_begin()
+
+ fn serialize_seq(&mut self, _len: Option<usize>)
+ -> Result<EncoderState, Error> {
+ self.seq_begin().map(|s| EncoderState { inner: s })
}
- fn serialize_seq_elt<T>(&mut self, _state: &mut State, value: T) -> Result<(), Error>
+
+ fn serialize_seq_elt<T>(&mut self,
+ _state: &mut EncoderState,
+ value: T) -> Result<(), Error>
where T: ser::Serialize
{
value.serialize(self)
}
- fn serialize_seq_end(&mut self, state: State) -> Result<(), Error> {
- self.seq_end(state)
+
+ fn serialize_seq_end(&mut self, state: EncoderState) -> Result<(), Error> {
+ self.seq_end(state.inner)
}
- fn serialize_tuple(&mut self, len: usize) -> Result<State, Error> {
+
+ fn serialize_tuple(&mut self, len: usize)
+ -> Result<EncoderState, Error> {
self.serialize_seq(Some(len))
}
- fn serialize_tuple_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
+
+ fn serialize_tuple_elt<T>(&mut self,
+ state: &mut EncoderState,
+ value: T) -> Result<(), Error>
where T: ser::Serialize
{
self.serialize_seq_elt(state, value)
}
- fn serialize_tuple_end(&mut self, state: State) -> Result<(), Error> {
+
+ fn serialize_tuple_end(&mut self, state: EncoderState) -> Result<(), Error> {
self.serialize_seq_end(state)
}
- fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<State, Error> {
+
+ fn serialize_tuple_struct(&mut self,
+ _name: &'static str,
+ len: usize) -> Result<EncoderState, Error> {
self.serialize_seq(Some(len))
}
- fn serialize_tuple_struct_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
+
+ fn serialize_tuple_struct_elt<T>(&mut self,
+ state: &mut EncoderState,
+ 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> {
+
+ fn serialize_tuple_struct_end(&mut self, state: EncoderState)
+ -> 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> {
+
+ fn serialize_tuple_variant(&mut self,
+ _name: &'static str,
+ _id: usize,
+ _variant: &'static str,
+ len: usize) -> Result<EncoderState, Error> {
self.serialize_seq(Some(len))
}
- fn serialize_tuple_variant_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
+
+ fn serialize_tuple_variant_elt<T>(&mut self,
+ state: &mut EncoderState,
+ 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> {
+
+ fn serialize_tuple_variant_end(&mut self, state: EncoderState)
+ -> 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>
+
+ 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>
+
+ fn serialize_map_value<V>(&mut self,
+ _state: &mut Encoder,
+ value: V) -> Result<(), Error>
where V: ser::Serialize
{
value.serialize(self)
}
+
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> {
+
+ 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>
+
+ fn serialize_struct_elt<V>(&mut self,
+ state: &mut Encoder,
+ key: &'static str,
+ value: V) -> Result<(), Error>
where V: ser::Serialize
{
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> {
+
+ 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_struct_variant_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
+
+ fn serialize_struct_variant_elt<V>(&mut self,
+ state: &mut Encoder,
+ key: &'static str,
+ value: V) -> Result<(), Error>
where V: ser::Serialize
{
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,
value: T) -> Result<(), Self::Error>
@@ -173,6 +281,7 @@ impl ser::Serializer for Encoder {
// Don't serialize the newtype struct in a tuple.
value.serialize(self)
}
+
fn serialize_newtype_variant<T>(&mut self,
_name: &'static str,
_variant_index: usize,
@@ -183,6 +292,7 @@ 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,