use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("object backend error: {0}")]
Backend(String),
#[error("cryptographic error: {0}")]
Crypto(String),
#[error("invalid object header: {0}")]
InvalidHeader(String),
#[error("unsupported object format version: {0}")]
UnsupportedVersion(u16),
#[error("unsupported cipher suite: {0}")]
UnsupportedSuite(u16),
#[error("unknown key id: {0}")]
UnknownKey(String),
#[error("authentication failed")]
AuthenticationFailed,
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("invalid configuration: {0}")]
Config(String),
}
impl Error {
#[cfg(feature = "store")]
pub(crate) fn backend(err: impl fmt::Display) -> Self {
Self::Backend(err.to_string())
}
pub(crate) fn crypto(err: impl fmt::Display) -> Self {
Self::Crypto(err.to_string())
}
pub(crate) fn invalid_header(err: impl fmt::Display) -> Self {
Self::InvalidHeader(err.to_string())
}
pub(crate) fn config(err: impl fmt::Display) -> Self {
Self::Config(err.to_string())
}
}
#[cfg(feature = "store")]
pub(crate) fn io_error(err: Error) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidData, err)
}
#[cfg(feature = "store")]
pub(crate) fn error_from_io(err: std::io::Error) -> Error {
match err.downcast::<Error>() {
Ok(inner) => inner,
Err(err) => Error::Io(err),
}
}