Tweak some style

This commit is contained in:
Alex Crichton 2015-04-21 09:14:49 -07:00
parent 5dd623d3b1
commit 3301c39630
2 changed files with 49 additions and 38 deletions

View file

@ -3,8 +3,10 @@ sudo: false
script: script:
- cargo build --verbose - cargo build --verbose
- cargo build --verbose --no-default-features - cargo build --verbose --no-default-features
- cargo build --verbose --features serde --no-default-features
- cargo test --verbose - cargo test --verbose
- cargo test --verbose --no-default-features - cargo test --verbose --no-default-features
- cargo test --verbose --features serde --no-default-features
- rustdoc --test README.md -L target - rustdoc --test README.md -L target
- cargo doc --no-deps - cargo doc --no-deps
after_success: | after_success: |

View file

@ -3,7 +3,12 @@ use Value;
use super::{Decoder, DecodeError, DecodeErrorKind}; use super::{Decoder, DecodeError, DecodeErrorKind};
use std::collections::BTreeMap; use std::collections::BTreeMap;
struct MapVisitor<'a, I>(I, Option<Value>, &'a mut Option<Value>, Option<String>); struct MapVisitor<'a, I> {
iter: I,
toml: &'a mut Option<Value>,
key: Option<String>,
value: Option<Value>,
}
fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError { fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError {
match err { match err {
@ -52,7 +57,12 @@ impl de::Deserializer for Decoder {
visitor.visit_seq(SeqDeserializer::new(iter, len, &mut self.toml)) visitor.visit_seq(SeqDeserializer::new(iter, len, &mut self.toml))
} }
Some(Value::Table(t)) => { Some(Value::Table(t)) => {
visitor.visit_map(MapVisitor(t.into_iter(), None, &mut self.toml, None)) visitor.visit_map(MapVisitor {
iter: t.into_iter(),
toml: &mut self.toml,
key: None,
value: None,
})
} }
None => Err(de::Error::end_of_stream_error()), None => Err(de::Error::end_of_stream_error()),
} }
@ -87,19 +97,17 @@ struct SeqDeserializer<'a, I> {
toml: &'a mut Option<Value>, toml: &'a mut Option<Value>,
} }
impl<'a, I> SeqDeserializer<'a, I> impl<'a, I> SeqDeserializer<'a, I> where I: Iterator<Item=Value> {
where I: Iterator<Item=Value>, fn new(iter: I, len: usize, toml: &'a mut Option<Value>) -> Self {
{
pub fn new(iter: I, len: usize, toml: &'a mut Option<Value>) -> Self {
SeqDeserializer { SeqDeserializer {
iter: iter, iter: iter,
len: len, len: len,
toml: toml, toml: toml,
} }
} }
fn remember(&mut self, v: Value) {
fn put_value_back(&mut self, v: Value) {
*self.toml = self.toml.take().or(Some(Value::Array(Vec::new()))); *self.toml = self.toml.take().or(Some(Value::Array(Vec::new())));
// remember unknown field
match self.toml.as_mut().unwrap() { match self.toml.as_mut().unwrap() {
&mut Value::Array(ref mut a) => { &mut Value::Array(ref mut a) => {
a.push(v); a.push(v);
@ -135,7 +143,7 @@ impl<'a, I> de::SeqVisitor for SeqDeserializer<'a, I>
let mut de = Decoder::new(value); let mut de = Decoder::new(value);
let v = try!(de::Deserialize::deserialize(&mut de)); let v = try!(de::Deserialize::deserialize(&mut de));
if let Some(t) = de.toml { if let Some(t) = de.toml {
self.remember(t); self.put_value_back(t);
} }
Ok(Some(v)) Ok(Some(v))
} }
@ -178,12 +186,13 @@ impl de::Error for DecodeError {
} }
impl<'a, I> MapVisitor<'a, I> { impl<'a, I> MapVisitor<'a, I> {
fn remember(&mut self, v: Value) { fn put_value_back(&mut self, v: Value) {
*self.2 = self.2.take().or(Some(Value::Table(BTreeMap::new()))); *self.toml = self.toml.take().or_else(|| {
// remember unknown field Some(Value::Table(BTreeMap::new()))
match self.2.as_mut().unwrap() { });
match self.toml.as_mut().unwrap() {
&mut Value::Table(ref mut t) => { &mut Value::Table(ref mut t) => {
t.insert(self.3.take().unwrap(), v); t.insert(self.key.take().unwrap(), v);
}, },
_ => unreachable!(), _ => unreachable!(),
} }
@ -198,36 +207,35 @@ impl<'a, I> de::MapVisitor for MapVisitor<'a, I>
fn visit_key<K>(&mut self) -> Result<Option<K>, DecodeError> fn visit_key<K>(&mut self) -> Result<Option<K>, DecodeError>
where K: de::Deserialize where K: de::Deserialize
{ {
match self.0.next() { while let Some((k, v)) = self.iter.next() {
Some((k, v)) => { self.key = Some(k.clone());
self.3 = Some(k.clone()); let mut dec = Decoder::new(Value::String(k));
let dec = &mut Decoder::new(Value::String(k)); match de::Deserialize::deserialize(&mut dec) {
match de::Deserialize::deserialize(dec) { Ok(val) => {
Err(DecodeError {kind: DecodeErrorKind::UnknownField, ..}) => { self.value = Some(v);
self.remember(v); return Ok(Some(val))
self.visit_key()
}
Ok(val) => {
self.1 = Some(v);
Ok(Some(val))
},
Err(e) => Err(e),
} }
}
None => Ok(None),
}
// If this was an unknown field, then we put the toml value
// back into the map and keep going.
Err(DecodeError {kind: DecodeErrorKind::UnknownField, ..}) => {
self.put_value_back(v);
}
Err(e) => return Err(e),
}
}
Ok(None)
} }
fn visit_value<V>(&mut self) -> Result<V, DecodeError> fn visit_value<V>(&mut self) -> Result<V, DecodeError>
where V: de::Deserialize where V: de::Deserialize
{ {
match self.1.take() { match self.value.take() {
Some(t) => { Some(t) => {
let mut dec = Decoder::new(t); let mut dec = Decoder::new(t);
let v = try!(de::Deserialize::deserialize(&mut dec)); let v = try!(de::Deserialize::deserialize(&mut dec));
if let Some(t) = dec.toml { if let Some(t) = dec.toml {
self.remember(t); self.put_value_back(t);
} }
Ok(v) Ok(v)
}, },
@ -239,13 +247,14 @@ impl<'a, I> de::MapVisitor for MapVisitor<'a, I>
Ok(()) Ok(())
} }
fn missing_field<V>(&mut self, field_name: &'static str) -> Result<V, DecodeError> fn missing_field<V>(&mut self, field_name: &'static str)
where V: de::Deserialize, -> Result<V, DecodeError> where V: de::Deserialize {
{
println!("missing field: {}", field_name);
// See if the type can deserialize from a unit. // See if the type can deserialize from a unit.
match de::Deserialize::deserialize(&mut UnitDeserializer) { match de::Deserialize::deserialize(&mut UnitDeserializer) {
Err(DecodeError {kind: DecodeErrorKind::SyntaxError, field}) => Err(DecodeError { Err(DecodeError {
kind: DecodeErrorKind::SyntaxError,
field,
}) => Err(DecodeError {
field: field.or(Some(field_name.to_string())), field: field.or(Some(field_name.to_string())),
kind: DecodeErrorKind::ExpectedField(None), kind: DecodeErrorKind::ExpectedField(None),
}), }),