use std::error::Error;
use std::fmt::{Display, Formatter, Result as FormatResult};
pub type ThingdResult<T> = Result<T, ThingdError>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ThingdError {
InvalidInput(String),
NotFound(String),
Conflict(String),
Protected(String),
Storage(String),
EncryptionRequired(String),
InvalidEncryptionKey(String),
EncryptionAuthentication(String),
UnsupportedEncryptionVersion(String),
EncryptionMigration(String),
}
impl Display for ThingdError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult {
match self {
Self::InvalidInput(message) => write!(formatter, "invalid input: {message}"),
Self::NotFound(message) => write!(formatter, "not found: {message}"),
Self::Conflict(message) => write!(formatter, "conflict: {message}"),
Self::Protected(message) => write!(formatter, "protected: {message}"),
Self::Storage(message) => write!(formatter, "storage error: {message}"),
Self::EncryptionRequired(message) => {
write!(formatter, "encryption required: {message}")
},
Self::InvalidEncryptionKey(message) => {
write!(formatter, "invalid encryption key: {message}")
},
Self::EncryptionAuthentication(message) => {
write!(formatter, "encryption authentication failed: {message}")
},
Self::UnsupportedEncryptionVersion(message) => {
write!(formatter, "unsupported encryption version: {message}")
},
Self::EncryptionMigration(message) => {
write!(formatter, "encryption migration failed: {message}")
},
}
}
}
impl Error for ThingdError {}
#[cfg(feature = "persistent")]
impl From<fjall::Error> for ThingdError {
fn from(error: fjall::Error) -> Self {
Self::Storage(error.to_string())
}
}