rama_net/user/
id.rs

1#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2/// The identifier of a user.
3///
4/// Usually created by the layer which authenticates the user.
5pub enum UserId {
6    /// User identified by a username.
7    ///
8    /// E.g. the username of a Basic Auth user.
9    Username(String),
10    /// User identified by a token.
11    ///
12    /// E.g. the token of a Bearer Auth user.
13    Token(Vec<u8>),
14    /// User remains anonymous.
15    ///
16    /// E.g. the user is not authenticated via any credentials.
17    Anonymous,
18}
19
20impl PartialEq<str> for UserId {
21    fn eq(&self, other: &str) -> bool {
22        match self {
23            UserId::Username(username) => username == other,
24            UserId::Token(token) => {
25                let other = other.as_bytes();
26                token == other
27            }
28            UserId::Anonymous => false,
29        }
30    }
31}
32
33impl PartialEq<UserId> for str {
34    fn eq(&self, other: &UserId) -> bool {
35        other == self
36    }
37}
38
39impl PartialEq<[u8]> for UserId {
40    fn eq(&self, other: &[u8]) -> bool {
41        match self {
42            UserId::Username(username) => {
43                let username_bytes = username.as_bytes();
44                username_bytes == other
45            }
46            UserId::Token(token) => token == other,
47            UserId::Anonymous => false,
48        }
49    }
50}
51
52impl PartialEq<UserId> for [u8] {
53    fn eq(&self, other: &UserId) -> bool {
54        other == self
55    }
56}
57
58impl PartialEq<String> for UserId {
59    fn eq(&self, other: &String) -> bool {
60        match self {
61            UserId::Username(username) => username == other,
62            UserId::Token(token) => {
63                let other = other.as_bytes();
64                token == other
65            }
66            UserId::Anonymous => false,
67        }
68    }
69}
70
71impl PartialEq<UserId> for String {
72    fn eq(&self, other: &UserId) -> bool {
73        other == self
74    }
75}
76
77impl PartialEq<Vec<u8>> for UserId {
78    fn eq(&self, other: &Vec<u8>) -> bool {
79        match self {
80            UserId::Username(username) => {
81                let username_bytes = username.as_bytes();
82                username_bytes == other
83            }
84            UserId::Token(token) => token == other,
85            UserId::Anonymous => false,
86        }
87    }
88}
89
90impl PartialEq<UserId> for Vec<u8> {
91    fn eq(&self, other: &UserId) -> bool {
92        other == self
93    }
94}