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 `phc::Error` out of the public API
8// so we can upgrade the `phc` version without it being a breaking change
9#[derive(Clone, Copy, Eq, PartialEq)]
10pub struct ParseError(phc::Error);
11
12impl ParseError {
13    /// Create a new parse error.
14    pub(crate) fn new(err: phc::Error) -> Self {
15        Self(err)
16    }
17}
18
19impl fmt::Debug for ParseError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        f.debug_tuple("ParseError")
22            .field(&self.0.to_string())
23            .finish()
24    }
25}
26
27impl fmt::Display for ParseError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{}", &self.0)
30    }
31}
32
33/// Password verification errors.
34#[derive(Clone, Copy, Debug, Eq, PartialEq)]
35pub enum VerifyError {
36    /// Password hash parsing errors.
37    Parse(ParseError),
38
39    /// Password is invalid.
40    PasswordInvalid,
41}
42
43impl fmt::Display for VerifyError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::Parse(err) => write!(f, "{err}"),
47            Self::PasswordInvalid => write!(f, "password is invalid"),
48        }
49    }
50}
51
52impl From<ParseError> for VerifyError {
53    fn from(err: ParseError) -> VerifyError {
54        VerifyError::Parse(err)
55    }
56}
57
58#[cfg(feature = "std")]
59impl std::error::Error for ParseError {}
60
61#[cfg(feature = "std")]
62impl std::error::Error for VerifyError {}