use crate::id::AuditId;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Actor {
Record {
user_type: String,
user_id: AuditId,
},
Name(String),
}
impl Actor {
pub fn record(user_type: impl Into<String>, user_id: impl Into<AuditId>) -> Self {
Actor::Record {
user_type: user_type.into(),
user_id: user_id.into(),
}
}
pub fn name(username: impl Into<String>) -> Self {
Actor::Name(username.into())
}
pub(crate) fn into_columns(self) -> (Option<AuditId>, Option<String>, Option<String>) {
match self {
Actor::Record { user_type, user_id } => (Some(user_id), Some(user_type), None),
Actor::Name(name) => (None, None, Some(name)),
}
}
pub(crate) fn from_columns(
user_id: Option<AuditId>,
user_type: Option<String>,
username: Option<String>,
) -> Option<Actor> {
match (user_id, user_type) {
(Some(id), Some(ty)) => Some(Actor::Record {
user_type: ty,
user_id: id,
}),
_ => username.map(Actor::Name),
}
}
}
impl From<&str> for Actor {
fn from(value: &str) -> Self {
Actor::Name(value.to_owned())
}
}
impl From<String> for Actor {
fn from(value: String) -> Self {
Actor::Name(value)
}
}
impl From<&String> for Actor {
fn from(value: &String) -> Self {
Actor::Name(value.clone())
}
}