Skip to main content

valence_core/
actor.rs

1//! Actor identity for privacy checks and audit trails.
2
3use serde::{Deserialize, Serialize};
4
5/// Who or what is performing a Valence operation.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7pub enum Actor {
8    User { user_id: String },
9    ServiceUser { service_name: String },
10    System { operation: String },
11    Anonymous,
12}
13
14impl Actor {
15    pub fn is_user(&self) -> bool {
16        matches!(self, Actor::User { .. })
17    }
18
19    pub fn is_system(&self) -> bool {
20        matches!(self, Actor::System { .. })
21    }
22
23    pub fn is_anonymous(&self) -> bool {
24        matches!(self, Actor::Anonymous)
25    }
26
27    pub fn user_id(&self) -> Option<&str> {
28        match self {
29            Actor::User { user_id, .. } => Some(user_id),
30            _ => None,
31        }
32    }
33
34    pub fn initialize_system_context() -> Self {
35        Actor::System {
36            operation: "initialize_system_context".to_string(),
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn actor_kinds() {
47        let user = Actor::User {
48            user_id: "u1".into(),
49        };
50        assert!(user.is_user());
51        assert_eq!(user.user_id(), Some("u1"));
52
53        let system = Actor::System {
54            operation: "boot".into(),
55        };
56        assert!(system.is_system());
57
58        assert!(Actor::Anonymous.is_anonymous());
59    }
60}