use core::fmt;
#[cfg(feature = "password-hash")]
use password_hash::errors::InvalidValue;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
AlgorithmInvalid,
MemoryTooLittle,
ThreadsTooFew,
ThreadsTooMany,
TimeTooSmall,
OutputSize {
actual: usize,
expected: usize,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::AlgorithmInvalid => f.write_str("algorithm identifier invalid"),
Error::MemoryTooLittle => f.write_str("memory cost is too small"),
Error::ThreadsTooFew => f.write_str("not enough threads"),
Error::ThreadsTooMany => f.write_str("too many threads"),
Error::TimeTooSmall => f.write_str("time cost is too small"),
Error::OutputSize { expected, .. } => {
write!(f, "unexpected output size, expected {expected} bytes")
}
}
}
}
#[cfg(feature = "password-hash")]
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
impl From<Error> for password_hash::Error {
fn from(err: Error) -> password_hash::Error {
match err {
Error::AlgorithmInvalid => password_hash::Error::Algorithm,
Error::MemoryTooLittle => InvalidValue::TooShort.param_error(),
Error::ThreadsTooFew => InvalidValue::TooShort.param_error(),
Error::ThreadsTooMany => InvalidValue::TooLong.param_error(),
Error::TimeTooSmall => InvalidValue::TooShort.param_error(),
Error::OutputSize { actual, expected } => password_hash::Error::OutputSize {
provided: actual.cmp(&expected),
expected,
},
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}