From 00e84033b4af019f3dcfe5f2073f32e5e9c14b7d Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sun, 11 Aug 2024 20:40:54 +0200 Subject: [PATCH] [bencode/de] Implemented any deserialization --- src/bencode/de.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/bencode/de.rs b/src/bencode/de.rs index 3b276ca..df86b5a 100644 --- a/src/bencode/de.rs +++ b/src/bencode/de.rs @@ -81,12 +81,16 @@ impl<'de> Deserializer<'de> { impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { type Error = Error; - fn deserialize_any(self, _: V) -> Result + fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de> { - println!("i don't get this API!"); - Err(Error::WontImplement) + match self.peek_byte()? as char { + 'i' => self.deserialize_i64(visitor), + 'l' => self.deserialize_seq(visitor), + 'd' => self.deserialize_map(visitor), + _ => Err(Error::Syntax), + } } fn deserialize_bool(self, _: V) -> Result @@ -329,12 +333,11 @@ impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { self.deserialize_bytes(visitor) } - fn deserialize_ignored_any(self, _: V) -> Result + fn deserialize_ignored_any(self, visitor: V) -> Result where V: Visitor<'de> { - println!("i really don't get this API!"); - Err(Error::WontImplement) + self.deserialize_any(visitor) } } @@ -516,4 +519,14 @@ mod test { assert_eq!(de.l, vec![23, 17]); assert_eq!(de.o, Some(42)); } + + #[test] + fn test_struct_extra() { + #[derive(Deserialize, Debug)] + struct A { + a: i64, + } + let de: A = from_str("d1:ai42e1:bi18ee").unwrap(); + assert_eq!(de.a, 42); + } }