Remove panics in bencode serialization
This commit is contained in:
parent
4d6b5181eb
commit
d9ec702f59
3 changed files with 42 additions and 92 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -1318,7 +1318,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "librqbit-bencode"
|
name = "librqbit-bencode"
|
||||||
version = "2.2.1"
|
version = "2.2.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"librqbit-buffers",
|
"librqbit-buffers",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "librqbit-bencode"
|
name = "librqbit-bencode"
|
||||||
version = "2.2.1"
|
version = "2.2.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Bencode serialization and deserialization using Serde"
|
description = "Bencode serialization and deserialization using Serde"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
|
@ -17,8 +17,8 @@ sha1-openssl = ["sha1w/sha1-openssl"]
|
||||||
sha1-rust = ["sha1w/sha1-rust"]
|
sha1-rust = ["sha1w/sha1-rust"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = {version = "1", features=["derive"]}
|
serde = { version = "1", features = ["derive"] }
|
||||||
buffers = {path = "../buffers", package="librqbit-buffers", version = "2.2.1"}
|
buffers = { path = "../buffers", package = "librqbit-buffers", version = "2.2.1" }
|
||||||
clone_to_owned = {path = "../clone_to_owned", package="librqbit-clone-to-owned", version = "2.2.1"}
|
clone_to_owned = { path = "../clone_to_owned", package = "librqbit-clone-to-owned", version = "2.2.1" }
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
sha1w = {path="../sha1w", default-features=false, package="librqbit-sha1-wrapper", version="2.2.1"}
|
sha1w = { path = "../sha1w", default-features = false, package = "librqbit-sha1-wrapper", version = "2.2.1" }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{ser::Impossible, Serialize, Serializer};
|
||||||
|
|
||||||
use buffers::ByteString;
|
use buffers::ByteString;
|
||||||
|
|
||||||
|
|
@ -134,46 +134,6 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeTuple for SerializeTuple<'ser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
struct SerializeMap<'ser, W: std::io::Write> {
|
||||||
ser: &'ser mut BencodeSerializer<W>,
|
ser: &'ser mut BencodeSerializer<W>,
|
||||||
tmp: BTreeMap<ByteString, ByteString>,
|
tmp: BTreeMap<ByteString, ByteString>,
|
||||||
|
|
@ -251,50 +211,16 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeStruct for SerializeStruct<'s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
|
|
||||||
type Error = SerError;
|
type Error = SerError;
|
||||||
|
|
||||||
type SerializeSeq = SerializeSeq<'ser, W>;
|
type SerializeSeq = SerializeSeq<'ser, W>;
|
||||||
|
|
||||||
type SerializeTuple = SerializeTuple<'ser, W>;
|
type SerializeTuple = SerializeTuple<'ser, W>;
|
||||||
|
type SerializeTupleStruct = Impossible<(), SerError>;
|
||||||
type SerializeTupleStruct = SerializeTupleStruct<'ser, W>;
|
type SerializeTupleVariant = Impossible<(), SerError>;
|
||||||
|
|
||||||
type SerializeTupleVariant = SerializeTupleVariant<'ser, W>;
|
|
||||||
|
|
||||||
type SerializeMap = SerializeMap<'ser, W>;
|
type SerializeMap = SerializeMap<'ser, W>;
|
||||||
|
|
||||||
type SerializeStruct = SerializeStruct<'ser, W>;
|
type SerializeStruct = SerializeStruct<'ser, W>;
|
||||||
|
type SerializeStructVariant = Impossible<(), SerError>;
|
||||||
type SerializeStructVariant = SerializeStructVariant<'ser, W>;
|
|
||||||
|
|
||||||
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
||||||
Err(SerError::custom_with_ser(
|
Err(SerError::custom_with_ser(
|
||||||
|
|
@ -386,7 +312,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support unit structs",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_variant(
|
fn serialize_unit_variant(
|
||||||
|
|
@ -395,7 +324,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
_variant_index: u32,
|
_variant_index: u32,
|
||||||
_variant: &'static str,
|
_variant: &'static str,
|
||||||
) -> Result<Self::Ok, Self::Error> {
|
) -> Result<Self::Ok, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support unit variants",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_struct<T: ?Sized>(
|
fn serialize_newtype_struct<T: ?Sized>(
|
||||||
|
|
@ -406,7 +338,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
where
|
where
|
||||||
T: serde::Serialize,
|
T: serde::Serialize,
|
||||||
{
|
{
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support newtype structs",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_newtype_variant<T: ?Sized>(
|
fn serialize_newtype_variant<T: ?Sized>(
|
||||||
|
|
@ -419,7 +354,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
where
|
where
|
||||||
T: serde::Serialize,
|
T: serde::Serialize,
|
||||||
{
|
{
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support newtype variants",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||||
|
|
@ -428,7 +366,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support tuples",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_struct(
|
fn serialize_tuple_struct(
|
||||||
|
|
@ -436,7 +377,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
_len: usize,
|
_len: usize,
|
||||||
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support tuple structs",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
|
|
@ -446,7 +390,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
_variant: &'static str,
|
_variant: &'static str,
|
||||||
_len: usize,
|
_len: usize,
|
||||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support tuple variants",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||||
|
|
@ -477,7 +424,10 @@ impl<'ser, W: std::io::Write> Serializer for &'ser mut BencodeSerializer<W> {
|
||||||
_variant: &'static str,
|
_variant: &'static str,
|
||||||
_len: usize,
|
_len: usize,
|
||||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||||
todo!()
|
Err(SerError::custom_with_ser(
|
||||||
|
"bencode doesn't support struct variants",
|
||||||
|
self,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue