From 944b94c21a064d0b1ad80ccabf12d95726c72139 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 12 Dec 2014 13:26:49 -0800 Subject: Handle deserializing empty or missing arrays --- src/serialization.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/serialization.rs b/src/serialization.rs index 457f361..829d89a 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -260,7 +260,6 @@ impl serialize::Encoder for Encoder { let old = mem::replace(&mut self.state, NextKey(f_name.to_string())); try!(f(self)); if self.state != Start { - println!("{}", self.state); return Err(NoValue) } self.state = old; @@ -636,6 +635,7 @@ impl serialize::Decoder for Decoder { { let len = match self.toml { Some(Array(ref arr)) => arr.len(), + None => 0, ref found => return Err(self.mismatch("array", found)), }; 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 } + #[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> } + #[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()); + } } -- cgit v1.2.3