2019-10-03 11:49:13 +02:00
|
|
|
macro_rules! forward_to_unserializable_type {
|
|
|
|
($($ty:ident => $se_method:ident,)*) => {
|
|
|
|
$(
|
|
|
|
fn $se_method(self, _v: $ty) -> Result<Self::Ok, Self::Error> {
|
|
|
|
Err(SerializerError::UnserializableType { type_name: "$ty" })
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod convert_to_number;
|
|
|
|
mod convert_to_string;
|
|
|
|
mod deserializer;
|
|
|
|
mod extract_document_id;
|
|
|
|
mod indexer;
|
|
|
|
mod serializer;
|
|
|
|
|
|
|
|
pub use self::deserializer::{Deserializer, DeserializerError};
|
|
|
|
pub use self::extract_document_id::{extract_document_id, compute_document_id, value_to_string};
|
|
|
|
pub use self::convert_to_string::ConvertToString;
|
|
|
|
pub use self::convert_to_number::ConvertToNumber;
|
|
|
|
pub use self::indexer::Indexer;
|
|
|
|
pub use self::serializer::Serializer;
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::{fmt, error::Error};
|
|
|
|
|
|
|
|
use meilidb_schema::SchemaAttr;
|
|
|
|
use serde_json::Error as SerdeJsonError;
|
|
|
|
use serde::ser;
|
|
|
|
|
|
|
|
use crate::{DocumentId, ParseNumberError};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SerializerError {
|
|
|
|
DocumentIdNotFound,
|
|
|
|
InvalidDocumentIdType,
|
|
|
|
RkvError(rkv::StoreError),
|
2019-10-11 16:16:21 +02:00
|
|
|
SerdeJson(SerdeJsonError),
|
|
|
|
ParseNumber(ParseNumberError),
|
2019-10-03 11:49:13 +02:00
|
|
|
UnserializableType { type_name: &'static str },
|
|
|
|
UnindexableType { type_name: &'static str },
|
|
|
|
UnrankableType { type_name: &'static str },
|
|
|
|
Custom(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ser::Error for SerializerError {
|
|
|
|
fn custom<T: fmt::Display>(msg: T) -> Self {
|
|
|
|
SerializerError::Custom(msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for SerializerError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
SerializerError::DocumentIdNotFound => {
|
2019-10-11 16:16:21 +02:00
|
|
|
f.write_str("serialized document does not have an id according to the schema")
|
2019-10-03 11:49:13 +02:00
|
|
|
},
|
|
|
|
SerializerError::InvalidDocumentIdType => {
|
2019-10-11 16:16:21 +02:00
|
|
|
f.write_str("document identifier can only be of type string or number")
|
2019-10-03 11:49:13 +02:00
|
|
|
},
|
|
|
|
SerializerError::RkvError(e) => write!(f, "rkv related error: {}", e),
|
2019-10-11 16:16:21 +02:00
|
|
|
SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e),
|
|
|
|
SerializerError::ParseNumber(e) => {
|
2019-10-03 11:49:13 +02:00
|
|
|
write!(f, "error while trying to parse a number: {}", e)
|
|
|
|
},
|
|
|
|
SerializerError::UnserializableType { type_name } => {
|
2019-10-03 15:04:11 +02:00
|
|
|
write!(f, "{} is not a serializable type", type_name)
|
2019-10-03 11:49:13 +02:00
|
|
|
},
|
|
|
|
SerializerError::UnindexableType { type_name } => {
|
2019-10-03 15:04:11 +02:00
|
|
|
write!(f, "{} is not an indexable type", type_name)
|
2019-10-03 11:49:13 +02:00
|
|
|
},
|
|
|
|
SerializerError::UnrankableType { type_name } => {
|
|
|
|
write!(f, "{} types can not be used for ranking", type_name)
|
|
|
|
},
|
|
|
|
SerializerError::Custom(s) => f.write_str(s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for SerializerError {}
|
|
|
|
|
|
|
|
impl From<String> for SerializerError {
|
|
|
|
fn from(value: String) -> SerializerError {
|
|
|
|
SerializerError::Custom(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SerdeJsonError> for SerializerError {
|
|
|
|
fn from(error: SerdeJsonError) -> SerializerError {
|
2019-10-11 16:16:21 +02:00
|
|
|
SerializerError::SerdeJson(error)
|
2019-10-03 11:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<rkv::StoreError> for SerializerError {
|
|
|
|
fn from(error: rkv::StoreError) -> SerializerError {
|
|
|
|
SerializerError::RkvError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseNumberError> for SerializerError {
|
|
|
|
fn from(error: ParseNumberError) -> SerializerError {
|
2019-10-11 16:16:21 +02:00
|
|
|
SerializerError::ParseNumber(error)
|
2019-10-03 11:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RamDocumentStore(BTreeMap<(DocumentId, SchemaAttr), Vec<u8>>);
|
|
|
|
|
|
|
|
impl RamDocumentStore {
|
|
|
|
pub fn new() -> RamDocumentStore {
|
|
|
|
RamDocumentStore(BTreeMap::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_document_field(&mut self, id: DocumentId, attr: SchemaAttr, value: Vec<u8>) {
|
|
|
|
self.0.insert((id, attr), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_inner(self) -> BTreeMap<(DocumentId, SchemaAttr), Vec<u8>> {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|