2019-04-19 15:50:53 +02:00
|
|
|
use meilidb_core::DocumentId;
|
2019-04-19 16:19:50 +02:00
|
|
|
use serde::ser;
|
2019-04-19 15:50:53 +02:00
|
|
|
|
|
|
|
use crate::database::RawIndex;
|
2019-04-19 18:03:32 +02:00
|
|
|
use crate::indexer::Indexer as RawIndexer;
|
|
|
|
use crate::schema::{Schema, SchemaAttr};
|
|
|
|
use super::{SerializerError, ConvertToString, Indexer};
|
2019-04-19 15:50:53 +02:00
|
|
|
|
|
|
|
pub struct Serializer<'a> {
|
|
|
|
pub schema: &'a Schema,
|
|
|
|
pub index: &'a RawIndex,
|
2019-04-19 18:03:32 +02:00
|
|
|
pub indexer: &'a mut RawIndexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
pub document_id: DocumentId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::Serializer for Serializer<'a> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = SerializerError;
|
|
|
|
type SerializeSeq = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTuple = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTupleStruct = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
type SerializeMap = MapSerializer<'a>;
|
|
|
|
type SerializeStruct = StructSerializer<'a>;
|
|
|
|
type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
|
|
|
|
|
|
|
|
forward_to_unserializable_type! {
|
|
|
|
bool => serialize_bool,
|
|
|
|
char => serialize_char,
|
|
|
|
|
|
|
|
i8 => serialize_i8,
|
|
|
|
i16 => serialize_i16,
|
|
|
|
i32 => serialize_i32,
|
|
|
|
i64 => serialize_i64,
|
|
|
|
|
|
|
|
u8 => serialize_u8,
|
|
|
|
u16 => serialize_u16,
|
|
|
|
u32 => serialize_u32,
|
|
|
|
u64 => serialize_u64,
|
|
|
|
|
|
|
|
f32 => serialize_f32,
|
|
|
|
f64 => serialize_f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "str" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "&[u8]" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "Option" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "Option" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "()" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "unit struct" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str
|
|
|
|
) -> Result<Self::Ok, Self::Error>
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "unit variant" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_struct<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
value: &T
|
|
|
|
) -> Result<Self::Ok, Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_value: &T
|
|
|
|
) -> Result<Self::Ok, Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "newtype variant" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "sequence" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "tuple" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize
|
|
|
|
) -> Result<Self::SerializeTupleStruct, Self::Error>
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "tuple struct" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize
|
|
|
|
) -> Result<Self::SerializeTupleVariant, Self::Error>
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "tuple variant" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
|
|
|
Ok(MapSerializer {
|
|
|
|
schema: self.schema,
|
|
|
|
document_id: self.document_id,
|
|
|
|
index: self.index,
|
2019-04-19 18:03:32 +02:00
|
|
|
indexer: self.indexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
current_key_name: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize
|
|
|
|
) -> Result<Self::SerializeStruct, Self::Error>
|
|
|
|
{
|
|
|
|
Ok(StructSerializer {
|
|
|
|
schema: self.schema,
|
|
|
|
document_id: self.document_id,
|
|
|
|
index: self.index,
|
2019-04-19 18:03:32 +02:00
|
|
|
indexer: self.indexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
_variant: &'static str,
|
|
|
|
_len: usize
|
|
|
|
) -> Result<Self::SerializeStructVariant, Self::Error>
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "struct variant" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MapSerializer<'a> {
|
2019-04-21 21:58:54 +02:00
|
|
|
schema: &'a Schema,
|
|
|
|
document_id: DocumentId,
|
|
|
|
index: &'a RawIndex,
|
|
|
|
indexer: &'a mut RawIndexer,
|
|
|
|
current_key_name: Option<String>,
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::SerializeMap for MapSerializer<'a> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = SerializerError;
|
|
|
|
|
|
|
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
let key = key.serialize(ConvertToString)?;
|
2019-04-19 15:50:53 +02:00
|
|
|
self.current_key_name = Some(key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
|
|
|
let key = self.current_key_name.take().unwrap();
|
|
|
|
self.serialize_entry(&key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_entry<K: ?Sized, V: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &K,
|
|
|
|
value: &V,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where K: ser::Serialize, V: ser::Serialize,
|
|
|
|
{
|
2019-04-19 18:03:32 +02:00
|
|
|
let key = key.serialize(ConvertToString)?;
|
2019-04-19 15:50:53 +02:00
|
|
|
|
|
|
|
serialize_value(
|
|
|
|
self.schema,
|
|
|
|
self.document_id,
|
|
|
|
self.index,
|
2019-04-19 18:03:32 +02:00
|
|
|
self.indexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
&key,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructSerializer<'a> {
|
2019-04-21 21:58:54 +02:00
|
|
|
schema: &'a Schema,
|
|
|
|
document_id: DocumentId,
|
|
|
|
index: &'a RawIndex,
|
|
|
|
indexer: &'a mut RawIndexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ser::SerializeStruct for StructSerializer<'a> {
|
|
|
|
type Ok = ();
|
|
|
|
type Error = SerializerError;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
|
|
|
serialize_value(
|
|
|
|
self.schema,
|
|
|
|
self.document_id,
|
|
|
|
self.index,
|
2019-04-19 18:03:32 +02:00
|
|
|
self.indexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
key,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized>(
|
|
|
|
schema: &Schema,
|
|
|
|
document_id: DocumentId,
|
|
|
|
index: &RawIndex,
|
2019-04-19 18:03:32 +02:00
|
|
|
indexer: &mut RawIndexer,
|
2019-04-19 15:50:53 +02:00
|
|
|
key: &str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), SerializerError>
|
|
|
|
where T: ser::Serialize,
|
|
|
|
{
|
|
|
|
if let Some(attr) = schema.attribute(key) {
|
|
|
|
let props = schema.props(attr);
|
|
|
|
|
|
|
|
if props.is_stored() {
|
|
|
|
let value = rmp_serde::to_vec_named(value)?;
|
|
|
|
index.set_document_attribute(document_id, attr, value)?;
|
|
|
|
}
|
2019-04-19 18:03:32 +02:00
|
|
|
|
|
|
|
if props.is_indexed() {
|
|
|
|
let indexer = Indexer {
|
|
|
|
attribute: attr,
|
|
|
|
indexer: indexer,
|
|
|
|
document_id: document_id,
|
|
|
|
};
|
|
|
|
value.serialize(indexer)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if props.is_ranked() {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|