sha_crypt/
errors.rs

1//! Error types.
2
3use core::fmt;
4
5#[cfg(doc)]
6use crate::Params;
7
8/// Result type for the `sha-crypt` crate with its [`Error`] type.
9pub type Result<T> = core::result::Result<T, Error>;
10
11/// Error type.
12#[derive(Debug)]
13pub enum Error {
14    /// Parameters are invalid (e.g. parse error)
15    ParamsInvalid,
16
17    /// `rounds=` be within range [`Params::ROUNDS_MIN`]..=[`Params::ROUNDS_MIN`]
18    RoundsInvalid,
19}
20
21impl core::error::Error for Error {}
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Error::ParamsInvalid => write!(f, "parameters are invalid"),
27            Error::RoundsInvalid => write!(f, "rounds error"),
28        }
29    }
30}
31
32#[cfg(feature = "password-hash")]
33impl From<Error> for password_hash::Error {
34    fn from(err: Error) -> Self {
35        match err {
36            Error::RoundsInvalid => password_hash::Error::ParamInvalid { name: "rounds" },
37            Error::ParamsInvalid => password_hash::Error::ParamsInvalid,
38        }
39    }
40}