Skip to main content

auth0_integration/models/
role.rs

1use std::str::FromStr;
2
3use crate::error::AppError;
4
5/// Represents a user role in Auth0.
6///
7/// The `FromStr` impl accepts case-insensitive short names (`"admin"`, `"super_admin"`, `"worker"`).
8/// `as_auth0_name()` returns the exact string stored in Auth0 (`"ADMIN"`, `"SUPER_ADMIN"`, `"WORKER"`).
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Role {
11    Admin,
12    SuperAdmin,
13    Worker,
14}
15
16impl Role {
17    /// Returns the role name as stored in Auth0.
18    pub fn as_auth0_name(&self) -> &str {
19        match self {
20            Role::Admin => "ADMIN",
21            Role::SuperAdmin => "SUPER_ADMIN",
22            Role::Worker => "WORKER",
23        }
24    }
25}
26
27impl FromStr for Role {
28    type Err = AppError;
29
30    /// Parses a role from a user-friendly string (case-insensitive).
31    ///
32    /// Accepted values: `"admin"`, `"super_admin"`, `"worker"`.
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s.to_lowercase().as_str() {
35            "admin" => Ok(Role::Admin),
36            "super_admin" => Ok(Role::SuperAdmin),
37            "worker" => Ok(Role::Worker),
38            _ => Err(AppError::InvalidRole(s.to_string())),
39        }
40    }
41}