From 899567042fbbebe8c78ecd1c3c91816d347ae346 Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Wed, 24 Jun 2020 18:07:34 +0300 Subject: [PATCH] Add support for map and seq --- src/serde/ser.rs | 56 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 093a5d5..15a1fc7 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -284,8 +284,14 @@ impl<'a> ser::Serializer for &'a mut Serializer { todo!("`newtype_variant` is not supported by WIT for the moment.") } - fn serialize_seq(self, _len: Option) -> Result { - todo!("`seq` is not supported by WIT for the moment.") + fn serialize_seq(self, len: Option) -> Result { + if let Some(len) = len { + self.push_with_capacity(len); + + Ok(self) + } else { + todo!("`seq` without known size are not supported for the moment") + } } fn serialize_tuple(self, _len: usize) -> Result { @@ -312,8 +318,14 @@ impl<'a> ser::Serializer for &'a mut Serializer { todo!("`tuple_variant` is not supported by WIT for the moment.") } - fn serialize_map(self, _len: Option) -> Result { - todo!("`map` is not supported by WIT for the moment.") + fn serialize_map(self, len: Option) -> Result { + if let Some(len) = len { + self.push_with_capacity(len); + + Ok(self) + } else { + todo!("`map` without known size are not supported for the moment") + } } fn serialize_struct( @@ -341,15 +353,20 @@ impl<'a> ser::SerializeSeq for &'a mut Serializer { type Ok = (); type Error = SerializeError; - fn serialize_element(&mut self, _value: &T) -> Result + fn serialize_element(&mut self, value: &T) -> Result where T: ?Sized + Serialize, { - unimplemented!() + value.serialize(&mut **self) } fn end(self) -> Result { - unimplemented!() + let record = InterfaceValue::Record( + Vec1::new(self.pop()?).map_err(|_| Self::Error::RecordNeedsAtLeastOneField)?, + ); + self.last().push(record); + + Ok(()) } } @@ -406,26 +423,27 @@ impl<'a> ser::SerializeTupleVariant for &'a mut Serializer { } } -impl<'a> ser::SerializeMap for &'a mut Serializer { +impl <'a> ser::SerializeMap for &'a mut Serializer { type Ok = (); type Error = SerializeError; - fn serialize_key(&mut self, _key: &T) -> Result - where - T: ?Sized + Serialize, - { - unimplemented!() + fn serialize_key(&mut self, _key: &T) -> Result<(), Self::Error> where + T: Serialize { + Ok(()) } - fn serialize_value(&mut self, _value: &T) -> Result - where - T: ?Sized + Serialize, - { - unimplemented!() + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> where + T: Serialize { + value.serialize(&mut **self) } fn end(self) -> Result { - unimplemented!() + let record = InterfaceValue::Record( + Vec1::new(self.pop()?).map_err(|_| Self::Error::RecordNeedsAtLeastOneField)?, + ); + self.last().push(record); + + Ok(()) } }