use core::{fmt::Display, num::TryFromIntError, str::Utf8Error};
use rkyv::rancor;
use std::io;
use thiserror::Error;
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("{0}")]
pub struct DatabaseError(String);
impl DatabaseError {
pub fn new(error: impl Display) -> Self {
Self(error.to_string())
}
}
impl From<fjall::Error> for DatabaseError {
fn from(error: fjall::Error) -> Self {
Self::new(error)
}
}
impl From<io::Error> for DatabaseError {
fn from(error: io::Error) -> Self {
Self::new(error)
}
}
impl From<rancor::Error> for DatabaseError {
fn from(error: rancor::Error) -> Self {
Self::new(error)
}
}
impl From<redb::Error> for DatabaseError {
fn from(error: redb::Error) -> Self {
Self::new(error)
}
}
impl From<TryFromIntError> for DatabaseError {
fn from(error: TryFromIntError) -> Self {
Self::new(error)
}
}
impl From<Utf8Error> for DatabaseError {
fn from(error: Utf8Error) -> Self {
Self::new(error)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display() {
assert_eq!(
DatabaseError::new("database not initialized").to_string(),
"database not initialized"
);
}
}