Cfg off functions only needed for rustc-serialize or serde

This commit is contained in:
David Tolnay 2016-07-28 11:08:39 -07:00
parent 492690669e
commit 242b8038a1
2 changed files with 31 additions and 13 deletions

View file

@ -1,6 +1,9 @@
use std::error; use std::error;
use std::fmt; use std::fmt;
#[cfg(feature = "rustc-serialize")]
use std::collections::{btree_map, BTreeMap}; use std::collections::{btree_map, BTreeMap};
#[cfg(feature = "rustc-serialize")]
use std::iter::Peekable; use std::iter::Peekable;
use Value; use Value;
@ -19,7 +22,9 @@ pub struct Decoder {
/// whether fields were decoded or not. /// whether fields were decoded or not.
pub toml: Option<Value>, pub toml: Option<Value>,
cur_field: Option<String>, cur_field: Option<String>,
#[cfg(feature = "rustc-serialize")]
cur_map: Peekable<btree_map::IntoIter<String, Value>>, cur_map: Peekable<btree_map::IntoIter<String, Value>>,
#[cfg(feature = "rustc-serialize")]
leftover_map: ::Table, leftover_map: ::Table,
} }
@ -115,30 +120,39 @@ impl Decoder {
/// This decoder can be passed to the `Decodable` methods or driven /// This decoder can be passed to the `Decodable` methods or driven
/// manually. /// manually.
pub fn new(toml: Value) -> Decoder { pub fn new(toml: Value) -> Decoder {
Decoder { Decoder::new_empty(Some(toml), None)
toml: Some(toml),
cur_field: None,
leftover_map: BTreeMap::new(),
cur_map: BTreeMap::new().into_iter().peekable(),
}
} }
fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder { fn sub_decoder(&self, toml: Option<Value>, field: &str) -> Decoder {
Decoder { let cur_field = if field.is_empty() {
toml: toml,
cur_field: if field.is_empty() {
self.cur_field.clone() self.cur_field.clone()
} else { } else {
match self.cur_field { match self.cur_field {
None => Some(field.to_string()), None => Some(field.to_string()),
Some(ref s) => Some(format!("{}.{}", s, field)) Some(ref s) => Some(format!("{}.{}", s, field))
} }
}, };
Decoder::new_empty(toml, cur_field)
}
#[cfg(feature = "rustc-serialize")]
fn new_empty(toml: Option<Value>, cur_field: Option<String>) -> Decoder {
Decoder {
toml: toml,
cur_field: cur_field,
leftover_map: BTreeMap::new(), leftover_map: BTreeMap::new(),
cur_map: BTreeMap::new().into_iter().peekable(), cur_map: BTreeMap::new().into_iter().peekable(),
} }
} }
#[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 { fn err(&self, kind: DecodeErrorKind) -> DecodeError {
DecodeError { DecodeError {
field: self.cur_field.clone(), field: self.cur_field.clone(),

View file

@ -113,6 +113,7 @@ impl Encoder {
} }
} }
#[cfg(feature = "rustc-serialize")]
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>
{ {
@ -132,6 +133,7 @@ impl Encoder {
} }
} }
#[cfg(feature = "rustc-serialize")]
fn table<F>(&mut self, f: F) -> Result<(), Error> fn table<F>(&mut self, f: F) -> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error>
{ {
@ -154,6 +156,7 @@ impl Encoder {
} }
} }
#[cfg(feature = "serde")]
fn table_begin(&mut self) -> Result<Self, Error> { fn table_begin(&mut self) -> Result<Self, Error> {
match self.state { match self.state {
State::NextMapKey => Err(Error::InvalidMapKeyLocation), State::NextMapKey => Err(Error::InvalidMapKeyLocation),
@ -161,6 +164,7 @@ impl Encoder {
} }
} }
#[cfg(feature = "serde")]
fn table_end(&mut self, mut state: Self) -> Result<(), Error> { fn table_end(&mut self, mut state: Self) -> Result<(), Error> {
match state.state { match state.state {
State::NextKey(key) => { State::NextKey(key) => {