Initial private torrents support

This commit is contained in:
Igor Katson 2025-01-13 15:47:13 +00:00
parent bc5e23bf6d
commit 8efd77fce2
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
7 changed files with 50 additions and 16 deletions

View file

@ -218,14 +218,26 @@ impl<'de> serde::de::Deserializer<'de> for &mut BencodeDeserializer<'de> {
}
}
fn deserialize_bool<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
Err(
Error::new_from_kind(ErrorKind::NotSupported("bencode doesn't support booleans"))
.set_context(self),
)
if !self.buf.starts_with(b"i") {
return Err(Error::custom_with_de(
"expected bencode int to represent bool",
self,
));
}
let value = self.parse_integer()?;
if value > 1 {
return Err(Error::custom_with_de(
format!("expected 0 or 1 for boolean, but got {value}"),
self,
));
}
visitor
.visit_bool(value == 1)
.map_err(|e: Self::Error| e.set_context(self))
}
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>

View file

@ -218,11 +218,8 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
type SerializeStruct = SerializeStruct<'ser, W>;
type SerializeStructVariant = Impossible<(), SerError>;
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
Err(SerError::custom_with_ser(
"bencode doesn't support booleans",
self,
))
fn serialize_bool(self, value: bool) -> Result<Self::Ok, Self::Error> {
self.write_number(if value { 1 } else { 0 })
}
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {