Make a serde bencode serializer
This commit is contained in:
parent
563ca78774
commit
5f60f9e1b4
11 changed files with 2436 additions and 142 deletions
1
crates/librqbit/.gitignore
vendored
Normal file
1
crates/librqbit/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target/
|
||||||
1741
crates/librqbit/Cargo.lock
generated
Normal file
1741
crates/librqbit/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
197
crates/librqbit/src/bencode_value.rs
Normal file
197
crates/librqbit/src/bencode_value.rs
Normal file
|
|
@ -0,0 +1,197 @@
|
||||||
|
use std::{collections::HashMap, marker::PhantomData};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
buffers::{ByteBuf, ByteString},
|
||||||
|
clone_to_owned::CloneToOwned,
|
||||||
|
serde_bencode_de::from_bytes,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn dyn_from_bytes<'de, ByteBuf>(buf: &'de [u8]) -> anyhow::Result<BencodeValue<ByteBuf>>
|
||||||
|
where
|
||||||
|
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
||||||
|
{
|
||||||
|
from_bytes(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<ByteBuf: serde::Serialize + Eq + std::hash::Hash> serde::Serialize for BencodeValue<ByteBuf> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
match self {
|
||||||
|
BencodeValue::Bytes(b) => b.serialize(serializer),
|
||||||
|
BencodeValue::Integer(v) => v.serialize(serializer),
|
||||||
|
BencodeValue::List(l) => l.serialize(serializer),
|
||||||
|
BencodeValue::Dict(d) => d.serialize(serializer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, ByteBuf> serde::de::Deserialize<'de> for BencodeValue<ByteBuf>
|
||||||
|
where
|
||||||
|
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
||||||
|
{
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct Visitor<ByteBuf> {
|
||||||
|
buftype: PhantomData<ByteBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, ByteBuf> serde::de::Visitor<'de> for Visitor<ByteBuf>
|
||||||
|
where
|
||||||
|
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
||||||
|
{
|
||||||
|
type Value = BencodeValue<ByteBuf>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
write!(formatter, "a bencode value")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
Ok(BencodeValue::Integer(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: serde::de::SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut v = Vec::new();
|
||||||
|
while let Some(value) = seq.next_element()? {
|
||||||
|
v.push(value);
|
||||||
|
}
|
||||||
|
Ok(BencodeValue::List(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
||||||
|
where
|
||||||
|
E: serde::de::Error,
|
||||||
|
{
|
||||||
|
Ok(BencodeValue::Bytes(ByteBuf::from(v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: serde::de::MapAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut hmap = HashMap::new();
|
||||||
|
while let Some(key) = map.next_key()? {
|
||||||
|
let value = map.next_value()?;
|
||||||
|
hmap.insert(key, value);
|
||||||
|
}
|
||||||
|
Ok(BencodeValue::Dict(hmap))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_any(Visitor {
|
||||||
|
buftype: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A dynamic value when we don't know exactly what we are deserializing.
|
||||||
|
// Useful for debugging.
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
pub enum BencodeValue<ByteBuf: std::hash::Hash + Eq> {
|
||||||
|
Bytes(ByteBuf),
|
||||||
|
Integer(i64),
|
||||||
|
List(Vec<BencodeValue<ByteBuf>>),
|
||||||
|
Dict(HashMap<ByteBuf, BencodeValue<ByteBuf>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<ByteBuf: std::fmt::Debug + std::hash::Hash + Eq> std::fmt::Debug for BencodeValue<ByteBuf> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
BencodeValue::Bytes(b) => std::fmt::Debug::fmt(b, f),
|
||||||
|
BencodeValue::Integer(i) => std::fmt::Debug::fmt(i, f),
|
||||||
|
BencodeValue::List(l) => std::fmt::Debug::fmt(l, f),
|
||||||
|
BencodeValue::Dict(d) => std::fmt::Debug::fmt(d, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<ByteBuf> CloneToOwned for BencodeValue<ByteBuf>
|
||||||
|
where
|
||||||
|
ByteBuf: CloneToOwned + std::hash::Hash + Eq,
|
||||||
|
<ByteBuf as CloneToOwned>::Target: Eq + std::hash::Hash,
|
||||||
|
{
|
||||||
|
type Target = BencodeValue<<ByteBuf as CloneToOwned>::Target>;
|
||||||
|
|
||||||
|
fn clone_to_owned(&self) -> Self::Target {
|
||||||
|
match self {
|
||||||
|
BencodeValue::Bytes(b) => BencodeValue::Bytes(b.clone_to_owned()),
|
||||||
|
BencodeValue::Integer(i) => BencodeValue::Integer(*i),
|
||||||
|
BencodeValue::List(l) => BencodeValue::List(l.clone_to_owned()),
|
||||||
|
BencodeValue::Dict(d) => BencodeValue::Dict(d.clone_to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type BencodeValueBorrowed<'a> = BencodeValue<ByteBuf<'a>>;
|
||||||
|
pub type BencodeValueOwned = BencodeValue<ByteString>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::serde_bencode_ser::bencode_serialize_to_writer;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use serde::Serialize;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_deserialize_torrent_dyn() {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent";
|
||||||
|
std::fs::File::open(filename)
|
||||||
|
.unwrap()
|
||||||
|
.read_to_end(&mut buf)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let torrent_borrowed: BencodeValueBorrowed = from_bytes(&buf).unwrap();
|
||||||
|
let torrent_owned: BencodeValueOwned = from_bytes(&buf).unwrap();
|
||||||
|
dbg!(torrent_borrowed);
|
||||||
|
dbg!(torrent_owned);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serialize_torrent_dyn() {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent";
|
||||||
|
std::fs::File::open(filename)
|
||||||
|
.unwrap()
|
||||||
|
.read_to_end(&mut buf)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let torrent: BencodeValueBorrowed = from_bytes(&buf).unwrap();
|
||||||
|
|
||||||
|
let mut buf = Vec::<u8>::new();
|
||||||
|
bencode_serialize_to_writer(&torrent, &mut buf).unwrap();
|
||||||
|
|
||||||
|
let new_torrent = from_bytes(&buf).unwrap();
|
||||||
|
assert_eq!(torrent, new_torrent);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serialize_struct_with_option() {
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Test {
|
||||||
|
f1: i64,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
missing: Option<i64>,
|
||||||
|
}
|
||||||
|
let test = Test {
|
||||||
|
f1: 100,
|
||||||
|
missing: None,
|
||||||
|
};
|
||||||
|
let mut buf = Vec::<u8>::new();
|
||||||
|
bencode_serialize_to_writer(&test, &mut buf).unwrap();
|
||||||
|
assert_eq!(&buf, b"d2:f1i100ee");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -123,6 +123,24 @@ impl From<Vec<u8>> for ByteString {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> serde::ser::Serialize for ByteBuf<'a> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(self.as_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::ser::Serialize for ByteString {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_bytes(self.as_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de> serde::de::Deserialize<'de> for ByteString {
|
impl<'de> serde::de::Deserialize<'de> for ByteString {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where
|
where
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod bencode_value;
|
||||||
pub mod buffers;
|
pub mod buffers;
|
||||||
pub mod chunk_tracker;
|
pub mod chunk_tracker;
|
||||||
pub mod clone_to_owned;
|
pub mod clone_to_owned;
|
||||||
|
|
@ -9,7 +10,8 @@ pub mod peer_binary_protocol;
|
||||||
pub mod peer_connection;
|
pub mod peer_connection;
|
||||||
pub mod peer_id;
|
pub mod peer_id;
|
||||||
pub mod peer_state;
|
pub mod peer_state;
|
||||||
pub mod serde_bencode;
|
pub mod serde_bencode_de;
|
||||||
|
pub mod serde_bencode_ser;
|
||||||
pub mod sha1w;
|
pub mod sha1w;
|
||||||
pub mod spawn_utils;
|
pub mod spawn_utils;
|
||||||
pub mod speed_estimator;
|
pub mod speed_estimator;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
|
use std::{collections::HashMap, marker::PhantomData};
|
||||||
|
|
||||||
use bincode::Options;
|
use bincode::Options;
|
||||||
use byteorder::{ByteOrder, BE};
|
use byteorder::{ByteOrder, BE};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
bencode_value::BencodeValue,
|
||||||
buffers::{ByteBuf, ByteString},
|
buffers::{ByteBuf, ByteString},
|
||||||
clone_to_owned::CloneToOwned,
|
clone_to_owned::CloneToOwned,
|
||||||
constants::CHUNK_SIZE,
|
constants::CHUNK_SIZE,
|
||||||
|
|
@ -36,6 +39,7 @@ const MSGID_HAVE: u8 = 4;
|
||||||
const MSGID_BITFIELD: u8 = 5;
|
const MSGID_BITFIELD: u8 = 5;
|
||||||
const MSGID_REQUEST: u8 = 6;
|
const MSGID_REQUEST: u8 = 6;
|
||||||
const MSGID_PIECE: u8 = 7;
|
const MSGID_PIECE: u8 = 7;
|
||||||
|
const MSGID_EXTENDED: u8 = 20;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum MessageDeserializeError {
|
pub enum MessageDeserializeError {
|
||||||
|
|
@ -155,7 +159,7 @@ impl std::error::Error for MessageDeserializeError {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Message<ByteBuf> {
|
pub enum Message<ByteBuf: std::hash::Hash + Eq> {
|
||||||
Request(Request),
|
Request(Request),
|
||||||
Bitfield(ByteBuf),
|
Bitfield(ByteBuf),
|
||||||
KeepAlive,
|
KeepAlive,
|
||||||
|
|
@ -165,6 +169,7 @@ pub enum Message<ByteBuf> {
|
||||||
Interested,
|
Interested,
|
||||||
NotInterested,
|
NotInterested,
|
||||||
Piece(Piece<ByteBuf>),
|
Piece(Piece<ByteBuf>),
|
||||||
|
Extended(ExtendedMessage<ByteBuf>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type MessageBorrowed<'a> = Message<ByteBuf<'a>>;
|
pub type MessageBorrowed<'a> = Message<ByteBuf<'a>>;
|
||||||
|
|
@ -177,7 +182,11 @@ pub struct Bitfield<'a> {
|
||||||
pub data: BitfieldBorrowed<'a>,
|
pub data: BitfieldBorrowed<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ByteBuf: CloneToOwned> CloneToOwned for Message<ByteBuf> {
|
impl<ByteBuf> CloneToOwned for Message<ByteBuf>
|
||||||
|
where
|
||||||
|
ByteBuf: CloneToOwned + std::hash::Hash + Eq,
|
||||||
|
<ByteBuf as CloneToOwned>::Target: std::hash::Hash + Eq,
|
||||||
|
{
|
||||||
type Target = Message<<ByteBuf as CloneToOwned>::Target>;
|
type Target = Message<<ByteBuf as CloneToOwned>::Target>;
|
||||||
|
|
||||||
fn clone_to_owned(&self) -> Self::Target {
|
fn clone_to_owned(&self) -> Self::Target {
|
||||||
|
|
@ -195,6 +204,7 @@ impl<ByteBuf: CloneToOwned> CloneToOwned for Message<ByteBuf> {
|
||||||
Message::KeepAlive => Message::KeepAlive,
|
Message::KeepAlive => Message::KeepAlive,
|
||||||
Message::Have(v) => Message::Have(*v),
|
Message::Have(v) => Message::Have(*v),
|
||||||
Message::NotInterested => Message::NotInterested,
|
Message::NotInterested => Message::NotInterested,
|
||||||
|
Message::Extended(e) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -218,7 +228,7 @@ impl<'a> std::fmt::Debug for Bitfield<'a> {
|
||||||
|
|
||||||
impl<ByteBuf> Message<ByteBuf>
|
impl<ByteBuf> Message<ByteBuf>
|
||||||
where
|
where
|
||||||
ByteBuf: AsRef<[u8]>,
|
ByteBuf: AsRef<[u8]> + std::hash::Hash + Eq,
|
||||||
{
|
{
|
||||||
pub fn len_prefix_and_msg_id(&self) -> (u32, u8) {
|
pub fn len_prefix_and_msg_id(&self) -> (u32, u8) {
|
||||||
match self {
|
match self {
|
||||||
|
|
@ -234,6 +244,7 @@ where
|
||||||
),
|
),
|
||||||
Message::KeepAlive => (LEN_PREFIX_KEEPALIVE, 0),
|
Message::KeepAlive => (LEN_PREFIX_KEEPALIVE, 0),
|
||||||
Message::Have(_) => (LEN_PREFIX_HAVE, MSGID_HAVE),
|
Message::Have(_) => (LEN_PREFIX_HAVE, MSGID_HAVE),
|
||||||
|
Message::Extended(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn serialize(&self, out: &mut Vec<u8>) -> usize {
|
pub fn serialize(&self, out: &mut Vec<u8>) -> usize {
|
||||||
|
|
@ -284,6 +295,7 @@ where
|
||||||
BE::write_u32(&mut out[PREAMBLE_LEN..], *v);
|
BE::write_u32(&mut out[PREAMBLE_LEN..], *v);
|
||||||
msg_len
|
msg_len
|
||||||
}
|
}
|
||||||
|
Message::Extended(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn deserialize<'a>(
|
pub fn deserialize<'a>(
|
||||||
|
|
@ -481,6 +493,19 @@ impl Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ExtendedMessage<ByteBuf: std::hash::Hash + Eq> {
|
||||||
|
Dyn(BencodeValue<ByteBuf>),
|
||||||
|
Unimplemented(PhantomData<ByteBuf>),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ExtendedHandshake<ByteBuf: Eq + std::hash::Hash> {
|
||||||
|
m: HashMap<ByteBuf, BencodeValue<ByteBuf>>,
|
||||||
|
p: Option<u32>,
|
||||||
|
v: Option<ByteBuf>,
|
||||||
|
// _phantom: PhantomData<&'a ()>,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,12 @@ impl PeerConnection {
|
||||||
Message::NotInterested => {
|
Message::NotInterested => {
|
||||||
info!("received \"not interested\", but we don't care yet")
|
info!("received \"not interested\", but we don't care yet")
|
||||||
}
|
}
|
||||||
|
message => {
|
||||||
|
warn!(
|
||||||
|
"{}: received unsupported message {:?}, ignoring",
|
||||||
|
handle, message
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if read_so_far > size {
|
if read_so_far > size {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use serde::de::Deserializer;
|
use serde::de::Deserializer;
|
||||||
use serde::de::Error as DeError;
|
use serde::de::Error as DeError;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use serde::Serializer;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
|
@ -88,13 +89,6 @@ where
|
||||||
Ok(T::deserialize(&mut de)?)
|
Ok(T::deserialize(&mut de)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dyn_from_bytes<'de, ByteBuf>(buf: &'de [u8]) -> anyhow::Result<BencodeValue<ByteBuf>>
|
|
||||||
where
|
|
||||||
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
|
||||||
{
|
|
||||||
from_bytes(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ErrorKind {
|
enum ErrorKind {
|
||||||
Other(anyhow::Error),
|
Other(anyhow::Error),
|
||||||
|
|
@ -562,130 +556,3 @@ impl<'a, 'de> serde::de::SeqAccess<'de> for SeqAccess<'a, 'de> {
|
||||||
Ok(Some(seed.deserialize(&mut *self.de)?))
|
Ok(Some(seed.deserialize(&mut *self.de)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, ByteBuf> serde::de::Deserialize<'de> for BencodeValue<ByteBuf>
|
|
||||||
where
|
|
||||||
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
|
||||||
{
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
struct Visitor<ByteBuf> {
|
|
||||||
buftype: PhantomData<ByteBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, ByteBuf> serde::de::Visitor<'de> for Visitor<ByteBuf>
|
|
||||||
where
|
|
||||||
ByteBuf: From<&'de [u8]> + Deserialize<'de> + std::hash::Hash + Eq,
|
|
||||||
{
|
|
||||||
type Value = BencodeValue<ByteBuf>;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
write!(formatter, "a bencode value")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
Ok(BencodeValue::Integer(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
||||||
where
|
|
||||||
A: serde::de::SeqAccess<'de>,
|
|
||||||
{
|
|
||||||
let mut v = Vec::new();
|
|
||||||
while let Some(value) = seq.next_element()? {
|
|
||||||
v.push(value);
|
|
||||||
}
|
|
||||||
Ok(BencodeValue::List(v))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: serde::de::Error,
|
|
||||||
{
|
|
||||||
Ok(BencodeValue::Bytes(ByteBuf::from(v)))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
|
||||||
where
|
|
||||||
A: serde::de::MapAccess<'de>,
|
|
||||||
{
|
|
||||||
let mut hmap = HashMap::new();
|
|
||||||
while let Some(key) = map.next_key()? {
|
|
||||||
let value = map.next_value()?;
|
|
||||||
hmap.insert(key, value);
|
|
||||||
}
|
|
||||||
Ok(BencodeValue::Dict(hmap))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_any(Visitor {
|
|
||||||
buftype: PhantomData,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A dynamic value when we don't know exactly what we are deserializing.
|
|
||||||
// Useful for debugging.
|
|
||||||
pub enum BencodeValue<ByteBuf> {
|
|
||||||
Bytes(ByteBuf),
|
|
||||||
Integer(i64),
|
|
||||||
List(Vec<BencodeValue<ByteBuf>>),
|
|
||||||
Dict(HashMap<ByteBuf, BencodeValue<ByteBuf>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<ByteBuf: std::fmt::Debug> std::fmt::Debug for BencodeValue<ByteBuf> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
BencodeValue::Bytes(b) => std::fmt::Debug::fmt(b, f),
|
|
||||||
BencodeValue::Integer(i) => std::fmt::Debug::fmt(i, f),
|
|
||||||
BencodeValue::List(l) => std::fmt::Debug::fmt(l, f),
|
|
||||||
BencodeValue::Dict(d) => std::fmt::Debug::fmt(d, f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<ByteBuf> CloneToOwned for BencodeValue<ByteBuf>
|
|
||||||
where
|
|
||||||
ByteBuf: CloneToOwned,
|
|
||||||
<ByteBuf as CloneToOwned>::Target: Eq + std::hash::Hash,
|
|
||||||
{
|
|
||||||
type Target = BencodeValue<<ByteBuf as CloneToOwned>::Target>;
|
|
||||||
|
|
||||||
fn clone_to_owned(&self) -> Self::Target {
|
|
||||||
match self {
|
|
||||||
BencodeValue::Bytes(b) => BencodeValue::Bytes(b.clone_to_owned()),
|
|
||||||
BencodeValue::Integer(i) => BencodeValue::Integer(*i),
|
|
||||||
BencodeValue::List(l) => BencodeValue::List(l.clone_to_owned()),
|
|
||||||
BencodeValue::Dict(d) => BencodeValue::Dict(d.clone_to_owned()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type DynBencodeNodeBorrowed<'a> = BencodeValue<ByteBuf<'a>>;
|
|
||||||
pub type DynBencodeNodeOwned = BencodeValue<ByteString>;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_torrent_dyn() {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
let filename = "resources/ubuntu-21.04-desktop-amd64.iso.torrent";
|
|
||||||
std::fs::File::open(filename)
|
|
||||||
.unwrap()
|
|
||||||
.read_to_end(&mut buf)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let torrent_borrowed: DynBencodeNodeBorrowed = from_bytes(&buf).unwrap();
|
|
||||||
let torrent_owned: DynBencodeNodeOwned = from_bytes(&buf).unwrap();
|
|
||||||
dbg!(torrent_borrowed);
|
|
||||||
dbg!(torrent_owned);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
437
crates/librqbit/src/serde_bencode_ser.rs
Normal file
437
crates/librqbit/src/serde_bencode_ser.rs
Normal file
|
|
@ -0,0 +1,437 @@
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SerErrorKind {
|
||||||
|
Other(anyhow::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SerError {
|
||||||
|
kind: SerErrorKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerError {
|
||||||
|
fn custom_with_ser<T: std::fmt::Display, W: std::io::Write>(
|
||||||
|
msg: T,
|
||||||
|
ser: &BencodeSerializer<W>,
|
||||||
|
) -> Self {
|
||||||
|
serde::ser::Error::custom(msg)
|
||||||
|
}
|
||||||
|
fn from_err_with_ser<E: std::error::Error + Send + Sync + 'static, W: std::io::Write>(
|
||||||
|
err: E,
|
||||||
|
ser: &BencodeSerializer<W>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
kind: SerErrorKind::Other(err.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::ser::Error for SerError {
|
||||||
|
fn custom<T>(msg: T) -> Self
|
||||||
|
where
|
||||||
|
T: std::fmt::Display,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
kind: SerErrorKind::Other(anyhow::anyhow!("{}", msg)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for SerError {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for SerError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BencodeSerializer<W: std::io::Write> {
|
||||||
|
writer: W,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: std::io::Write> BencodeSerializer<W> {
|
||||||
|
fn write_fmt(&mut self, fmt: core::fmt::Arguments<'_>) -> Result<(), SerError> {
|
||||||
|
self.writer
|
||||||
|
.write_fmt(fmt)
|
||||||
|
.map_err(|e| SerError::from_err_with_ser(e, &self))
|
||||||
|
}
|
||||||
|
fn write_byte(&mut self, byte: u8) -> Result<(), SerError> {
|
||||||
|
self.writer
|
||||||
|
.write_all(&[byte])
|
||||||
|
.map_err(|e| SerError::from_err_with_ser(e, &self))
|
||||||
|
}
|
||||||
|
fn write_number<N: std::fmt::Display>(&mut self, number: N) -> Result<(), SerError> {
|
||||||
|
self.write_fmt(format_args!("i{}e", number))
|
||||||
|
}
|
||||||
|
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SerError> {
|
||||||
|
self.write_fmt(format_args!("{}:", bytes.len()))?;
|
||||||
|
self.writer
|
||||||
|
.write_all(bytes)
|
||||||
|
.map_err(|e| SerError::from_err_with_ser(e, &self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeSeq<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeSeq for SerializeSeq<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
value.serialize(&mut *self.ser)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.ser.write_byte(b'e')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeTuple<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeTuple for SerializeTuple<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
value.serialize(&mut *self.ser)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.ser.write_byte(b'e')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeTupleStruct<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeTupleStruct for SerializeTupleStruct<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeTupleVariant<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeMap<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeMap for SerializeMap<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
key.serialize(&mut *self.ser)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
value.serialize(&mut *self.ser)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.ser.write_byte(b'e')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeStruct<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeStruct for SerializeStruct<'ser, W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_field<T: ?Sized>(
|
||||||
|
&mut self,
|
||||||
|
key: &'static str,
|
||||||
|
value: &T,
|
||||||
|
) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
self.ser.write_bytes(key.as_bytes())?;
|
||||||
|
value.serialize(&mut *self.ser)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.ser.write_byte(b'e')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializeStructVariant<'ser, W: std::io::Write> {
|
||||||
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
|
}
|
||||||
|
impl<'ser, W: std::io::Write> serde::ser::SerializeStructVariant
|
||||||
|
for SerializeStructVariant<'ser, W>
|
||||||
|
{
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
fn serialize_field<T: ?Sized>(
|
||||||
|
&mut self,
|
||||||
|
key: &'static str,
|
||||||
|
value: &T,
|
||||||
|
) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
|
type Ok = ();
|
||||||
|
|
||||||
|
type Error = SerError;
|
||||||
|
|
||||||
|
type SerializeSeq = SerializeSeq<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeTuple = SerializeTuple<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeTupleStruct = SerializeTupleStruct<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeTupleVariant = SerializeTupleVariant<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeMap = SerializeMap<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeStruct = SerializeStruct<'ser, W>;
|
||||||
|
|
||||||
|
type SerializeStructVariant = SerializeStructVariant<'ser, W>;
|
||||||
|
|
||||||
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support booleans",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_number(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support f32",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support f32",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support chars",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_bytes(v.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
||||||
|
self.write_bytes(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support None",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
value.serialize(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
||||||
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support Rust unit ()",
|
||||||
|
&self,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_unit_variant(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
variant_index: u32,
|
||||||
|
variant: &'static str,
|
||||||
|
) -> Result<Self::Ok, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_newtype_struct<T: ?Sized>(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
value: &T,
|
||||||
|
) -> Result<Self::Ok, Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_newtype_variant<T: ?Sized>(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
variant_index: u32,
|
||||||
|
variant: &'static str,
|
||||||
|
value: &T,
|
||||||
|
) -> Result<Self::Ok, Self::Error>
|
||||||
|
where
|
||||||
|
T: serde::Serialize,
|
||||||
|
{
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||||
|
self.write_byte(b'l')?;
|
||||||
|
Ok(SerializeSeq { ser: self })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple_struct(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
len: usize,
|
||||||
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tuple_variant(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
variant_index: u32,
|
||||||
|
variant: &'static str,
|
||||||
|
len: usize,
|
||||||
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||||
|
self.write_byte(b'd')?;
|
||||||
|
Ok(SerializeMap { ser: self })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_struct(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
len: usize,
|
||||||
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||||
|
self.write_byte(b'd')?;
|
||||||
|
Ok(SerializeStruct { ser: self })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_struct_variant(
|
||||||
|
self,
|
||||||
|
name: &'static str,
|
||||||
|
variant_index: u32,
|
||||||
|
variant: &'static str,
|
||||||
|
len: usize,
|
||||||
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bencode_serialize_to_writer<T: Serialize, W: std::io::Write>(
|
||||||
|
value: T,
|
||||||
|
writer: &mut W,
|
||||||
|
) -> Result<(), SerError> {
|
||||||
|
let mut serializer = BencodeSerializer { writer };
|
||||||
|
value.serialize(&mut serializer)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -301,7 +301,7 @@ impl TorrentManager {
|
||||||
anyhow::bail!("tracker responded with {:?}", response.status());
|
anyhow::bail!("tracker responded with {:?}", response.status());
|
||||||
}
|
}
|
||||||
let bytes = response.bytes().await?;
|
let bytes = response.bytes().await?;
|
||||||
match crate::serde_bencode::from_bytes::<TrackerError>(&bytes) {
|
match crate::serde_bencode_de::from_bytes::<TrackerError>(&bytes) {
|
||||||
Ok(error) => anyhow::bail!(
|
Ok(error) => anyhow::bail!(
|
||||||
"tracker returned failure. Failure reason: {}",
|
"tracker returned failure. Failure reason: {}",
|
||||||
error.failure_reason
|
error.failure_reason
|
||||||
|
|
@ -310,7 +310,7 @@ impl TorrentManager {
|
||||||
// ignore, assume ok response
|
// ignore, assume ok response
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let response = crate::serde_bencode::from_bytes::<TrackerResponse>(&bytes)?;
|
let response = crate::serde_bencode_de::from_bytes::<TrackerResponse>(&bytes)?;
|
||||||
|
|
||||||
for peer in response.peers.iter_sockaddrs() {
|
for peer in response.peers.iter_sockaddrs() {
|
||||||
self.state.add_peer_if_not_seen(peer);
|
self.state.add_peer_if_not_seen(peer);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use serde::Deserialize;
|
||||||
use crate::{
|
use crate::{
|
||||||
buffers::{ByteBuf, ByteString},
|
buffers::{ByteBuf, ByteString},
|
||||||
clone_to_owned::CloneToOwned,
|
clone_to_owned::CloneToOwned,
|
||||||
serde_bencode::BencodeDeserializer,
|
serde_bencode_de::BencodeDeserializer,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
pub type TorrentMetaV1Borrowed<'a> = TorrentMetaV1<ByteBuf<'a>>;
|
||||||
|
|
@ -240,7 +240,7 @@ where
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use crate::serde_bencode::from_bytes;
|
use crate::serde_bencode_de::from_bytes;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue