use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
UnsupportedCipher,
InvalidKey,
InvalidKeyLength {
expected: usize,
actual: usize,
},
InvalidPayload,
InvalidMac,
Decrypt,
Encrypt,
Unserialize,
Serialize,
InvalidUtf8,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::UnsupportedCipher => f.write_str(
"Unsupported cipher. Supported ciphers are: aes-128-cbc, aes-256-cbc, \
aes-128-gcm, aes-256-gcm.",
),
Error::InvalidKey => f.write_str("The encryption key is empty or malformed."),
Error::InvalidKeyLength { expected, actual } => write!(
f,
"Incorrect key length for the cipher: expected {expected} bytes, got {actual}."
),
Error::InvalidPayload => f.write_str("The payload is invalid."),
Error::InvalidMac => f.write_str("The MAC is invalid."),
Error::Decrypt => f.write_str("Could not decrypt the data."),
Error::Encrypt => f.write_str("Could not encrypt the data."),
Error::Unserialize => f.write_str("The decrypted data is not valid PHP serialization."),
Error::Serialize => f.write_str("The value cannot be PHP-serialized."),
Error::InvalidUtf8 => f.write_str("The decrypted data is not valid UTF-8."),
}
}
}
impl std::error::Error for Error {}