Skip to main content

turtle_build/infrastructure/database/
error.rs

1use core::{fmt::Display, num::TryFromIntError, str::Utf8Error};
2use rkyv::rancor;
3use std::io;
4use thiserror::Error;
5
6/// A database error.
7#[derive(Clone, Debug, Eq, Error, PartialEq)]
8#[error("{0}")]
9pub struct DatabaseError(String);
10
11impl DatabaseError {
12    /// Creates an error.
13    pub fn new(error: impl Display) -> Self {
14        Self(error.to_string())
15    }
16}
17
18impl From<fjall::Error> for DatabaseError {
19    fn from(error: fjall::Error) -> Self {
20        Self::new(error)
21    }
22}
23
24impl From<io::Error> for DatabaseError {
25    fn from(error: io::Error) -> Self {
26        Self::new(error)
27    }
28}
29
30impl From<rancor::Error> for DatabaseError {
31    fn from(error: rancor::Error) -> Self {
32        Self::new(error)
33    }
34}
35
36impl From<redb::Error> for DatabaseError {
37    fn from(error: redb::Error) -> Self {
38        Self::new(error)
39    }
40}
41
42impl From<TryFromIntError> for DatabaseError {
43    fn from(error: TryFromIntError) -> Self {
44        Self::new(error)
45    }
46}
47
48impl From<Utf8Error> for DatabaseError {
49    fn from(error: Utf8Error) -> Self {
50        Self::new(error)
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn display() {
60        assert_eq!(
61            DatabaseError::new("database not initialized").to_string(),
62            "database not initialized"
63        );
64    }
65}