1use core::fmt;
4
5use hpke_crypto::CryptoError;
6
7#[derive(Debug)]
9pub enum Error {
10 InvalidInput(&'static str),
12
13 InconsistentPsk,
15
16 MissingPsk,
18
19 UnnecessaryPsk,
21
22 InsecurePsk,
24
25 MessageLimitReached,
27
28 CryptoError(CryptoError),
30}
31
32impl Error {
33 pub const fn is_invalid_input(&self) -> bool {
35 matches!(self, Error::InvalidInput(_))
36 }
37
38 pub const fn is_unsupported_kem(&self) -> bool {
41 matches!(self, Error::CryptoError(CryptoError::KemUnsupported))
42 }
43}
44
45impl core::error::Error for Error {
46 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
47 match self {
48 Error::CryptoError(e) => Some(e),
49 _ => None,
50 }
51 }
52}
53
54impl fmt::Display for Error {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 Error::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
58 Error::InconsistentPsk => write!(f, "Inconsistent PSK input"),
59 Error::MissingPsk => write!(f, "PSK input is required but missing"),
60 Error::UnnecessaryPsk => write!(f, "PSK input is provided but not needed"),
61 Error::InsecurePsk => {
62 write!(f, "PSK input is too short (needs to be at least 32 bytes)")
63 }
64 Error::MessageLimitReached => {
65 write!(f, "The message limit for this AEAD, key, and nonce")
66 }
67 Error::CryptoError(e) => write!(f, "Crypto error: {e}"),
68 }
69 }
70}
71
72impl From<CryptoError> for Error {
73 fn from(e: CryptoError) -> Self {
74 Error::CryptoError(e)
75 }
76}