password_auth/
errors.rs

1//! Error types.
2
3use alloc::string::ToString;
4use core::fmt;
5
6/// Password hash parse errors.
7// This type has no public constructor and deliberately keeps
8// `password_hash::Error` out of the public API so it can evolve
9// independently (e.g. get to 1.0 faster)
10#[derive(Clone, Copy, Eq, PartialEq)]
11pub struct ParseError(password_hash::Error);
12
13impl ParseError {
14    /// Create a new parse error.
15    pub(crate) fn new(err: password_hash::Error) -> Self {
16        Self(err)
17    }
18}
19
20impl fmt::Debug for ParseError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        f.debug_tuple("ParseError")
23            .field(&self.0.to_string())
24            .finish()
25    }
26}
27
28impl fmt::Display for ParseError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(f, "{}", &self.0)
31    }
32}
33
34/// Password verification errors.
35#[derive(Clone, Copy, Debug, Eq, PartialEq)]
36pub enum VerifyError {
37    /// Password hash parsing errors.
38    Parse(ParseError),
39
40    /// Password is invalid.
41    PasswordInvalid,
42}
43
44impl fmt::Display for VerifyError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::Parse(err) => write!(f, "{err}"),
48            Self::PasswordInvalid => write!(f, "password is invalid"),
49        }
50    }
51}
52
53impl From<ParseError> for VerifyError {
54    fn from(err: ParseError) -> VerifyError {
55        VerifyError::Parse(err)
56    }
57}
58
59#[cfg(feature = "std")]
60impl std::error::Error for ParseError {}
61
62#[cfg(feature = "std")]
63impl std::error::Error for VerifyError {}