use crate::Salt;
use base64ct::Error as B64Error;
use core::{cmp::Ordering, fmt};
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
Base64(B64Error),
MissingField,
OutputSize {
provided: Ordering,
expected: usize,
},
ParamNameDuplicated,
ParamNameInvalid,
ParamValueInvalid,
ParamValueTooLong,
ParamsMaxExceeded,
SaltTooShort,
SaltTooLong,
TrailingData,
ValueTooLong,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
match self {
Self::Base64(err) => write!(f, "{err}"),
Self::MissingField => write!(f, "password hash string missing field"),
Self::OutputSize { provided, expected } => match provided {
Ordering::Less => write!(
f,
"output size too short, expected at least {expected} bytes",
),
Ordering::Equal => write!(f, "output size unexpected, expected {expected} bytes"),
Ordering::Greater => {
write!(f, "output size too long, expected at most {expected} bytes")
}
},
Self::ParamNameDuplicated => write!(f, "duplicate parameter"),
Self::ParamNameInvalid => write!(f, "invalid parameter name"),
Self::ParamValueInvalid => write!(f, "invalid parameter value"),
Self::ParamValueTooLong => write!(f, "parameter value too long"),
Self::ParamsMaxExceeded => write!(f, "maximum number of parameters reached"),
Self::SaltTooShort => write!(f, "salt too short (minimum {} bytes)", Salt::MIN_LENGTH),
Self::SaltTooLong => write!(f, "salt too long (maximum {} bytes)", Salt::MAX_LENGTH),
Self::TrailingData => write!(f, "password hash has unexpected trailing characters"),
Self::ValueTooLong => f.write_str("value too long"),
}
}
}
impl core::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Base64(err) => Some(err),
_ => None,
}
}
}
impl From<B64Error> for Error {
fn from(err: B64Error) -> Error {
Error::Base64(err)
}
}
impl From<base64ct::InvalidLengthError> for Error {
fn from(_: base64ct::InvalidLengthError) -> Error {
Error::Base64(B64Error::InvalidLength)
}
}