use std::fmt;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Actor(ActorName);
#[derive(Clone, Debug)]
enum ActorName {
System(&'static str),
Anonymous,
User(Arc<str>)
}
impl Actor {
pub const fn system(component: &'static str) -> Self {
Self(ActorName::System(component))
}
pub const fn anonymous() -> Self {
Self(ActorName::Anonymous)
}
pub fn user(user_id: impl Into<Arc<str>>) -> Self {
Self(ActorName::User(user_id.into()))
}
pub fn is_system(&self) -> bool {
matches!(self.0, ActorName::System(_))
}
pub fn is_anonymous(&self) -> bool {
matches!(self.0, ActorName::Anonymous)
}
pub fn is_user(&self) -> bool {
matches!(self.0, ActorName::User(_))
}
pub fn name(&self) -> &str {
match &self.0 {
ActorName::System(component) => component,
ActorName::Anonymous => "anonymous",
ActorName::User(user_id) => user_id.as_ref(),
}
}
pub fn audit_name(&self) -> String {
match self.0 {
ActorName::System(ref component) => component.to_string(),
ActorName::Anonymous => "anonymous".to_string(),
ActorName::User(ref user_id) => {
format!("user:{}", user_id.as_ref())
}
}
}
}
impl fmt::Display for Actor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.name())
}
}