use std::{error::Error as StdError, fmt, result};
use serde::{de, ser};
#[derive(Debug)]
pub enum Error {
Unsupported(String),
ValueTooLarge(String),
Serialization(String),
Deserialization { column: Option<String>, message: String },
Rusqlite(rusqlite::Error),
ColumnNamesNotAvailable,
}
pub type Result<T> = result::Result<T, Error>;
impl Error {
pub fn ser_unsupported(typ: &str) -> Self {
Error::Unsupported(format!("Serialization is not supported from type: {}", typ))
}
pub fn de_unsupported(typ: &str) -> Self {
Error::Unsupported(format!("Deserialization is not supported into type: {}", typ))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
match self {
Error::Unsupported(s) | Error::ValueTooLarge(s) => write!(f, "{}", s),
Error::Serialization(s) => write!(f, "Serialization error: {}", s),
Error::Deserialization { column: None, message } => write!(f, "Deserialization error: {}", message),
Error::Deserialization {
column: Some(column),
message,
} => write!(f, "Deserialization failed for column: {} error: {}", column, message),
Error::Rusqlite(s) => write!(f, "Rusqlite error: {}", s),
Error::ColumnNamesNotAvailable => write!(f, "Column names are not available"),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::Rusqlite(e) => Some(e),
_ => None,
}
}
}
impl de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Deserialization {
column: None,
message: msg.to_string(),
}
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Serialization(msg.to_string())
}
}
impl From<rusqlite::Error> for Error {
fn from(e: rusqlite::Error) -> Self {
Error::Rusqlite(e)
}
}