Touch up the encoder/decoder a bit

* Whitespace things
* Don't make `State` public
* Remove `#[cfg]` annotations
This commit is contained in:
Alex Crichton 2016-07-29 10:23:06 -07:00
parent b480a26f8d
commit 4ee9cb72e6
5 changed files with 180 additions and 100 deletions

View file

@ -1,9 +1,7 @@
use std::error;
use std::fmt;
#[cfg(feature = "rustc-serialize")]
use std::collections::{btree_map, BTreeMap};
#[cfg(feature = "rustc-serialize")]
use std::iter::Peekable;
use Value;
@ -22,9 +20,11 @@ pub struct Decoder {
/// whether fields were decoded or not.
pub toml: Option<Value>,
cur_field: Option<String>,
#[cfg(feature = "rustc-serialize")]
// These aren't used if serde is in use
#[cfg_attr(feature = "serde", allow(dead_code))]
cur_map: Peekable<btree_map::IntoIter<String, Value>>,
#[cfg(feature = "rustc-serialize")]
#[cfg_attr(feature = "serde", allow(dead_code))]
leftover_map: ::Table,
}
@ -135,7 +135,6 @@ impl Decoder {
Decoder::new_empty(toml, cur_field)
}
#[cfg(feature = "rustc-serialize")]
fn new_empty(toml: Option<Value>, cur_field: Option<String>) -> Decoder {
Decoder {
toml: toml,
@ -145,14 +144,6 @@ impl Decoder {
}
}
#[cfg(not(feature = "rustc-serialize"))]
fn new_empty(toml: Option<Value>, cur_field: Option<String>) -> Decoder {
Decoder {
toml: toml,
cur_field: cur_field,
}
}
fn err(&self, kind: DecodeErrorKind) -> DecodeError {
DecodeError {
field: self.cur_field.clone(),

View file

@ -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>
{

View file

@ -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;

View file

@ -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,

View file

@ -48,7 +48,7 @@ use std::str::FromStr;
pub use parser::{Parser, ParserError};
#[cfg(any(feature = "rustc-serialize", feature = "serde"))]
pub use self::encoder::{Encoder, Error, encode, encode_str};
pub use self::encoder::{Encoder, Error, EncoderState, encode, encode_str};
#[cfg(any(feature = "rustc-serialize", feature = "serde"))]
pub use self::decoder::{Decoder, DecodeError, DecodeErrorKind, decode, decode_str};