use crate::key::{Algorithm, Operation};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Unsupported {
operation: Operation,
algorithm: Algorithm,
},
AlgorithmMismatch {
expected: Algorithm,
found: Algorithm,
},
UnsupportedParam {
param: &'static str,
},
InvalidParams,
Signature,
Encryption,
Decryption,
KeyAgreement,
Encapsulation,
Decapsulation,
Encoding,
}
impl Error {
pub fn unsupported(operation: Operation, algorithm: Algorithm) -> Self {
Error::Unsupported {
operation,
algorithm,
}
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Unsupported {
operation,
algorithm,
} => write!(f, "{algorithm:?} keys do not support {operation}"),
Error::AlgorithmMismatch { expected, found } => write!(
f,
"key-agreement peer mismatch: expected {expected:?}, got {found:?}"
),
Error::UnsupportedParam { param } => {
write!(
f,
"the `{param}` parameter is not supported by this algorithm"
)
}
Error::InvalidParams => {
f.write_str("a parameter value is not supported by this algorithm")
}
Error::Signature => f.write_str("signature operation failed"),
Error::Encryption => f.write_str("encryption failed"),
Error::Decryption => f.write_str("decryption failed"),
Error::KeyAgreement => f.write_str("key agreement failed"),
Error::Encapsulation => f.write_str("KEM encapsulation failed"),
Error::Decapsulation => f.write_str("KEM decapsulation failed"),
Error::Encoding => f.write_str("key encoding/decoding failed"),
}
}
}
impl core::error::Error for Error {}