use core::fmt::{Display, Formatter, Result};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HkErr {
Attrkey(attrkey::AttrkeyError),
Argon2(argon2::Error),
MemoryTooSmall,
TimeTooSmall,
ThreadsTooFew,
ThreadsTooMany,
ConstraintTooSmall,
CharsTooFew,
CharsTooMany,
BadConfirmation,
AttributeNotFound(String),
}
impl Display for HkErr {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let msg = match self {
Self::Attrkey(e) => return Display::fmt(e, f),
Self::Argon2(e) => return Display::fmt(e, f),
Self::MemoryTooSmall => "memory cost is too small",
Self::TimeTooSmall => "time cost is too small",
Self::ThreadsTooFew => "not enough threads",
Self::ThreadsTooMany => "too many threads",
Self::ConstraintTooSmall => "constraint is too small",
Self::CharsTooFew => "not enough code characters",
Self::CharsTooMany => "too many code characters",
Self::BadConfirmation => "confirmation code invalid",
Self::AttributeNotFound(kind) => {
return write!(f, "selected attribute {kind} not found");
}
};
f.write_str(msg)
}
}
impl std::error::Error for HkErr {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Attrkey(e) => Some(e),
Self::Argon2(e) => Some(e),
_ => None,
}
}
}
impl From<attrkey::AttrkeyError> for HkErr {
fn from(value: attrkey::AttrkeyError) -> Self {
Self::Attrkey(value)
}
}
impl From<argon2::Error> for HkErr {
fn from(value: argon2::Error) -> Self {
Self::Argon2(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn display() {
assert_eq!(
HkErr::MemoryTooSmall.to_string(),
"memory cost is too small"
);
assert_eq!(HkErr::TimeTooSmall.to_string(), "time cost is too small");
assert_eq!(HkErr::ThreadsTooFew.to_string(), "not enough threads");
assert_eq!(HkErr::ThreadsTooMany.to_string(), "too many threads");
assert_eq!(
HkErr::ConstraintTooSmall.to_string(),
"constraint is too small"
);
assert_eq!(
HkErr::CharsTooFew.to_string(),
"not enough code characters"
);
assert_eq!(HkErr::CharsTooMany.to_string(), "too many code characters");
assert_eq!(
HkErr::BadConfirmation.to_string(),
"confirmation code invalid"
);
assert_eq!(
HkErr::AttributeNotFound("Name".to_string()).to_string(),
"selected attribute Name not found"
);
}
#[test]
fn display_attrkey_passthrough() {
let inner = attrkey::AttrkeyError::AttributesTooFew;
let wrapped = HkErr::Attrkey(inner.clone());
assert_eq!(wrapped.to_string(), inner.to_string());
}
#[test]
fn from_attrkey_converts_variant() {
let inner = attrkey::AttrkeyError::AttributesTooFew;
let converted: HkErr = inner.clone().into();
assert_eq!(converted, HkErr::Attrkey(inner));
}
#[test]
fn display_argon2_passthrough() {
let inner = argon2::Error::ThreadsTooFew;
let wrapped = HkErr::Argon2(inner.clone());
assert_eq!(wrapped.to_string(), inner.to_string());
}
#[test]
fn from_argon2_converts_variant() {
let inner = argon2::Error::ThreadsTooFew;
let converted: HkErr = inner.clone().into();
assert_eq!(converted, HkErr::Argon2(inner));
}
#[test]
fn source() {
assert!(HkErr::MemoryTooSmall.source().is_none());
let inner = attrkey::AttrkeyError::AttributesTooFew;
let wrapped = HkErr::Attrkey(inner.clone());
let source = wrapped
.source()
.expect("Attrkey variant should have a source");
assert_eq!(
source.downcast_ref::<attrkey::AttrkeyError>(),
Some(&inner)
);
let inner = argon2::Error::ThreadsTooFew;
let wrapped = HkErr::Argon2(inner.clone());
let source = wrapped
.source()
.expect("Argon2 variant should have a source");
assert_eq!(source.downcast_ref::<argon2::Error>(), Some(&inner));
}
}