1use std::{error, fmt};
10
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14#[derive(Clone, Debug, PartialEq)]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
18pub enum Error {
19 OutputTooShort,
21
22 OutputTooLong,
24
25 PwdTooShort,
27
28 PwdTooLong,
30
31 SaltTooShort,
33
34 SaltTooLong,
36
37 AdTooShort,
39
40 AdTooLong,
42
43 SecretTooShort,
45
46 SecretTooLong,
48
49 TimeTooSmall,
51
52 TimeTooLarge,
54
55 MemoryTooLittle,
57
58 MemoryTooMuch,
60
61 LanesTooFew,
63
64 LanesTooMany,
66
67 IncorrectType,
69
70 IncorrectVersion,
72
73 DecodingFail,
75}
76
77impl Error {
78 fn msg(&self) -> &str {
79 match *self {
80 Error::OutputTooShort => "Output is too short",
81 Error::OutputTooLong => "Output is too long",
82 Error::PwdTooShort => "Password is too short",
83 Error::PwdTooLong => "Password is too long",
84 Error::SaltTooShort => "Salt is too short",
85 Error::SaltTooLong => "Salt is too long",
86 Error::AdTooShort => "Associated data is too short",
87 Error::AdTooLong => "Associated data is too long",
88 Error::SecretTooShort => "Secret is too short",
89 Error::SecretTooLong => "Secret is too long",
90 Error::TimeTooSmall => "Time cost is too small",
91 Error::TimeTooLarge => "Time cost is too large",
92 Error::MemoryTooLittle => "Memory cost is too small",
93 Error::MemoryTooMuch => "Memory cost is too large",
94 Error::LanesTooFew => "Too few lanes",
95 Error::LanesTooMany => "Too many lanes",
96 Error::IncorrectType => "There is no such type of Argon2",
97 Error::IncorrectVersion => "There is no such version of Argon2",
98 Error::DecodingFail => "Decoding failed",
99 }
100 }
101}
102
103impl fmt::Display for Error {
104 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105 write!(f, "{}", self.msg())
106 }
107}
108
109impl error::Error for Error {
110 fn description(&self) -> &str {
111 self.msg()
112 }
113
114 fn cause(&self) -> Option<&dyn error::Error> {
115 None
116 }
117}
118
119impl From<base64::DecodeError> for Error {
120 fn from(_: base64::DecodeError) -> Self {
121 Error::DecodingFail
122 }
123}