use std::fmt;
#[derive(Debug)]
pub enum Error {
Sqlite(rusqlite::Error),
SchemaTooNew {
found: i64,
supported: i64,
},
InvalidTimestamp(String),
Json(serde_json::Error),
Search(omgbase_search::Error),
Mutation(omgbase_mutate::MutationError),
Other(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Sqlite(e) => write!(f, "sqlite: {e}"),
Error::SchemaTooNew { found, supported } => write!(
f,
"database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
),
Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
Error::Json(e) => write!(f, "json: {e}"),
Error::Search(e) => write!(f, "search: {e}"),
Error::Mutation(e) => write!(f, "mutation: {e}"),
Error::Other(msg) => f.write_str(msg),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Sqlite(e) => Some(e),
Error::Json(e) => Some(e),
Error::Search(e) => Some(e),
Error::Mutation(e) => Some(e),
_ => None,
}
}
}
impl From<rusqlite::Error> for Error {
fn from(e: rusqlite::Error) -> Self {
Error::Sqlite(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
impl From<omgbase_search::Error> for Error {
fn from(e: omgbase_search::Error) -> Self {
Error::Search(e)
}
}
impl From<omgbase_mutate::MutationError> for Error {
fn from(e: omgbase_mutate::MutationError) -> Self {
Error::Mutation(e)
}
}
impl Error {
#[must_use]
pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
match self {
Error::Mutation(e) => Some(e),
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;