2021-07-02 17:58:53 +01:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
2024-03-29 09:55:28 +00:00
|
|
|
use serde::{ser::Impossible, Serialize, Serializer};
|
2021-07-01 23:37:57 +01:00
|
|
|
|
2024-03-29 11:00:58 +00:00
|
|
|
use buffers::ByteBufOwned;
|
2021-07-02 17:58:53 +01:00
|
|
|
|
2021-07-01 23:37:57 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum SerErrorKind {
|
|
|
|
|
Other(anyhow::Error),
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
impl std::fmt::Display for SerErrorKind {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match self {
|
2022-12-08 20:20:23 +00:00
|
|
|
SerErrorKind::Other(e) => write!(f, "{e}"),
|
2021-07-02 10:21:19 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 23:37:57 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct SerError {
|
|
|
|
|
kind: SerErrorKind,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SerError {
|
|
|
|
|
fn custom_with_ser<T: std::fmt::Display, W: std::io::Write>(
|
|
|
|
|
msg: T,
|
2021-07-02 10:21:19 +01:00
|
|
|
_ser: &BencodeSerializer<W>,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Self {
|
|
|
|
|
serde::ser::Error::custom(msg)
|
|
|
|
|
}
|
|
|
|
|
fn from_err_with_ser<E: std::error::Error + Send + Sync + 'static, W: std::io::Write>(
|
|
|
|
|
err: E,
|
2021-07-02 10:21:19 +01:00
|
|
|
_ser: &BencodeSerializer<W>,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> 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 {
|
2021-07-02 10:21:19 +01:00
|
|
|
write!(f, "{}", self.kind)
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct BencodeSerializer<W: std::io::Write> {
|
|
|
|
|
writer: W,
|
2021-07-02 17:58:53 +01:00
|
|
|
hack_no_bytestring_prefix: bool,
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<W: std::io::Write> BencodeSerializer<W> {
|
2021-07-02 17:58:53 +01:00
|
|
|
pub fn new(writer: W) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
writer,
|
|
|
|
|
hack_no_bytestring_prefix: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn write_raw(&mut self, buf: &[u8]) -> Result<(), SerError> {
|
|
|
|
|
self.writer
|
|
|
|
|
.write_all(buf)
|
2021-10-18 16:38:43 +01:00
|
|
|
.map_err(|e| SerError::from_err_with_ser(e, self))
|
2021-07-02 17:58:53 +01:00
|
|
|
}
|
2021-07-01 23:37:57 +01:00
|
|
|
fn write_fmt(&mut self, fmt: core::fmt::Arguments<'_>) -> Result<(), SerError> {
|
|
|
|
|
self.writer
|
|
|
|
|
.write_fmt(fmt)
|
2021-10-18 16:38:43 +01:00
|
|
|
.map_err(|e| SerError::from_err_with_ser(e, self))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
fn write_byte(&mut self, byte: u8) -> Result<(), SerError> {
|
2021-07-02 17:58:53 +01:00
|
|
|
self.write_raw(&[byte])
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
fn write_number<N: std::fmt::Display>(&mut self, number: N) -> Result<(), SerError> {
|
2022-12-08 20:20:23 +00:00
|
|
|
self.write_fmt(format_args!("i{number}e"))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SerError> {
|
2021-07-02 17:58:53 +01:00
|
|
|
if !self.hack_no_bytestring_prefix {
|
|
|
|
|
self.write_fmt(format_args!("{}:", bytes.len()))?;
|
|
|
|
|
}
|
|
|
|
|
self.write_raw(bytes)
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 SerializeMap<'ser, W: std::io::Write> {
|
|
|
|
|
ser: &'ser mut BencodeSerializer<W>,
|
2024-03-29 11:00:58 +00:00
|
|
|
tmp: BTreeMap<ByteBufOwned, ByteBufOwned>,
|
|
|
|
|
last_key: Option<ByteBufOwned>,
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
{
|
2021-07-02 17:58:53 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
let mut ser = BencodeSerializer::new(&mut buf);
|
|
|
|
|
ser.hack_no_bytestring_prefix = true;
|
|
|
|
|
key.serialize(&mut ser)?;
|
2024-03-29 11:00:58 +00:00
|
|
|
self.last_key.replace(ByteBufOwned::from(buf));
|
2021-07-02 17:58:53 +01:00
|
|
|
Ok(())
|
|
|
|
|
// key.serialize(&mut *self.ser);
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
|
|
|
|
where
|
|
|
|
|
T: serde::Serialize,
|
|
|
|
|
{
|
2021-07-02 17:58:53 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
let mut ser = BencodeSerializer::new(&mut buf);
|
|
|
|
|
value.serialize(&mut ser)?;
|
|
|
|
|
self.tmp
|
2024-03-29 11:00:58 +00:00
|
|
|
.insert(self.last_key.take().unwrap(), ByteBufOwned::from(buf));
|
2021-07-02 17:58:53 +01:00
|
|
|
Ok(())
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
2021-07-02 17:58:53 +01:00
|
|
|
for (key, value) in self.tmp {
|
|
|
|
|
self.ser.write_bytes(&key)?;
|
|
|
|
|
self.ser.write_raw(&value)?;
|
|
|
|
|
}
|
2021-07-01 23:37:57 +01:00
|
|
|
self.ser.write_byte(b'e')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct SerializeStruct<'ser, W: std::io::Write> {
|
|
|
|
|
ser: &'ser mut BencodeSerializer<W>,
|
2024-03-29 11:00:58 +00:00
|
|
|
tmp: BTreeMap<&'static str, ByteBufOwned>,
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
{
|
2021-07-02 17:58:53 +01:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
let mut ser = BencodeSerializer::new(&mut buf);
|
|
|
|
|
value.serialize(&mut ser)?;
|
2024-03-29 11:00:58 +00:00
|
|
|
self.tmp.insert(key, ByteBufOwned::from(buf));
|
2021-07-02 17:58:53 +01:00
|
|
|
Ok(())
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
2021-07-02 17:58:53 +01:00
|
|
|
for (key, value) in self.tmp {
|
|
|
|
|
self.ser.write_bytes(key.as_bytes())?;
|
2021-07-03 00:22:46 +01:00
|
|
|
self.ser.write_raw(&value)?;
|
2021-07-02 17:58:53 +01:00
|
|
|
}
|
2021-07-01 23:37:57 +01:00
|
|
|
self.ser.write_byte(b'e')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>;
|
2024-03-29 09:55:28 +00:00
|
|
|
type SerializeTupleStruct = Impossible<(), SerError>;
|
|
|
|
|
type SerializeTupleVariant = Impossible<(), SerError>;
|
2021-07-01 23:37:57 +01:00
|
|
|
type SerializeMap = SerializeMap<'ser, W>;
|
|
|
|
|
type SerializeStruct = SerializeStruct<'ser, W>;
|
2024-03-29 09:55:28 +00:00
|
|
|
type SerializeStructVariant = Impossible<(), SerError>;
|
2021-07-01 23:37:57 +01:00
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support booleans",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support f32",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support f32",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support chars",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ()",
|
2021-10-18 16:38:43 +01:00
|
|
|
self,
|
2021-07-01 23:37:57 +01:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support unit structs",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_unit_variant(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_variant_index: u32,
|
|
|
|
|
_variant: &'static str,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::Ok, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support unit variants",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_value: &T,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::Ok, Self::Error>
|
|
|
|
|
where
|
|
|
|
|
T: serde::Serialize,
|
|
|
|
|
{
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support newtype structs",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_variant_index: u32,
|
|
|
|
|
_variant: &'static str,
|
|
|
|
|
_value: &T,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::Ok, Self::Error>
|
|
|
|
|
where
|
|
|
|
|
T: serde::Serialize,
|
|
|
|
|
{
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support newtype variants",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
self.write_byte(b'l')?;
|
|
|
|
|
Ok(SerializeSeq { ser: self })
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support tuples",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_tuple_struct(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_len: usize,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support tuple structs",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_tuple_variant(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_variant_index: u32,
|
|
|
|
|
_variant: &'static str,
|
|
|
|
|
_len: usize,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support tuple variants",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:21:19 +01:00
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
2021-07-01 23:37:57 +01:00
|
|
|
self.write_byte(b'd')?;
|
2021-07-02 17:58:53 +01:00
|
|
|
Ok(SerializeMap {
|
|
|
|
|
ser: self,
|
|
|
|
|
tmp: Default::default(),
|
|
|
|
|
last_key: None,
|
|
|
|
|
})
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_struct(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_len: usize,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::SerializeStruct, Self::Error> {
|
|
|
|
|
self.write_byte(b'd')?;
|
2021-07-02 17:58:53 +01:00
|
|
|
Ok(SerializeStruct {
|
|
|
|
|
ser: self,
|
|
|
|
|
tmp: Default::default(),
|
|
|
|
|
})
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_struct_variant(
|
|
|
|
|
self,
|
2021-07-02 10:21:19 +01:00
|
|
|
_name: &'static str,
|
|
|
|
|
_variant_index: u32,
|
|
|
|
|
_variant: &'static str,
|
|
|
|
|
_len: usize,
|
2021-07-01 23:37:57 +01:00
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
2024-03-29 09:55:28 +00:00
|
|
|
Err(SerError::custom_with_ser(
|
|
|
|
|
"bencode doesn't support struct variants",
|
|
|
|
|
self,
|
|
|
|
|
))
|
2021-07-01 23:37:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bencode_serialize_to_writer<T: Serialize, W: std::io::Write>(
|
|
|
|
|
value: T,
|
|
|
|
|
writer: &mut W,
|
|
|
|
|
) -> Result<(), SerError> {
|
2021-07-02 17:58:53 +01:00
|
|
|
let mut serializer = BencodeSerializer::new(writer);
|
2021-07-01 23:37:57 +01:00
|
|
|
value.serialize(&mut serializer)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|