//! Errors for the triple store.

/// Errors surfaced by `TripleStore`.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Underlying redb database failure.
    #[error("redb: {0}")]
    Db(String),
    /// Canonical encoding/decoding failure.
    #[error("canonical: {0}")]
    Canonical(#[from] ontoref_types::Error),
    /// An identifier exceeded the 65535-byte key encoding limit.
    #[error("identifier too long ({0} bytes)")]
    IdentifierTooLong(usize),
    /// A stored EAV row's key bytes did not decode to a valid (attr, hlc)
    /// tuple.
    #[error("corrupt key in eav table")]
    CorruptKey,
}

macro_rules! redb_from {
    ($($t:ty),*) => {
        $(impl From<$t> for Error {
            fn from(err: $t) -> Self {
                Error::Db(err.to_string())
            }
        })*
    };
}

redb_from!(
    redb::Error,
    redb::DatabaseError,
    redb::TransactionError,
    redb::TableError,
    redb::StorageError,
    redb::CommitError
);

/// Convenience alias.
pub type Result<T> = std::result::Result<T, Error>;
