MeiliSearch/meilisearch-core/src/serde/mod.rs

93 lines
2.9 KiB
Rust
Raw Normal View History

2019-10-03 11:49:13 +02:00
mod deserializer;
2019-10-18 13:05:28 +02:00
pub use self::deserializer::{Deserializer, DeserializerError};
2019-10-03 11:49:13 +02:00
2019-10-18 13:05:28 +02:00
use std::{error::Error, fmt};
2019-10-03 11:49:13 +02:00
use serde::ser;
2019-10-18 13:05:28 +02:00
use serde_json::Error as SerdeJsonError;
2020-01-10 18:20:30 +01:00
use meilisearch_schema::Error as SchemaError;
2019-10-03 11:49:13 +02:00
use crate::ParseNumberError;
2019-10-03 11:49:13 +02:00
#[derive(Debug)]
pub enum SerializerError {
DocumentIdNotFound,
InvalidDocumentIdFormat,
2019-10-21 12:05:53 +02:00
Zlmdb(heed::Error),
2019-10-11 16:16:21 +02:00
SerdeJson(SerdeJsonError),
ParseNumber(ParseNumberError),
2020-01-10 18:20:30 +01:00
Schema(SchemaError),
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-18 13:05:28 +02:00
}
SerializerError::InvalidDocumentIdFormat => {
2020-03-10 15:35:19 +01:00
f.write_str("a document primary key can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).")
2019-10-18 13:05:28 +02:00
}
2019-10-21 12:05:53 +02:00
SerializerError::Zlmdb(e) => write!(f, "heed 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)
2019-10-18 13:05:28 +02:00
}
2020-01-10 18:20:30 +01:00
SerializerError::Schema(e) => write!(f, "impossible to update schema: {}", e),
2019-10-03 11:49:13 +02:00
SerializerError::UnserializableType { type_name } => {
2019-10-03 15:04:11 +02:00
write!(f, "{} is not a serializable type", type_name)
2019-10-18 13:05:28 +02:00
}
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-18 13:05:28 +02:00
}
2019-10-03 11:49:13 +02:00
SerializerError::UnrankableType { type_name } => {
write!(f, "{} types can not be used for ranking", type_name)
2019-10-18 13:05:28 +02:00
}
2019-10-03 11:49:13 +02:00
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
}
}
2019-10-21 12:05:53 +02:00
impl From<heed::Error> for SerializerError {
fn from(error: heed::Error) -> SerializerError {
2019-10-16 17:05:24 +02:00
SerializerError::Zlmdb(error)
2019-10-03 11:49:13 +02:00
}
}
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
}
}
2020-01-10 18:20:30 +01:00
impl From<SchemaError> for SerializerError {
fn from(error: SchemaError) -> SerializerError {
SerializerError::Schema(error)
}
}