Skip to main content

turtle_build/infrastructure/database/
error.rs

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