Skip to content

Commit

Permalink
Merge pull request #25 from madadam/issue-16
Browse files Browse the repository at this point in the history
Fix deserialization of nested tuple
  • Loading branch information
madadam authored Oct 28, 2021
2 parents 06bb9a6 + 0c6c144 commit 3721c5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ enum ParseResult {
End,
}

impl ParseResult {
fn to_unexpected_error(&self, expected: &str) -> Error {
match self {
Self::Int(i) => Error::invalid_type(Unexpected::Signed(*i), &expected),
Self::Bytes(bytes) => Error::invalid_type(Unexpected::Bytes(bytes), &expected),
Self::List => Error::invalid_type(Unexpected::Seq, &expected),
Self::Map => Error::invalid_type(Unexpected::Map, &expected),
Self::End => Error::custom(format_args!("unexpected end, expected {}", expected)),
}
}
}

/// A structure for deserializing bencode into Rust values.
#[derive(Debug)]
pub struct Deserializer<R: Read> {
Expand Down Expand Up @@ -246,9 +258,8 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
}

forward_to_deserialize_any! {
i64 seq bool i8 i16 i32 u8 u16 u32
u64 f32 f64 char unit bytes byte_buf map unit_struct tuple_struct tuple
ignored_any struct
bool char i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 unit bytes byte_buf seq map unit_struct
tuple_struct ignored_any struct
}

#[inline]
Expand Down Expand Up @@ -287,10 +298,7 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
{
let bytes = self.parse().and_then(|r| match r {
ParseResult::Bytes(bytes) => Ok(bytes),
ParseResult::Int(i) => Err(Error::invalid_type(Unexpected::Signed(i), &"Bytes")),
ParseResult::List => Err(Error::invalid_type(Unexpected::Seq, &"Bytes")),
ParseResult::Map => Err(Error::invalid_type(Unexpected::Map, &"Bytes")),
ParseResult::End => Err(Error::EndOfStream),
_ => Err(r.to_unexpected_error("bytes")),
})?;

let s = str::from_utf8(&bytes)
Expand All @@ -311,6 +319,18 @@ impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut Deserializer<R> {
{
self.deserialize_str(visitor)
}

fn deserialize_tuple<V>(self, size: usize, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
self.parse().and_then(|r| match r {
ParseResult::List => Ok(()),
_ => Err(r.to_unexpected_error("list")),
})?;

visitor.visit_seq(BencodeAccess::new(self, Some(size)))
}
}

/// Deserialize an instance of type `T` from a string of bencode.
Expand Down
6 changes: 6 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,9 @@ fn ser_de_flattened_adjacently_tagged_enum() {
body: Body::Request { token: 456 },
});
}

// https://github.com/toby/serde-bencode/issues/16 (simplified)
#[test]
fn ser_de_vec_of_tuples() {
test_ser_de_eq(vec![(1, 2), (3, 4)]);
}

0 comments on commit 3721c5e

Please sign in to comment.