Skip to main content

omgbase_store/
error.rs

1//! The crate's error type.
2
3use std::fmt;
4
5/// Everything that can go wrong opening or writing a store.
6#[derive(Debug)]
7pub enum Error {
8    /// SQLite reported an error.
9    Sqlite(rusqlite::Error),
10    /// The database's `user_version` is newer than this build (spec §1 step 3).
11    SchemaTooNew {
12        /// The database's `user_version`.
13        found: i64,
14        /// [`crate::SCHEMA_VERSION`].
15        supported: i64,
16    },
17    /// A timestamp is not RFC 3339 UTC as spec §2.4 requires.
18    InvalidTimestamp(String),
19    /// A JSON column did not parse (tree entries, attrs, detail).
20    Json(serde_json::Error),
21    /// The embedding provider failed (`spec/search` §5).
22    Search(omgbase_search::Error),
23    /// A mutation was refused (`spec/mutate` §8): the code and its data.
24    Mutation(omgbase_mutate::MutationError),
25    /// Anything else, with a message.
26    Other(String),
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Error::Sqlite(e) => write!(f, "sqlite: {e}"),
33            Error::SchemaTooNew { found, supported } => write!(
34                f,
35                "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
36            ),
37            Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
38            Error::Json(e) => write!(f, "json: {e}"),
39            Error::Search(e) => write!(f, "search: {e}"),
40            Error::Mutation(e) => write!(f, "mutation: {e}"),
41            Error::Other(msg) => f.write_str(msg),
42        }
43    }
44}
45
46impl std::error::Error for Error {
47    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match self {
49            Error::Sqlite(e) => Some(e),
50            Error::Json(e) => Some(e),
51            Error::Search(e) => Some(e),
52            Error::Mutation(e) => Some(e),
53            _ => None,
54        }
55    }
56}
57
58impl From<rusqlite::Error> for Error {
59    fn from(e: rusqlite::Error) -> Self {
60        Error::Sqlite(e)
61    }
62}
63
64impl From<serde_json::Error> for Error {
65    fn from(e: serde_json::Error) -> Self {
66        Error::Json(e)
67    }
68}
69
70impl From<omgbase_search::Error> for Error {
71    fn from(e: omgbase_search::Error) -> Self {
72        Error::Search(e)
73    }
74}
75
76impl From<omgbase_mutate::MutationError> for Error {
77    fn from(e: omgbase_mutate::MutationError) -> Self {
78        Error::Mutation(e)
79    }
80}
81
82impl Error {
83    /// The mutation error inside, if this is one.
84    #[must_use]
85    pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
86        match self {
87            Error::Mutation(e) => Some(e),
88            _ => None,
89        }
90    }
91}
92
93/// `Result` with this crate's [`Error`].
94pub type Result<T> = std::result::Result<T, Error>;