balloon_hash/
error.rs

1//! Error type
2
3use core::fmt;
4
5#[cfg(feature = "password-hash")]
6use password_hash::errors::InvalidValue;
7
8/// Result with balloon's [`Error`] type.
9pub type Result<T> = core::result::Result<T, Error>;
10
11/// Error type.
12#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum Error {
15    /// Algorithm identifier invalid.
16    AlgorithmInvalid,
17    /// Memory cost is too small.
18    MemoryTooLittle,
19    /// Not enough threads.
20    ThreadsTooFew,
21    /// Too many threads.
22    ThreadsTooMany,
23    /// Time cost is too small.
24    TimeTooSmall,
25    /// Output size not correct.
26    OutputSize {
27        /// Output size provided.
28        actual: usize,
29        /// Output size expected.
30        expected: usize,
31    },
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Error::AlgorithmInvalid => f.write_str("algorithm identifier invalid"),
38            Error::MemoryTooLittle => f.write_str("memory cost is too small"),
39            Error::ThreadsTooFew => f.write_str("not enough threads"),
40            Error::ThreadsTooMany => f.write_str("too many threads"),
41            Error::TimeTooSmall => f.write_str("time cost is too small"),
42            Error::OutputSize { expected, .. } => {
43                write!(f, "unexpected output size, expected {expected} bytes")
44            }
45        }
46    }
47}
48
49#[cfg(feature = "password-hash")]
50#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
51impl From<Error> for password_hash::Error {
52    fn from(err: Error) -> password_hash::Error {
53        match err {
54            Error::AlgorithmInvalid => password_hash::Error::Algorithm,
55            Error::MemoryTooLittle => InvalidValue::TooShort.param_error(),
56            Error::ThreadsTooFew => InvalidValue::TooShort.param_error(),
57            Error::ThreadsTooMany => InvalidValue::TooLong.param_error(),
58            Error::TimeTooSmall => InvalidValue::TooShort.param_error(),
59            Error::OutputSize { actual, expected } => password_hash::Error::OutputSize {
60                provided: actual.cmp(&expected),
61                expected,
62            },
63        }
64    }
65}
66
67#[cfg(feature = "std")]
68impl std::error::Error for Error {}