use core::fmt;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("io error")]
Io(#[from] std::io::Error),
#[error("invalid or corrupted container header")]
InvalidHeader,
#[error("unsupported container version: {0}")]
UnsupportedVersion(u32),
#[error("file uses a retired krypton format")]
RetiredFormat,
#[error("authentication failed: wrong password or corrupted data")]
Authentication,
#[error("encryption failed")]
Encryption,
#[error("key derivation failed")]
KeyDerivation,
#[error("invalid KDF parameters")]
InvalidKdfParams,
#[error("invalid vault structure")]
InvalidVault,
#[error("vault already initialized")]
VaultExists,
#[error("vault not found")]
VaultNotFound,
#[error("vault is locked")]
VaultLocked,
#[error("vault already unlocked")]
VaultUnlocked,
#[error("invalid entry name: {0}")]
InvalidEntryName(String),
#[error("entry not found: {0}")]
EntryNotFound(String),
#[error("entry already exists: {0}")]
EntryExists(String),
#[error("malformed decrypted payload")]
MalformedPayload,
#[error("{0}")]
Operation(String),
}
impl Error {
pub(crate) fn invalid_name(name: impl fmt::Display) -> Self {
let s = name.to_string();
let preview: String = s.chars().take(32).collect();
Error::InvalidEntryName(preview)
}
}
pub type Result<T> = std::result::Result<T, Error>;