diff --git a/meilidb-data/src/serde/indexer.rs b/meilidb-data/src/serde/indexer.rs index b8eb6c599..c6e0d0c75 100644 --- a/meilidb-data/src/serde/indexer.rs +++ b/meilidb-data/src/serde/indexer.rs @@ -17,11 +17,11 @@ impl<'a> ser::Serializer for Indexer<'a> { type Ok = (); type Error = SerializerError; type SerializeSeq = SeqIndexer<'a>; - type SerializeTuple = ser::Impossible; + type SerializeTuple = TupleIndexer<'a>; type SerializeTupleStruct = ser::Impossible; type SerializeTupleVariant = ser::Impossible; - type SerializeMap = ser::Impossible; - type SerializeStruct = ser::Impossible; + type SerializeMap = MapIndexer<'a>; + type SerializeStruct = StructSerializer<'a>; type SerializeStructVariant = ser::Impossible; fn serialize_bool(self, value: bool) -> Result { @@ -156,7 +156,14 @@ impl<'a> ser::Serializer for Indexer<'a> { } fn serialize_tuple(self, _len: usize) -> Result { - Err(SerializerError::UnindexableType { type_name: "tuple" }) + let indexer = TupleIndexer { + attribute: self.attribute, + document_id: self.document_id, + indexer: self.indexer, + texts: Vec::new(), + }; + + Ok(indexer) } fn serialize_tuple_struct( @@ -180,7 +187,14 @@ impl<'a> ser::Serializer for Indexer<'a> { } fn serialize_map(self, _len: Option) -> Result { - Err(SerializerError::UnindexableType { type_name: "map" }) + let indexer = MapIndexer { + attribute: self.attribute, + document_id: self.document_id, + indexer: self.indexer, + texts: Vec::new(), + }; + + Ok(indexer) } fn serialize_struct( @@ -220,14 +234,104 @@ impl<'a> ser::SerializeSeq for SeqIndexer<'a> { { let text = value.serialize(ConvertToString)?; self.texts.push(text); - Ok(()) } fn end(mut self) -> Result { let texts = self.texts.iter().map(String::as_str); self.indexer.index_text_seq(self.document_id, self.attribute, texts); - + Ok(()) + } +} + +pub struct MapIndexer<'a> { + attribute: SchemaAttr, + document_id: DocumentId, + indexer: &'a mut RawIndexer, + texts: Vec, +} + +impl<'a> ser::SerializeMap for MapIndexer<'a> { + type Ok = (); + type Error = SerializerError; + + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where T: ser::Serialize, + { + let text = key.serialize(ConvertToString)?; + self.texts.push(text); + Ok(()) + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where T: ser::Serialize, + { + let text = value.serialize(ConvertToString)?; + self.texts.push(text); + Ok(()) + } + + fn end(self) -> Result { + let texts = self.texts.iter().map(String::as_str); + self.indexer.index_text_seq(self.document_id, self.attribute, texts); + Ok(()) + } +} + +pub struct StructSerializer<'a> { + attribute: SchemaAttr, + document_id: DocumentId, + indexer: &'a mut RawIndexer, + texts: Vec, +} + +impl<'a> ser::SerializeStruct for StructSerializer<'a> { + type Ok = (); + type Error = SerializerError; + + fn serialize_field( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> + where T: ser::Serialize, + { + let key_text = key.to_owned(); + let value_text = value.serialize(ConvertToString)?; + self.texts.push(key_text); + self.texts.push(value_text); + Ok(()) + } + + fn end(self) -> Result { + let texts = self.texts.iter().map(String::as_str); + self.indexer.index_text_seq(self.document_id, self.attribute, texts); + Ok(()) + } +} + +pub struct TupleIndexer<'a> { + attribute: SchemaAttr, + document_id: DocumentId, + indexer: &'a mut RawIndexer, + texts: Vec, +} + +impl<'a> ser::SerializeTuple for TupleIndexer<'a> { + type Ok = (); + type Error = SerializerError; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where T: Serialize + { + let text = value.serialize(ConvertToString)?; + self.texts.push(text); + Ok(()) + } + + fn end(self) -> Result { + let texts = self.texts.iter().map(String::as_str); + self.indexer.index_text_seq(self.document_id, self.attribute, texts); Ok(()) } } diff --git a/meilidb-data/src/serde/serializer.rs b/meilidb-data/src/serde/serializer.rs index 34adaa326..d71c87f14 100644 --- a/meilidb-data/src/serde/serializer.rs +++ b/meilidb-data/src/serde/serializer.rs @@ -165,11 +165,11 @@ impl<'a> ser::Serializer for Serializer<'a> { } pub struct MapSerializer<'a> { - pub schema: &'a Schema, - pub document_id: DocumentId, - pub index: &'a RawIndex, - pub indexer: &'a mut RawIndexer, - pub current_key_name: Option, + schema: &'a Schema, + document_id: DocumentId, + index: &'a RawIndex, + indexer: &'a mut RawIndexer, + current_key_name: Option, } impl<'a> ser::SerializeMap for MapSerializer<'a> { @@ -216,10 +216,10 @@ impl<'a> ser::SerializeMap for MapSerializer<'a> { } pub struct StructSerializer<'a> { - pub schema: &'a Schema, - pub document_id: DocumentId, - pub index: &'a RawIndex, - pub indexer: &'a mut RawIndexer, + schema: &'a Schema, + document_id: DocumentId, + index: &'a RawIndex, + indexer: &'a mut RawIndexer, } impl<'a> ser::SerializeStruct for StructSerializer<'a> {