Skip to main content

greplm_core/
error.rs

1use std::path::PathBuf;
2
3/// Result type used across greplm-core.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Errors produced by the greplm core engine.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    #[error("io error at {path}: {source}")]
10    Io {
11        path: PathBuf,
12        #[source]
13        source: std::io::Error,
14    },
15
16    #[error("io error: {0}")]
17    PlainIo(#[from] std::io::Error),
18
19    #[error("json error: {0}")]
20    Json(#[from] serde_json::Error),
21
22    #[error("cache serialize error: {0}")]
23    Postcard(#[from] postcard::Error),
24
25    #[error("toml deserialize error: {0}")]
26    TomlDe(#[from] toml::de::Error),
27
28    #[error("toml serialize error: {0}")]
29    TomlSer(#[from] toml::ser::Error),
30
31    #[error("fst error: {0}")]
32    Fst(#[from] fst::Error),
33
34    #[error("redb database error: {0}")]
35    Db(String),
36
37    #[error("invalid regex: {0}")]
38    Regex(#[from] regex::Error),
39
40    #[error("index not found at {0}; run `greplm index` first")]
41    IndexMissing(PathBuf),
42
43    #[error("corrupt index: {0}")]
44    Corrupt(String),
45
46    #[error("{0}")]
47    Other(String),
48}
49
50impl Error {
51    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
52        Error::Io {
53            path: path.into(),
54            source,
55        }
56    }
57
58    pub fn other(msg: impl Into<String>) -> Self {
59        Error::Other(msg.into())
60    }
61}
62
63macro_rules! from_redb {
64    ($($t:ty),* $(,)?) => {
65        $(
66            impl From<$t> for Error {
67                fn from(e: $t) -> Self {
68                    Error::Db(e.to_string())
69                }
70            }
71        )*
72    };
73}
74
75from_redb!(
76    redb::Error,
77    redb::DatabaseError,
78    redb::TransactionError,
79    redb::TableError,
80    redb::StorageError,
81    redb::CommitError,
82);