palladium_actor/
policy.rs1use crate::errors::PathParseError;
2use crate::path::ActorPath;
3use std::time::Duration;
4
5#[derive(Debug, Clone)]
12pub struct NamespacePolicy {
13 pub(crate) allowed_prefixes: Vec<ActorPath>,
14 pub(crate) denied_prefixes: Vec<ActorPath>,
15}
16
17impl NamespacePolicy {
18 pub fn new(allowed_prefixes: Vec<ActorPath>) -> Self {
19 Self {
20 allowed_prefixes,
21 denied_prefixes: Vec::new(),
22 }
23 }
24
25 pub fn default_for(namespace: &ActorPath) -> Result<Self, PathParseError> {
28 let parent = namespace.parent().unwrap_or_else(ActorPath::root);
29 Ok(Self {
30 allowed_prefixes: vec![parent, ActorPath::parse("/system")?],
31 denied_prefixes: Vec::new(),
32 })
33 }
34
35 pub fn plugin_default(plugin_name: &str) -> Result<Self, PathParseError> {
38 let plugin_prefix = ActorPath::parse(&format!("/plugins/{plugin_name}"))?;
39 Ok(Self {
40 allowed_prefixes: vec![plugin_prefix, ActorPath::parse("/system")?],
41 denied_prefixes: Vec::new(),
42 })
43 }
44
45 pub fn deny_prefix(mut self, path: ActorPath) -> Self {
47 self.denied_prefixes.push(path);
48 self
49 }
50
51 pub fn allows(&self, path: &ActorPath) -> bool {
53 if self.matches_any(&self.denied_prefixes, path) {
54 return false;
55 }
56 self.matches_any(&self.allowed_prefixes, path)
57 }
58
59 fn matches_any(&self, prefixes: &[ActorPath], target: &ActorPath) -> bool {
60 prefixes
61 .iter()
62 .any(|p| p.as_str() == target.as_str() || p.is_ancestor_of(target))
63 }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum RestartPolicy {
71 Permanent,
73 Transient,
75 Temporary,
77}
78
79#[derive(Debug, Clone)]
81pub enum ShutdownPolicy {
82 Timeout(Duration),
84 Brutal,
86}