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("vault uses a legacy format; run `keres migrate` to upgrade it")]
MigrationRequired,
#[error("incompatible container format")]
IncompatibleFormat,
#[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 is busy: another process is using it")]
VaultBusy,
#[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("precondition failed: vault state changed")]
Conflict,
#[error("malformed decrypted payload")]
MalformedPayload,
#[error("{0}")]
Operation(String),
#[error("operation cancelled")]
Cancelled,
}
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>;