From b4f706cceb4dec7d36d787b46716deaf72e4775d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 28 Jun 2014 15:15:25 -0700 Subject: Be more resilient about unused values in array --- src/serialization.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) 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 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 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(&mut self, f: |&mut Decoder, uint| -> Result) @@ -1091,4 +1109,27 @@ mod tests { assert_eq!(d.toml, None); } + + #[test] + fn unused_fields7() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a: Vec } + #[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) + })]) + }))); + } } -- cgit v1.2.3