Handle deserializing empty or missing arrays

This commit is contained in:
Alex Crichton 2014-12-12 13:26:49 -08:00
parent 4dce070c70
commit 944b94c21a

View file

@ -260,7 +260,6 @@ impl serialize::Encoder<Error> for Encoder {
let old = mem::replace(&mut self.state, NextKey(f_name.to_string())); let old = mem::replace(&mut self.state, NextKey(f_name.to_string()));
try!(f(self)); try!(f(self));
if self.state != Start { if self.state != Start {
println!("{}", self.state);
return Err(NoValue) return Err(NoValue)
} }
self.state = old; self.state = old;
@ -636,6 +635,7 @@ impl serialize::Decoder<DecodeError> for Decoder {
{ {
let len = match self.toml { let len = match self.toml {
Some(Array(ref arr)) => arr.len(), Some(Array(ref arr)) => arr.len(),
None => 0,
ref found => return Err(self.mismatch("array", found)), ref found => return Err(self.mismatch("array", found)),
}; };
let ret = try!(f(self, len)); let ret = try!(f(self, len));
@ -1236,4 +1236,34 @@ mod tests {
})]) })])
}))); })));
} }
#[test]
fn empty_arrays() {
#[deriving(Encodable, Decodable, PartialEq, Show)]
struct Foo { a: Vec<Bar> }
#[deriving(Encodable, Decodable, PartialEq, Show)]
struct Bar;
let v = Foo { a: vec![] };
let mut d = Decoder::new(Table(map! {}));
assert_eq!(v, Decodable::decode(&mut d).unwrap());
}
#[test]
fn empty_arrays2() {
#[deriving(Encodable, Decodable, PartialEq, Show)]
struct Foo { a: Option<Vec<Bar>> }
#[deriving(Encodable, Decodable, PartialEq, Show)]
struct Bar;
let v = Foo { a: None };
let mut d = Decoder::new(Table(map! {}));
assert_eq!(v, Decodable::decode(&mut d).unwrap());
let v = Foo { a: Some(vec![]) };
let mut d = Decoder::new(Table(map! {
a: Array(vec![])
}));
assert_eq!(v, Decodable::decode(&mut d).unwrap());
}
} }