[bencode/ser] Implemented sorting of dictionaries
This commit is contained in:
parent
e7553e569d
commit
0264c133ac
@ -8,13 +8,19 @@ pub struct Serializer {
|
||||
output: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Serializer {
|
||||
fn new() -> Self {
|
||||
Serializer {
|
||||
output: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
||||
where
|
||||
T: Serialize + ?Sized
|
||||
{
|
||||
let mut serializer = Serializer{
|
||||
output: Vec::new(),
|
||||
};
|
||||
let mut serializer = Serializer::new();
|
||||
value.serialize(&mut serializer)?;
|
||||
Ok(serializer.output)
|
||||
}
|
||||
@ -27,7 +33,12 @@ where
|
||||
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
|
||||
{
|
||||
@ -39,7 +50,7 @@ impl<'a> ser::Serializer for &'a mut Serializer
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Self;
|
||||
type SerializeMap = Self;
|
||||
type SerializeMap = MapSerializer<'a>;
|
||||
type SerializeStruct = 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> {
|
||||
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> {
|
||||
@ -226,7 +242,7 @@ impl <'a> ser::SerializeTupleStruct for &'a mut Serializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
|
||||
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize
|
||||
{
|
||||
@ -242,7 +258,7 @@ impl <'a> ser::SerializeTupleVariant for &'a mut Serializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
|
||||
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
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 Error = Error;
|
||||
|
||||
@ -262,19 +278,30 @@ impl <'a> ser::SerializeMap for &'a mut Serializer {
|
||||
where
|
||||
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<()>
|
||||
where
|
||||
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<()> {
|
||||
// TODO serialize only here and sort first
|
||||
self.output.push('e' as u8);
|
||||
for (key, value) in self.sorted_map.into_iter() {
|
||||
self.super_serializer.output.extend(key);
|
||||
self.super_serializer.output.extend(value);
|
||||
}
|
||||
self.super_serializer.output.push('e' as u8);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -300,7 +327,7 @@ impl <'a> ser::SerializeStructVariant for &'a mut Serializer {
|
||||
type Ok = ();
|
||||
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
|
||||
T: ?Sized + Serialize
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user