#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PrincipalClass {
System,
User,
Agent,
}
impl PrincipalClass {
#[must_use]
pub fn from_str_opt(principal: Option<&str>) -> Self {
match principal {
None => Self::System,
Some(p) if p.starts_with("agent.") || p.starts_with("agent:") => Self::Agent,
Some(_) => Self::User,
}
}
#[must_use]
pub fn as_label(self) -> &'static str {
match self {
Self::System => "system",
Self::User => "user",
Self::Agent => "agent",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn classifies_buckets() {
assert_eq!(PrincipalClass::from_str_opt(None), PrincipalClass::System);
assert_eq!(
PrincipalClass::from_str_opt(Some("alice")),
PrincipalClass::User
);
assert_eq!(
PrincipalClass::from_str_opt(Some("agent.scout")),
PrincipalClass::Agent
);
assert_eq!(
PrincipalClass::from_str_opt(Some("agent:scout")),
PrincipalClass::Agent
);
}
#[test]
fn label_is_stable() {
assert_eq!(PrincipalClass::System.as_label(), "system");
assert_eq!(PrincipalClass::User.as_label(), "user");
assert_eq!(PrincipalClass::Agent.as_label(), "agent");
}
}