From 86b9259616ed05fe32c5eee233b65e7934a76666 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 28 Jul 2016 11:08:39 -0700 Subject: Cfg off functions only needed for rustc-serialize or serde --- src/decoder/mod.rs | 40 +++++++++++++++++++++++++++------------- src/encoder/mod.rs | 4 ++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index b223e03..9720528 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,6 +1,9 @@ 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; @@ -19,7 +22,9 @@ pub struct Decoder { /// whether fields were decoded or not. pub toml: Option, cur_field: Option, + #[cfg(feature = "rustc-serialize")] cur_map: Peekable>, + #[cfg(feature = "rustc-serialize")] leftover_map: ::Table, } @@ -115,27 +120,36 @@ impl Decoder { /// This decoder can be passed to the `Decodable` methods or driven /// manually. pub fn new(toml: Value) -> Decoder { + Decoder::new_empty(Some(toml), None) + } + + fn sub_decoder(&self, toml: Option, field: &str) -> Decoder { + let cur_field = if field.is_empty() { + self.cur_field.clone() + } else { + match self.cur_field { + None => Some(field.to_string()), + Some(ref s) => Some(format!("{}.{}", s, field)) + } + }; + Decoder::new_empty(toml, cur_field) + } + + #[cfg(feature = "rustc-serialize")] + fn new_empty(toml: Option, cur_field: Option) -> Decoder { Decoder { - toml: Some(toml), - cur_field: None, + toml: toml, + cur_field: cur_field, leftover_map: BTreeMap::new(), cur_map: BTreeMap::new().into_iter().peekable(), } } - fn sub_decoder(&self, toml: Option, field: &str) -> Decoder { + #[cfg(not(feature = "rustc-serialize"))] + fn new_empty(toml: Option, cur_field: Option) -> Decoder { Decoder { toml: toml, - cur_field: if field.is_empty() { - self.cur_field.clone() - } else { - match self.cur_field { - None => Some(field.to_string()), - Some(ref s) => Some(format!("{}.{}", s, field)) - } - }, - leftover_map: BTreeMap::new(), - cur_map: BTreeMap::new().into_iter().peekable(), + cur_field: cur_field, } } diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 0f13b81..fb00a47 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -113,6 +113,7 @@ impl Encoder { } } + #[cfg(feature = "rustc-serialize")] fn seq(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error> { @@ -132,6 +133,7 @@ impl Encoder { } } + #[cfg(feature = "rustc-serialize")] fn table(&mut self, f: F) -> Result<(), Error> where F: FnOnce(&mut Encoder) -> Result<(), Error> { @@ -154,6 +156,7 @@ impl Encoder { } } + #[cfg(feature = "serde")] fn table_begin(&mut self) -> Result { match self.state { State::NextMapKey => Err(Error::InvalidMapKeyLocation), @@ -161,6 +164,7 @@ impl Encoder { } } + #[cfg(feature = "serde")] fn table_end(&mut self, mut state: Self) -> Result<(), Error> { match state.state { State::NextKey(key) => { -- cgit v1.2.3