1use thiserror::Error as ThisError;
2
3#[derive(ThisError, Debug)]
4pub enum Error {
5 #[error("Argon2 error: {0}")]
6 Argon2(#[from] Argon2Error),
7}
8
9#[derive(ThisError, Debug, Copy, Clone, Eq, PartialEq)]
10pub enum Argon2Error {
11 #[error("Output pointer is null")]
12 OutputPtrNull,
13 #[error("Output is too short")]
14 OutputTooShort,
15 #[error("Output is too long")]
16 OutputTooLong,
17 #[error("Password is too short")]
18 PasswordTooShort,
19 #[error("Password is too long")]
20 PasswordTooLong,
21 #[error("Salt is too short")]
22 SaltTooShort,
23 #[error("Salt is too long")]
24 SaltTooLong,
25 #[error("Associated data is too short")]
26 AdTooShort,
27 #[error("Associated data is too long")]
28 AdTooLong,
29 #[error("Secret is too short")]
30 SecretTooShort,
31 #[error("Secret is too long")]
32 SecretTooLong,
33 #[error("Time cost is too small")]
34 TimeTooSmall,
35 #[error("Time cost is too large")]
36 TimeTooLarge,
37 #[error("Memory cost is too little")]
38 MemoryTooLittle,
39 #[error("Memory cost is too much")]
40 MemoryTooMuch,
41 #[error("Number of lanes is too few")]
42 LanesTooFew,
43 #[error("Number of lanes is too many")]
44 LanesTooMany,
45 #[error("Password pointer mismatch")]
46 PwdPtrMismatch,
47 #[error("Salt pointer mismatch")]
48 SaltPtrMismatch,
49 #[error("Secret pointer mismatch")]
50 SecretPtrMismatch,
51 #[error("Associated data pointer mismatch")]
52 AdPtrMismatch,
53 #[error("Memory allocation error")]
54 MemoryAllocationError,
55 #[error("Free memory callback is null")]
56 FreeMemoryCbkNull,
57 #[error("Allocate memory callback is null")]
58 AllocateMemoryCbkNull,
59 #[error("Incorrect parameter")]
60 IncorrectParameter,
61 #[error("Incorrect Argon2 type")]
62 IncorrectType,
63 #[error("Output pointer mismatch")]
64 OutPtrMismatch,
65 #[error("Number of threads is too few")]
66 ThreadsTooFew,
67 #[error("Number of threads is too many")]
68 ThreadsTooMany,
69 #[error("Missing arguments")]
70 MissingArgs,
71 #[error("Encoding failed")]
72 EncodingFail,
73 #[error("Decoding failed")]
74 DecodingFail,
75 #[error("Thread failed")]
76 ThreadFail,
77 #[error("Decoding length failed")]
78 DecodingLengthFail,
79 #[error("Verification mismatch")]
80 VerifyMismatch,
81 #[error("Unknown argon2 error with code: {0}")]
82 Unknown(i32),
83}
84
85#[derive(ThisError, Debug)]
86pub enum CredentialsError {
87 #[error("Username is empty")]
88 UsernameEmpty,
89
90 #[error("Password is empty")]
91 PasswordEmpty,
92
93 #[error("Confirm password is empty")]
94 ConfirmPasswordEmpty,
95
96 #[error("Passwords do not match")]
97 PasswordsDoNotMatch,
98
99 #[error("{0}")]
100 Custom(String),
101}
102
103pub(crate) fn map_argon2_error(code: i32) -> Argon2Error {
104 match code {
105 -1 => Argon2Error::OutputPtrNull,
106 -2 => Argon2Error::OutputTooShort,
107 -3 => Argon2Error::OutputTooLong,
108 -4 => Argon2Error::PasswordTooShort,
109 -5 => Argon2Error::PasswordTooLong,
110 -6 => Argon2Error::SaltTooShort,
111 -7 => Argon2Error::SaltTooLong,
112 -8 => Argon2Error::AdTooShort,
113 -9 => Argon2Error::AdTooLong,
114 -10 => Argon2Error::SecretTooShort,
115 -11 => Argon2Error::SecretTooLong,
116 -12 => Argon2Error::TimeTooSmall,
117 -13 => Argon2Error::TimeTooLarge,
118 -14 => Argon2Error::MemoryTooLittle,
119 -15 => Argon2Error::MemoryTooMuch,
120 -16 => Argon2Error::LanesTooFew,
121 -17 => Argon2Error::LanesTooMany,
122 -18 => Argon2Error::PwdPtrMismatch,
123 -19 => Argon2Error::SaltPtrMismatch,
124 -20 => Argon2Error::SecretPtrMismatch,
125 -21 => Argon2Error::AdPtrMismatch,
126 -22 => Argon2Error::MemoryAllocationError,
127 -23 => Argon2Error::FreeMemoryCbkNull,
128 -24 => Argon2Error::AllocateMemoryCbkNull,
129 -25 => Argon2Error::IncorrectParameter,
130 -26 => Argon2Error::IncorrectType,
131 -27 => Argon2Error::OutPtrMismatch,
132 -28 => Argon2Error::ThreadsTooFew,
133 -29 => Argon2Error::ThreadsTooMany,
134 -30 => Argon2Error::MissingArgs,
135 -31 => Argon2Error::EncodingFail,
136 -32 => Argon2Error::DecodingFail,
137 -33 => Argon2Error::ThreadFail,
138 -34 => Argon2Error::DecodingLengthFail,
139 -35 => Argon2Error::VerifyMismatch,
140 _ => Argon2Error::Unknown(code),
141 }
142}