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
42
43
44
45
46
47
48
49
50
use base64::DecodeError;
use std::fmt;
use std::string::FromUtf8Error;
#[derive(Debug)]
pub enum AuthBasicError {
InvalidAuthorizationHeader,
InvalidScheme(String),
InvalidBase64Value(String),
InvalidUTF8Value(String),
}
impl fmt::Display for AuthBasicError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AuthBasicError::InvalidAuthorizationHeader => write!(
f,
"Invalid value provided for the HTTP Authorization header"
),
AuthBasicError::InvalidScheme(scheme) => {
write!(f, "The scheme provided ({}) is not Basic", scheme)
}
AuthBasicError::InvalidBase64Value(message) => {
write!(f, "The value have an invalid base64 encoding\n{}", message)
}
AuthBasicError::InvalidUTF8Value(message) => {
write!(f, "Invalid UTF-8 Provided\n{}", message)
}
}
}
}
impl From<DecodeError> for AuthBasicError {
fn from(decode_error: DecodeError) -> Self {
AuthBasicError::InvalidBase64Value(decode_error.to_string())
}
}
impl From<FromUtf8Error> for AuthBasicError {
fn from(utf8_err: FromUtf8Error) -> Self {
AuthBasicError::InvalidUTF8Value(utf8_err.to_string())
}
}