diff options
author | Alex Crichton <alex@alexcrichton.com> | 2014-06-28 15:15:25 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2014-06-28 15:15:25 -0700 |
commit | b4f706cceb4dec7d36d787b46716deaf72e4775d (patch) | |
tree | 174ea03c1bbb2e6fa89d495800bfa8225d69256a /src | |
parent | 85af1747781985ee359158e9aaa0416f4a4ebf70 (diff) | |
download | milf-rs-b4f706cceb4dec7d36d787b46716deaf72e4775d.tar.gz milf-rs-b4f706cceb4dec7d36d787b46716deaf72e4775d.zip |
Be more resilient about unused values in array
Diffstat (limited to 'src')
-rw-r--r-- | src/serialization.rs | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/serialization.rs b/src/serialization.rs index a7c6f2b..b3b3682 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -617,6 +617,15 @@ impl serialize::Decoder<DecodeError> for Decoder { ref found => return Err(self.mismatch("array", found)), }; let ret = try!(f(self, len)); + match self.toml { + Some(Array(ref mut arr)) => { + println!("before: {}", arr); + arr.retain(|slot| slot.as_integer() != Some(0)); + println!("after: {}", arr); + if arr.len() != 0 { return Ok(ret) } + } + _ => return Ok(ret) + } self.toml.take(); Ok(ret) } @@ -628,7 +637,16 @@ impl serialize::Decoder<DecodeError> for Decoder { Some(Array(ref mut arr)) => mem::replace(arr.get_mut(idx), Integer(0)), ref found => return Err(self.mismatch("array", found)), }; - f(&mut self.sub_decoder(Some(toml), "")) + let mut d = self.sub_decoder(Some(toml), ""); + let ret = try!(f(&mut d)); + match d.toml { + Some(toml) => match self.toml { + Some(Array(ref mut arr)) => *arr.get_mut(idx) = toml, + _ => {} + }, + _ => {} + } + Ok(ret) } fn read_map<T>(&mut self, f: |&mut Decoder, uint| -> Result<T, DecodeError>) @@ -1091,4 +1109,27 @@ mod tests { assert_eq!(d.toml, None); } + + #[test] + fn unused_fields7() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a: Vec<Bar> } + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Bar { a: int } + + let v = Foo { a: vec![Bar { a: 1 }] }; + let mut d = Decoder::new(Table(map! { + a: Array(vec![Table(map! { + a: Integer(1), + b: Integer(2) + })]) + })); + assert_eq!(v, Decodable::decode(&mut d).unwrap()); + + assert_eq!(d.toml, Some(Table(map! { + a: Array(vec![Table(map! { + b: Integer(2) + })]) + }))); + } } |