zoe_app_primitives/group/events/
roles.rs1use forward_compatible_enum::U32Discriminants;
2use serde::{Deserialize, Serialize};
3
4use super::permissions::Permission;
5
6#[derive(
11 Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, U32Discriminants,
12)]
13#[u32_discriminants(fallback = "Member")]
14pub enum GroupRole {
15 #[discriminant(9)]
19 Owner,
20 #[discriminant(5)]
24 Admin,
25 #[discriminant(3)]
29 Moderator,
30 #[discriminant(0)]
34 Member,
35}
36
37impl GroupRole {
38 pub fn has_permission(&self, required: &Permission) -> bool {
42 match required {
43 Permission::OwnerOnly => matches!(self, GroupRole::Owner),
44 Permission::AdminOrAbove => matches!(self, GroupRole::Owner | GroupRole::Admin),
45 Permission::ModeratorOrAbove => matches!(
46 self,
47 GroupRole::Owner | GroupRole::Admin | GroupRole::Moderator
48 ),
49 Permission::AllMembers => true,
50 }
51 }
52
53 pub fn display_name(&self) -> &'static str {
55 match self {
56 GroupRole::Owner => "Owner",
57 GroupRole::Admin => "Administrator",
58 GroupRole::Moderator => "Moderator",
59 GroupRole::Member => "Member",
60 }
61 }
62
63 pub fn can_assign_role(&self, target_role: &GroupRole) -> bool {
67 match self {
68 GroupRole::Owner => true, GroupRole::Admin => !matches!(target_role, GroupRole::Owner),
70 GroupRole::Moderator => matches!(target_role, GroupRole::Member),
71 GroupRole::Member => false, }
73 }
74}