Skip to main content

systemprompt_models/auth/enums/
user_state.rs

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