1use core::fmt;
4
5#[cfg(feature = "password-hash")]
6use password_hash::errors::InvalidValue;
7
8pub type Result<T> = core::result::Result<T, Error>;
10
11#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum Error {
15 AlgorithmInvalid,
17 MemoryTooLittle,
19 ThreadsTooFew,
21 ThreadsTooMany,
23 TimeTooSmall,
25 OutputSize {
27 actual: usize,
29 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 {}