[bencode/ser] Implemented sorting of dictionaries

This commit is contained in:
Fabian 2024-08-04 22:55:31 +02:00
parent e7553e569d
commit 0264c133ac

View File

@ -8,13 +8,19 @@ pub struct Serializer {
output: Vec<u8>, output: Vec<u8>,
} }
impl Serializer {
fn new() -> Self {
Serializer {
output: Vec::new(),
}
}
}
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>> pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
where where
T: Serialize + ?Sized T: Serialize + ?Sized
{ {
let mut serializer = Serializer{ let mut serializer = Serializer::new();
output: Vec::new(),
};
value.serialize(&mut serializer)?; value.serialize(&mut serializer)?;
Ok(serializer.output) Ok(serializer.output)
} }
@ -27,7 +33,12 @@ where
Ok(std::str::from_utf8(&bytes[..])?.to_string()) Ok(std::str::from_utf8(&bytes[..])?.to_string())
} }
pub struct MapSerializer<'a> {
super_serializer: &'a mut Serializer,
sorted_map: BTreeMap<Vec<u8>, Vec<u8>>,
last_inserted_key: Vec<u8>,
value_serializer: Serializer,
}
impl<'a> ser::Serializer for &'a mut Serializer impl<'a> ser::Serializer for &'a mut Serializer
{ {
@ -39,7 +50,7 @@ impl<'a> ser::Serializer for &'a mut Serializer
type SerializeTuple = Self; type SerializeTuple = Self;
type SerializeTupleStruct = Self; type SerializeTupleStruct = Self;
type SerializeTupleVariant = Self; type SerializeTupleVariant = Self;
type SerializeMap = Self; type SerializeMap = MapSerializer<'a>;
type SerializeStruct = Self; type SerializeStruct = Self;
type SerializeStructVariant = Self; type SerializeStructVariant = Self;
@ -177,7 +188,12 @@ impl<'a> ser::Serializer for &'a mut Serializer
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
self.output.push('d' as u8); self.output.push('d' as u8);
Ok(self) Ok(MapSerializer{
super_serializer: self,
sorted_map: BTreeMap::new(),
last_inserted_key: Vec::new(),
value_serializer: Serializer::new(),
})
} }
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> { fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
@ -226,7 +242,7 @@ impl <'a> ser::SerializeTupleStruct for &'a mut Serializer {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()> fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
where where
T: ?Sized + Serialize T: ?Sized + Serialize
{ {
@ -242,7 +258,7 @@ impl <'a> ser::SerializeTupleVariant for &'a mut Serializer {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()> fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
where where
T: ?Sized + Serialize T: ?Sized + Serialize
{ {
@ -254,7 +270,7 @@ impl <'a> ser::SerializeTupleVariant for &'a mut Serializer {
} }
} }
impl <'a> ser::SerializeMap for &'a mut Serializer { impl <'a> ser::SerializeMap for MapSerializer<'a> {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
@ -262,19 +278,30 @@ impl <'a> ser::SerializeMap for &'a mut Serializer {
where where
T: ?Sized + Serialize T: ?Sized + Serialize
{ {
key.serialize(&mut **self) key.serialize(&mut self.value_serializer)?;
self.last_inserted_key = self.value_serializer.output.clone();
self.sorted_map.insert(self.value_serializer.output.clone(), Vec::new());
self.value_serializer.output.clear();
Ok(())
} }
fn serialize_value<T>(&mut self, value: &T) -> Result<()> fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where where
T: ?Sized + Serialize T: ?Sized + Serialize
{ {
value.serialize(&mut **self) value.serialize(&mut self.value_serializer)?;
self.sorted_map.insert(self.last_inserted_key.clone(), self.value_serializer.output.clone());
self.value_serializer.output.clear();
self.last_inserted_key.clear();
Ok(())
} }
fn end(self) -> Result<()> { fn end(self) -> Result<()> {
// TODO serialize only here and sort first for (key, value) in self.sorted_map.into_iter() {
self.output.push('e' as u8); self.super_serializer.output.extend(key);
self.super_serializer.output.extend(value);
}
self.super_serializer.output.push('e' as u8);
Ok(()) Ok(())
} }
} }
@ -300,7 +327,7 @@ impl <'a> ser::SerializeStructVariant for &'a mut Serializer {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
where where
T: ?Sized + Serialize T: ?Sized + Serialize
{ {