diff --git a/src/bencode/ser.rs b/src/bencode/ser.rs index fc4a8cc..b730e7c 100644 --- a/src/bencode/ser.rs +++ b/src/bencode/ser.rs @@ -8,13 +8,19 @@ pub struct Serializer { output: Vec, } +impl Serializer { + fn new() -> Self { + Serializer { + output: Vec::new(), + } + } +} + pub fn to_bytes(value: &T) -> Result> 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>, + last_inserted_key: Vec, + 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) -> Result { 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 { @@ -226,7 +242,7 @@ impl <'a> ser::SerializeTupleStruct for &'a mut Serializer { type Ok = (); type Error = Error; - fn serialize_field(&mut self, value: &T) -> Result<()> + fn serialize_field(&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(&mut self, value: &T) -> Result<()> + fn serialize_field(&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(&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(&mut self, key: &'static str, value: &T) -> Result<()> + fn serialize_field(&mut self, _key: &'static str, _value: &T) -> Result<()> where T: ?Sized + Serialize {