use core::fmt;
use hpke_crypto::CryptoError;
#[derive(Debug)]
pub enum Error {
InvalidInput(&'static str),
InconsistentPsk,
MissingPsk,
UnnecessaryPsk,
InsecurePsk,
MessageLimitReached,
CryptoError(CryptoError),
}
impl Error {
pub const fn is_invalid_input(&self) -> bool {
matches!(self, Error::InvalidInput(_))
}
pub const fn is_unsupported_kem(&self) -> bool {
matches!(self, Error::CryptoError(CryptoError::KemUnsupported))
}
}
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Error::CryptoError(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
Error::InconsistentPsk => write!(f, "Inconsistent PSK input"),
Error::MissingPsk => write!(f, "PSK input is required but missing"),
Error::UnnecessaryPsk => write!(f, "PSK input is provided but not needed"),
Error::InsecurePsk => {
write!(f, "PSK input is too short (needs to be at least 32 bytes)")
}
Error::MessageLimitReached => {
write!(f, "The message limit for this AEAD, key, and nonce")
}
Error::CryptoError(e) => write!(f, "Crypto error: {e}"),
}
}
}
impl From<CryptoError> for Error {
fn from(e: CryptoError) -> Self {
Error::CryptoError(e)
}
}