logicaffeine_language/analysis/
policy.rs1use logicaffeine_base::Symbol;
8use std::collections::HashMap;
9
10#[derive(Debug, Clone)]
13pub enum PolicyCondition {
14 FieldEquals {
16 field: Symbol,
17 value: Symbol,
18 is_string_literal: bool,
20 },
21 FieldBool { field: Symbol, value: bool },
23 Predicate { subject: Symbol, predicate: Symbol },
25 ObjectFieldEquals {
28 subject: Symbol,
29 object: Symbol,
30 field: Symbol,
31 },
32 SubjectFieldEqualsObjectField {
35 subject_field: Symbol,
36 object: Symbol,
37 object_field: Symbol,
38 },
39 Or(Box<PolicyCondition>, Box<PolicyCondition>),
41 And(Box<PolicyCondition>, Box<PolicyCondition>),
43}
44
45#[derive(Debug, Clone)]
47pub struct PredicateDef {
48 pub subject_type: Symbol,
50 pub predicate_name: Symbol,
52 pub condition: PolicyCondition,
54}
55
56#[derive(Debug, Clone)]
58pub struct CapabilityDef {
59 pub subject_type: Symbol,
61 pub action: Symbol,
63 pub object_type: Symbol,
65 pub condition: PolicyCondition,
67}
68
69#[derive(Debug, Default, Clone)]
71pub struct PolicyRegistry {
72 predicates: HashMap<Symbol, Vec<PredicateDef>>,
74 capabilities: HashMap<Symbol, Vec<CapabilityDef>>,
76}
77
78impl PolicyRegistry {
79 pub fn new() -> Self {
80 Self::default()
81 }
82
83 pub fn register_predicate(&mut self, def: PredicateDef) {
85 self.predicates
86 .entry(def.subject_type)
87 .or_insert_with(Vec::new)
88 .push(def);
89 }
90
91 pub fn register_capability(&mut self, def: CapabilityDef) {
93 self.capabilities
94 .entry(def.subject_type)
95 .or_insert_with(Vec::new)
96 .push(def);
97 }
98
99 pub fn get_predicates(&self, subject_type: Symbol) -> Option<&[PredicateDef]> {
101 self.predicates.get(&subject_type).map(|v| v.as_slice())
102 }
103
104 pub fn get_capabilities(&self, subject_type: Symbol) -> Option<&[CapabilityDef]> {
106 self.capabilities.get(&subject_type).map(|v| v.as_slice())
107 }
108
109 pub fn has_predicates(&self, subject_type: Symbol) -> bool {
111 self.predicates.contains_key(&subject_type)
112 }
113
114 pub fn has_capabilities(&self, subject_type: Symbol) -> bool {
116 self.capabilities.contains_key(&subject_type)
117 }
118
119 pub fn iter_predicates(&self) -> impl Iterator<Item = (&Symbol, &Vec<PredicateDef>)> {
121 self.predicates.iter()
122 }
123
124 pub fn iter_capabilities(&self) -> impl Iterator<Item = (&Symbol, &Vec<CapabilityDef>)> {
126 self.capabilities.iter()
127 }
128
129 pub fn is_empty(&self) -> bool {
131 self.predicates.is_empty() && self.capabilities.is_empty()
132 }
133}
134
135#[cfg(test)]
136#[path = "../../../logicaffeine_tests/unit/logicaffeine_language/analysis/policy/tests.rs"]
137mod tests;