password_hash/
error.rs

1//! Error types.
2
3use core::fmt;
4
5/// Result type.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Password hashing errors.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Unsupported algorithm.
13    Algorithm,
14
15    /// Cryptographic error.
16    Crypto,
17
18    /// Encoding errors (e.g. Base64).
19    EncodingInvalid,
20
21    /// Out of memory (heap allocation failure).
22    OutOfMemory,
23
24    /// Output size invalid.
25    OutputSize,
26
27    /// Invalid named parameter.
28    ParamInvalid {
29        /// Parameter name.
30        name: &'static str,
31    },
32
33    /// Invalid parameters.
34    ParamsInvalid,
35
36    /// Invalid password.
37    PasswordInvalid,
38
39    /// Invalid salt.
40    SaltInvalid,
41
42    /// Invalid algorithm version.
43    Version,
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
48        match self {
49            Self::Algorithm => write!(f, "unsupported algorithm"),
50            Self::Crypto => write!(f, "cryptographic error"),
51            Self::EncodingInvalid => write!(f, "invalid encoding"),
52            Self::OutOfMemory => write!(f, "out of memory"),
53            Self::OutputSize => write!(f, "password hash output size invalid"),
54            Self::ParamInvalid { name } => write!(f, "invalid parameter: {name:?}"),
55            Self::ParamsInvalid => write!(f, "invalid parameters"),
56            Self::PasswordInvalid => write!(f, "invalid password"),
57            Self::SaltInvalid => write!(f, "invalid salt"),
58            Self::Version => write!(f, "invalid algorithm version"),
59        }
60    }
61}
62
63impl core::error::Error for Error {}