use std::fmt::{Debug, Display};
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Argon2Error {
OutputPtrNull,
OutputTooShort,
OutputTooLong,
PasswordTooShort,
PasswordTooLong,
SaltTooShort,
SaltTooLong,
AdTooShort,
AdTooLong,
SecretTooShort,
SecretTooLong,
TimeTooSmall,
TimeTooLarge,
MemoryTooLittle,
MemoryTooMuch,
LanesTooFew,
LanesTooMany,
PwdPtrMismatch,
SaltPtrMismatch,
SecretPtrMismatch,
AdPtrMismatch,
MemoryAllocationError,
FreeMemoryCbkNull,
AllocateMemoryCbkNull,
IncorrectParameter,
IncorrectType,
OutPtrMismatch,
ThreadsTooFew,
ThreadsTooMany,
MissingArgs,
EncodingFail,
DecodingFail,
ThreadFail,
DecodingLengthFail,
VerifyMismatch,
Unknown(i32),
}
pub(crate) fn map_argon2_error(code: i32) -> Argon2Error {
match code {
-1 => Argon2Error::OutputPtrNull,
-2 => Argon2Error::OutputTooShort,
-3 => Argon2Error::OutputTooLong,
-4 => Argon2Error::PasswordTooShort,
-5 => Argon2Error::PasswordTooLong,
-6 => Argon2Error::SaltTooShort,
-7 => Argon2Error::SaltTooLong,
-8 => Argon2Error::AdTooShort,
-9 => Argon2Error::AdTooLong,
-10 => Argon2Error::SecretTooShort,
-11 => Argon2Error::SecretTooLong,
-12 => Argon2Error::TimeTooSmall,
-13 => Argon2Error::TimeTooLarge,
-14 => Argon2Error::MemoryTooLittle,
-15 => Argon2Error::MemoryTooMuch,
-16 => Argon2Error::LanesTooFew,
-17 => Argon2Error::LanesTooMany,
-18 => Argon2Error::PwdPtrMismatch,
-19 => Argon2Error::SaltPtrMismatch,
-20 => Argon2Error::SecretPtrMismatch,
-21 => Argon2Error::AdPtrMismatch,
-22 => Argon2Error::MemoryAllocationError,
-23 => Argon2Error::FreeMemoryCbkNull,
-24 => Argon2Error::AllocateMemoryCbkNull,
-25 => Argon2Error::IncorrectParameter,
-26 => Argon2Error::IncorrectType,
-27 => Argon2Error::OutPtrMismatch,
-28 => Argon2Error::ThreadsTooFew,
-29 => Argon2Error::ThreadsTooMany,
-30 => Argon2Error::MissingArgs,
-31 => Argon2Error::EncodingFail,
-32 => Argon2Error::DecodingFail,
-33 => Argon2Error::ThreadFail,
-34 => Argon2Error::DecodingLengthFail,
-35 => Argon2Error::VerifyMismatch,
_ => Argon2Error::Unknown(code),
}
}
impl std::error::Error for Argon2Error {}
impl Display for Argon2Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Argon2Error::OutputPtrNull => write!(f, "Output pointer is null"),
Argon2Error::OutputTooShort => write!(f, "Output is too short"),
Argon2Error::OutputTooLong => write!(f, "Output is too long"),
Argon2Error::PasswordTooShort => write!(f, "Password is too short"),
Argon2Error::PasswordTooLong => write!(f, "Password is too long"),
Argon2Error::SaltTooShort => write!(f, "Salt is too short"),
Argon2Error::SaltTooLong => write!(f, "Salt is too long"),
Argon2Error::AdTooShort => write!(f, "Associated data is too short"),
Argon2Error::AdTooLong => write!(f, "Associated data is too long"),
Argon2Error::SecretTooShort => write!(f, "Secret is too short"),
Argon2Error::SecretTooLong => write!(f, "Secret is too long"),
Argon2Error::TimeTooSmall => write!(f, "Time cost is too small"),
Argon2Error::TimeTooLarge => write!(f, "Time cost is too large"),
Argon2Error::MemoryTooLittle => write!(f, "Memory cost is too little"),
Argon2Error::MemoryTooMuch => write!(f, "Memory cost is too much"),
Argon2Error::LanesTooFew => write!(f, "Number of lanes is too few"),
Argon2Error::LanesTooMany => write!(f, "Number of lanes is too many"),
Argon2Error::PwdPtrMismatch => write!(f, "Password pointer mismatch"),
Argon2Error::SaltPtrMismatch => write!(f, "Salt pointer mismatch"),
Argon2Error::SecretPtrMismatch => write!(f, "Secret pointer mismatch"),
Argon2Error::AdPtrMismatch => write!(f, "Associated data pointer mismatch"),
Argon2Error::MemoryAllocationError => write!(f, "Memory allocation error"),
Argon2Error::FreeMemoryCbkNull => write!(f, "Free memory callback is null"),
Argon2Error::AllocateMemoryCbkNull => write!(f, "Allocate memory callback is null"),
Argon2Error::IncorrectParameter => write!(f, "Incorrect parameter"),
Argon2Error::IncorrectType => write!(f, "Incorrect Argon2 type"),
Argon2Error::OutPtrMismatch => write!(f, "Output pointer mismatch"),
Argon2Error::ThreadsTooFew => write!(f, "Number of threads is too few"),
Argon2Error::ThreadsTooMany => write!(f, "Number of threads is too many"),
Argon2Error::MissingArgs => write!(f, "Missing arguments"),
Argon2Error::EncodingFail => write!(f, "Encoding failed"),
Argon2Error::DecodingFail => write!(f, "Decoding failed"),
Argon2Error::ThreadFail => write!(f, "Thread failed"),
Argon2Error::DecodingLengthFail => write!(f, "Decoding length failed"),
Argon2Error::VerifyMismatch => write!(f, "Verification mismatch"),
Argon2Error::Unknown(code) => write!(f, "Unknown argon2 error with code: {code}"),
}
}
}
impl Debug for Argon2Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}