mirror of
https://github.com/dani-garcia/vaultwarden
synced 2024-12-14 17:43:46 +01:00
Simplify implementation of UserOrgType::cmp()
Also move `UserOrgType::from_str()` closer to the definition of `UserOrgType` since it references specific enum values.
This commit is contained in:
parent
a0d92a167c
commit
0eee907c88
1 changed files with 33 additions and 28 deletions
|
@ -44,24 +44,28 @@ pub enum UserOrgType {
|
||||||
Manager = 3,
|
Manager = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UserOrgType {
|
||||||
|
pub fn from_str(s: &str) -> Option<Self> {
|
||||||
|
match s {
|
||||||
|
"0" | "Owner" => Some(UserOrgType::Owner),
|
||||||
|
"1" | "Admin" => Some(UserOrgType::Admin),
|
||||||
|
"2" | "User" => Some(UserOrgType::User),
|
||||||
|
"3" | "Manager" => Some(UserOrgType::Manager),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Ord for UserOrgType {
|
impl Ord for UserOrgType {
|
||||||
fn cmp(&self, other: &UserOrgType) -> Ordering {
|
fn cmp(&self, other: &UserOrgType) -> Ordering {
|
||||||
if self == other {
|
// For easy comparison, map each variant to an access level (where 0 is lowest).
|
||||||
Ordering::Equal
|
static ACCESS_LEVEL: [i32; 4] = [
|
||||||
} else {
|
3, // Owner
|
||||||
match self {
|
2, // Admin
|
||||||
UserOrgType::Owner => Ordering::Greater,
|
0, // User
|
||||||
UserOrgType::Admin => match other {
|
1, // Manager
|
||||||
UserOrgType::Owner => Ordering::Less,
|
];
|
||||||
_ => Ordering::Greater,
|
ACCESS_LEVEL[*self as usize].cmp(&ACCESS_LEVEL[*other as usize])
|
||||||
},
|
|
||||||
UserOrgType::Manager => match other {
|
|
||||||
UserOrgType::Owner | UserOrgType::Admin => Ordering::Less,
|
|
||||||
_ => Ordering::Greater,
|
|
||||||
},
|
|
||||||
UserOrgType::User => Ordering::Less,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,18 +133,6 @@ impl PartialOrd<UserOrgType> for i32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserOrgType {
|
|
||||||
pub fn from_str(s: &str) -> Option<Self> {
|
|
||||||
match s {
|
|
||||||
"0" | "Owner" => Some(UserOrgType::Owner),
|
|
||||||
"1" | "Admin" => Some(UserOrgType::Admin),
|
|
||||||
"2" | "User" => Some(UserOrgType::User),
|
|
||||||
"3" | "Manager" => Some(UserOrgType::Manager),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Local methods
|
/// Local methods
|
||||||
impl Organization {
|
impl Organization {
|
||||||
pub fn new(name: String, billing_email: String) -> Self {
|
pub fn new(name: String, billing_email: String) -> Self {
|
||||||
|
@ -533,3 +525,16 @@ impl UserOrganization {
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
fn partial_cmp_UserOrgType() {
|
||||||
|
assert!(UserOrgType::Owner > UserOrgType::Admin);
|
||||||
|
assert!(UserOrgType::Admin > UserOrgType::Manager);
|
||||||
|
assert!(UserOrgType::Manager > UserOrgType::User);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue