[bencode/de] Implemented any deserialization

This commit is contained in:
Fabian 2024-08-11 20:40:54 +02:00
parent 7556471090
commit 00e84033b4

View File

@ -81,12 +81,16 @@ impl<'de> Deserializer<'de> {
impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
type Error = Error; type Error = Error;
fn deserialize_any<V>(self, _: V) -> Result<V::Value> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where where
V: Visitor<'de> V: Visitor<'de>
{ {
println!("i don't get this API!"); match self.peek_byte()? as char {
Err(Error::WontImplement) 'i' => self.deserialize_i64(visitor),
'l' => self.deserialize_seq(visitor),
'd' => self.deserialize_map(visitor),
_ => Err(Error::Syntax),
}
} }
fn deserialize_bool<V>(self, _: V) -> Result<V::Value> fn deserialize_bool<V>(self, _: V) -> Result<V::Value>
@ -329,12 +333,11 @@ impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.deserialize_bytes(visitor) self.deserialize_bytes(visitor)
} }
fn deserialize_ignored_any<V>(self, _: V) -> Result<V::Value> fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
where where
V: Visitor<'de> V: Visitor<'de>
{ {
println!("i really don't get this API!"); self.deserialize_any(visitor)
Err(Error::WontImplement)
} }
} }
@ -516,4 +519,14 @@ mod test {
assert_eq!(de.l, vec![23, 17]); assert_eq!(de.l, vec![23, 17]);
assert_eq!(de.o, Some(42)); 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);
}
} }