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
51
52
53
54
55
56
57
58
59
60
use serde::{Deserialize,Serialize,Deserializer};

// #[derive(Error, Debug)]
// pub enum AuthError {
//     #[error("error")]
//     SomeError(),
//     #[error("no authorization header found")]
//     NoAuthHeaderFoundError,
//     #[error("wrong authorization header format")]
//     InvalidAuthHeaderFormatError,
//     #[error("no user found for this token")]
//     InvalidTokenError,
//     #[error("error during authorization")]
//     AuthorizationError,
//     #[error("user is not unauthorized")]
//     UnauthorizedError,
//     #[error("no user found with this name")]
//     UserNotFoundError,
// }

#[derive(Eq, PartialEq, Clone, Serialize, Debug, Deserialize)]
pub enum UserRole {
  SuperUser,
  Developer,
  User,
  Anonymous,
}
impl Default for UserRole {
    fn default() -> Self {
       UserRole::Anonymous
    }
}
impl std::fmt::Display for UserRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UserRole::SuperUser => write!(f,"superuser"),
            UserRole::Developer => write!(f,"developer"),
            UserRole::User => write!(f,"user"),
            UserRole::Anonymous => write!(f,"anonymous"),
        }
    }
}
impl UserRole {
    #[allow(dead_code)]
    pub fn from_str(value: &str) -> UserRole {
        match value {
            "superuser" | "SuperUser" | "superUser" | "admin"  => UserRole::SuperUser,
            "developer" |  "Developer" => UserRole::Developer,
            "user" | "User" => UserRole::User,
            "anonymous" | "Anonymous" => UserRole::Anonymous,
            _ => UserRole::default(),
        }
    }
}
#[allow(dead_code)]
pub fn deserialize_user_role<'de, D>(deserializer: D) -> Result<UserRole, D::Error>
where D: Deserializer<'de> {
    let buf = String::deserialize(deserializer)?;
    Ok(UserRole::from_str(&buf))
}