Skip to main content

systemprompt_security/authz/types/
kinds.rs

1use std::fmt;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6use crate::authz::error::AuthzError;
7
8#[derive(
9    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, sqlx::Type,
10)]
11#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
12#[serde(rename_all = "lowercase")]
13pub enum RuleType {
14    User,
15    Role,
16}
17
18impl fmt::Display for RuleType {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        f.write_str(match *self {
21            Self::User => "user",
22            Self::Role => "role",
23        })
24    }
25}
26
27impl FromStr for RuleType {
28    type Err = AuthzError;
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        match s {
32            "user" => Ok(Self::User),
33            "role" => Ok(Self::Role),
34            other => Err(AuthzError::InvalidRuleType(other.to_owned())),
35        }
36    }
37}
38
39#[derive(
40    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, sqlx::Type,
41)]
42#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
43#[serde(rename_all = "lowercase")]
44pub enum Access {
45    Allow,
46    Deny,
47}
48
49impl fmt::Display for Access {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        f.write_str(match *self {
52            Self::Allow => "allow",
53            Self::Deny => "deny",
54        })
55    }
56}
57
58impl FromStr for Access {
59    type Err = AuthzError;
60
61    fn from_str(s: &str) -> Result<Self, Self::Err> {
62        match s {
63            "allow" => Ok(Self::Allow),
64            "deny" => Ok(Self::Deny),
65            other => Err(AuthzError::InvalidAccess(other.to_owned())),
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum EntityKind {
73    GatewayRoute,
74    McpServer,
75    Plugin,
76    Agent,
77    Marketplace,
78    Skill,
79    Hook,
80}
81
82impl EntityKind {
83    pub const fn as_str(self) -> &'static str {
84        match self {
85            Self::GatewayRoute => "gateway_route",
86            Self::McpServer => "mcp_server",
87            Self::Plugin => "plugin",
88            Self::Agent => "agent",
89            Self::Marketplace => "marketplace",
90            Self::Skill => "skill",
91            Self::Hook => "hook",
92        }
93    }
94}
95
96impl FromStr for EntityKind {
97    type Err = AuthzError;
98
99    fn from_str(s: &str) -> Result<Self, Self::Err> {
100        match s {
101            "gateway_route" => Ok(Self::GatewayRoute),
102            "mcp_server" => Ok(Self::McpServer),
103            "plugin" => Ok(Self::Plugin),
104            "agent" => Ok(Self::Agent),
105            "marketplace" => Ok(Self::Marketplace),
106            "skill" => Ok(Self::Skill),
107            "hook" => Ok(Self::Hook),
108            other => Err(AuthzError::Validation(format!(
109                "unknown entity_type: {other}"
110            ))),
111        }
112    }
113}
114
115impl fmt::Display for EntityKind {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        f.write_str(self.as_str())
118    }
119}