From 858bf359b8357bfe32f9f6b8b0713165db3d1799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 22 Oct 2019 16:02:02 +0200 Subject: [PATCH] Make possible to convert recursive object into strings --- meilidb-core/src/serde/convert_to_string.rs | 72 +++++++++++++++++++-- meilidb-core/src/serde/indexer.rs | 6 +- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/meilidb-core/src/serde/convert_to_string.rs b/meilidb-core/src/serde/convert_to_string.rs index 2f1fe0ca1..718bd1bf2 100644 --- a/meilidb-core/src/serde/convert_to_string.rs +++ b/meilidb-core/src/serde/convert_to_string.rs @@ -12,8 +12,8 @@ impl ser::Serializer for ConvertToString { type SerializeTuple = ser::Impossible; type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; - type SerializeMap = ser::Impossible; - type SerializeStruct = ser::Impossible; + type SerializeMap = MapConvertToString; + type SerializeStruct = StructConvertToString; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, _value: bool) -> Result { @@ -169,7 +169,9 @@ impl ser::Serializer for ConvertToString { } fn serialize_map(self, _len: Option) -> Result { - Err(SerializerError::UnserializableType { type_name: "map" }) + Ok(MapConvertToString { + text: String::new(), + }) } fn serialize_struct( @@ -177,8 +179,8 @@ impl ser::Serializer for ConvertToString { _name: &'static str, _len: usize, ) -> Result { - Err(SerializerError::UnserializableType { - type_name: "struct", + Ok(StructConvertToString { + text: String::new(), }) } @@ -194,3 +196,63 @@ impl ser::Serializer for ConvertToString { }) } } + +pub struct MapConvertToString { + text: String, +} + +impl ser::SerializeMap for MapConvertToString { + type Ok = String; + type Error = SerializerError; + + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let text = key.serialize(ConvertToString)?; + self.text.push_str(&text); + self.text.push_str(" "); + Ok(()) + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let text = value.serialize(ConvertToString)?; + self.text.push_str(&text); + Ok(()) + } + + fn end(self) -> Result { + Ok(self.text) + } +} + +pub struct StructConvertToString { + text: String, +} + +impl ser::SerializeStruct for StructConvertToString { + type Ok = String; + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where + T: ser::Serialize, + { + let value = value.serialize(ConvertToString)?; + self.text.push_str(key); + self.text.push_str(" "); + self.text.push_str(&value); + Ok(()) + } + + fn end(self) -> Result { + Ok(self.text) + } +} diff --git a/meilidb-core/src/serde/indexer.rs b/meilidb-core/src/serde/indexer.rs index 04d24a63a..49c91252d 100644 --- a/meilidb-core/src/serde/indexer.rs +++ b/meilidb-core/src/serde/indexer.rs @@ -20,7 +20,7 @@ impl<'a> ser::Serializer for Indexer<'a> { type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; type SerializeMap = MapIndexer<'a>; - type SerializeStruct = StructSerializer<'a>; + type SerializeStruct = StructIndexer<'a>; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, _value: bool) -> Result { @@ -302,14 +302,14 @@ impl<'a> ser::SerializeMap for MapIndexer<'a> { } } -pub struct StructSerializer<'a> { +pub struct StructIndexer<'a> { attribute: SchemaAttr, document_id: DocumentId, indexer: &'a mut RawIndexer, texts: Vec, } -impl<'a> ser::SerializeStruct for StructSerializer<'a> { +impl<'a> ser::SerializeStruct for StructIndexer<'a> { type Ok = Option; type Error = SerializerError;