nervusdb_v2/
error.rs

1use std::fmt;
2
3/// The error type for NervusDB operations.
4#[derive(Debug)]
5pub enum Error {
6    /// IO error interacting with the filesystem.
7    Io(std::io::Error),
8    /// Error returned by the storage engine.
9    Storage(String),
10    /// Error during query execution.
11    Query(String),
12    /// Other errors.
13    Other(String),
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Error::Io(e) => write!(f, "IO error: {}", e),
20            Error::Storage(e) => write!(f, "Storage error: {}", e),
21            Error::Query(e) => write!(f, "Query error: {}", e),
22            Error::Other(e) => write!(f, "Error: {}", e),
23        }
24    }
25}
26
27impl std::error::Error for Error {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            Error::Io(e) => Some(e),
31            _ => None,
32        }
33    }
34}
35
36impl From<std::io::Error> for Error {
37    fn from(e: std::io::Error) -> Self {
38        Error::Io(e)
39    }
40}
41
42// Convert storage errors to string to hide internal types
43impl From<nervusdb_v2_storage::Error> for Error {
44    fn from(e: nervusdb_v2_storage::Error) -> Self {
45        match e {
46            nervusdb_v2_storage::Error::Io(e) => Error::Io(e),
47            _ => Error::Storage(e.to_string()),
48        }
49    }
50}
51
52// Convert query errors to string to hide internal types
53impl From<nervusdb_v2_query::Error> for Error {
54    fn from(e: nervusdb_v2_query::Error) -> Self {
55        match e {
56            nervusdb_v2_query::Error::Io(e) => Error::Io(e),
57            _ => Error::Query(e.to_string()),
58        }
59    }
60}
61
62/// A specialized Result type for NervusDB operations.
63pub type Result<T> = std::result::Result<T, Error>;