1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::fmt::{Debug, Display, Formatter};
use std::io;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("{0}")]
    Io(#[from] io::Error),
    #[error("Password doesn't match")]
    PasswordNotMatch,
    #[error("{0}")]
    PasswordHash(#[from] PasswordHashError),
    #[error("Invalid salt")]
    InvalidSalt,
}

#[repr(transparent)]
#[derive(Debug)]
pub struct PasswordHashError(argon2::password_hash::Error);

impl Display for PasswordHashError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("{:?}", self))
    }
}

impl std::error::Error for PasswordHashError {}

impl From<argon2::password_hash::Error> for PasswordHashError {
    fn from(value: argon2::password_hash::Error) -> Self {
        Self(value)
    }
}

impl From<argon2::password_hash::Error> for Error {
    fn from(value: argon2::password_hash::Error) -> Self {
        Self::PasswordHash(value.into())
    }
}