2019-04-19 15:50:53 +02:00
|
|
|
macro_rules! forward_to_unserializable_type {
|
|
|
|
($($ty:ident => $se_method:ident,)*) => {
|
|
|
|
$(
|
|
|
|
fn $se_method(self, _v: $ty) -> Result<Self::Ok, Self::Error> {
|
2019-04-19 18:03:32 +02:00
|
|
|
Err(SerializerError::UnserializableType { type_name: "$ty" })
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:18:39 +02:00
|
|
|
mod convert_to_number;
|
|
|
|
mod convert_to_string;
|
2019-04-18 14:23:09 +02:00
|
|
|
mod deserializer;
|
2019-04-19 16:19:50 +02:00
|
|
|
mod extract_document_id;
|
2019-04-19 18:03:32 +02:00
|
|
|
mod indexer;
|
|
|
|
mod serializer;
|
2019-04-18 14:23:09 +02:00
|
|
|
|
|
|
|
pub use self::deserializer::Deserializer;
|
2019-04-19 16:19:50 +02:00
|
|
|
pub use self::extract_document_id::extract_document_id;
|
2019-04-19 18:03:32 +02:00
|
|
|
pub use self::convert_to_string::ConvertToString;
|
2019-04-26 12:18:39 +02:00
|
|
|
pub use self::convert_to_number::ConvertToNumber;
|
2019-04-19 18:03:32 +02:00
|
|
|
pub use self::indexer::Indexer;
|
|
|
|
pub use self::serializer::Serializer;
|
2019-04-19 15:50:53 +02:00
|
|
|
|
|
|
|
use std::{fmt, error::Error};
|
|
|
|
use rmp_serde::encode::Error as RmpError;
|
|
|
|
use serde::ser;
|
2019-04-26 12:18:39 +02:00
|
|
|
use crate::number::ParseNumberError;
|
2019-04-19 15:50:53 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SerializerError {
|
|
|
|
DocumentIdNotFound,
|
|
|
|
RmpError(RmpError),
|
|
|
|
SledError(sled::Error),
|
2019-04-26 12:18:39 +02:00
|
|
|
ParseNumberError(ParseNumberError),
|
2019-04-19 18:03:32 +02:00
|
|
|
UnserializableType { type_name: &'static str },
|
|
|
|
UnindexableType { type_name: &'static str },
|
2019-04-26 14:59:35 +02:00
|
|
|
UnrankableType { type_name: &'static str },
|
2019-04-19 15:50:53 +02:00
|
|
|
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 => {
|
|
|
|
write!(f, "serialized document does not have an id according to the schema")
|
|
|
|
}
|
|
|
|
SerializerError::RmpError(e) => write!(f, "rmp serde related error: {}", e),
|
|
|
|
SerializerError::SledError(e) => write!(f, "sled related error: {}", e),
|
2019-04-26 12:18:39 +02:00
|
|
|
SerializerError::ParseNumberError(e) => {
|
|
|
|
write!(f, "error while trying to parse a number: {}", e)
|
|
|
|
},
|
2019-04-19 18:03:32 +02:00
|
|
|
SerializerError::UnserializableType { type_name } => {
|
|
|
|
write!(f, "{} are not a serializable type", type_name)
|
|
|
|
},
|
|
|
|
SerializerError::UnindexableType { type_name } => {
|
|
|
|
write!(f, "{} are not an indexable type", type_name)
|
2019-04-19 15:50:53 +02:00
|
|
|
},
|
2019-04-26 14:59:35 +02:00
|
|
|
SerializerError::UnrankableType { type_name } => {
|
|
|
|
write!(f, "{} types can not be used for ranking", type_name)
|
|
|
|
},
|
2019-04-19 18:03:32 +02:00
|
|
|
SerializerError::Custom(s) => f.write_str(s),
|
2019-04-19 15:50:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for SerializerError {}
|
|
|
|
|
|
|
|
impl From<String> for SerializerError {
|
|
|
|
fn from(value: String) -> SerializerError {
|
|
|
|
SerializerError::Custom(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RmpError> for SerializerError {
|
|
|
|
fn from(error: RmpError) -> SerializerError {
|
|
|
|
SerializerError::RmpError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sled::Error> for SerializerError {
|
|
|
|
fn from(error: sled::Error) -> SerializerError {
|
|
|
|
SerializerError::SledError(error)
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 12:18:39 +02:00
|
|
|
|
|
|
|
impl From<ParseNumberError> for SerializerError {
|
|
|
|
fn from(error: ParseNumberError) -> SerializerError {
|
|
|
|
SerializerError::ParseNumberError(error)
|
|
|
|
}
|
|
|
|
}
|