Skip to main content

systemprompt_models/auth/enums/
user_state.rs

1//! Persisted user role and lifecycle status.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use serde::{Deserialize, Serialize};
7use std::fmt;
8use std::str::FromStr;
9
10use crate::errors::ParseEnumError;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13#[serde(rename_all = "lowercase")]
14pub enum UserRole {
15    Admin,
16    User,
17    Anonymous,
18}
19
20impl UserRole {
21    pub const fn as_str(&self) -> &'static str {
22        match self {
23            Self::Admin => "admin",
24            Self::User => "user",
25            Self::Anonymous => "anonymous",
26        }
27    }
28}
29
30impl fmt::Display for UserRole {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}", self.as_str())
33    }
34}
35
36impl FromStr for UserRole {
37    type Err = ParseEnumError;
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        match s {
40            "admin" => Ok(Self::Admin),
41            "user" => Ok(Self::User),
42            "anonymous" => Ok(Self::Anonymous),
43            _ => Err(ParseEnumError::new("user_role", s)),
44        }
45    }
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
49#[serde(rename_all = "lowercase")]
50pub enum UserStatus {
51    Active,
52    Inactive,
53    Suspended,
54    Pending,
55    Deleted,
56    Temporary,
57}
58
59impl UserStatus {
60    pub const fn as_str(&self) -> &'static str {
61        match self {
62            Self::Active => "active",
63            Self::Inactive => "inactive",
64            Self::Suspended => "suspended",
65            Self::Pending => "pending",
66            Self::Deleted => "deleted",
67            Self::Temporary => "temporary",
68        }
69    }
70
71    pub const fn is_active(&self) -> bool {
72        matches!(self, Self::Active)
73    }
74}
75
76impl fmt::Display for UserStatus {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "{}", self.as_str())
79    }
80}
81
82impl FromStr for UserStatus {
83    type Err = ParseEnumError;
84    fn from_str(s: &str) -> Result<Self, Self::Err> {
85        match s {
86            "active" => Ok(Self::Active),
87            "inactive" => Ok(Self::Inactive),
88            "suspended" => Ok(Self::Suspended),
89            "pending" => Ok(Self::Pending),
90            "deleted" => Ok(Self::Deleted),
91            "temporary" => Ok(Self::Temporary),
92            _ => Err(ParseEnumError::new("user_status", s)),
93        }
94    }
95}