diff options
author | Alex Crichton <alex@alexcrichton.com> | 2014-12-12 13:26:49 -0800 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2014-12-12 13:30:40 -0800 |
commit | 944b94c21a064d0b1ad80ccabf12d95726c72139 (patch) | |
tree | 086b2f3d3c7750e57d39b405a127cd3b4fa56263 /src | |
parent | 4dce070c70d758f586a8f481f9fb2237f7633a58 (diff) | |
download | milf-rs-944b94c21a064d0b1ad80ccabf12d95726c72139.tar.gz milf-rs-944b94c21a064d0b1ad80ccabf12d95726c72139.zip |
Handle deserializing empty or missing arrays
Diffstat (limited to 'src')
-rw-r--r-- | src/serialization.rs | 32 |
1 files changed, 31 insertions, 1 deletions
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<Error> 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<DecodeError> 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<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()); + } } |