Skip to main content

clm_rust_core/layer0/
gates.rs

1//! Layer 0: Stateless Kenotic Boolean Gate Predicates (FND X -> 2)
2//! Canonical Path: kernel/clm_rust_core/src/layer0/gates.rs
3
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7/// Primitive Boolean Gate Predicate operating over arbitrary JSON or MCard state.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(tag = "type", content = "config")]
10pub enum GatePredicate {
11    /// Always yields true.
12    AlwaysTrue,
13    /// Always yields false.
14    AlwaysFalse,
15    /// Exact match with expected JSON value.
16    Equals(Value),
17    /// Inverted match with expected JSON value.
18    NotEquals(Value),
19    /// Conjunction of child predicates (AND).
20    And(Vec<GatePredicate>),
21    /// Disjunction of child predicates (OR).
22    Or(Vec<GatePredicate>),
23    /// Negation of child predicate (NOT).
24    Not(Box<GatePredicate>),
25    /// M-of-N threshold evaluation.
26    Threshold {
27        required: usize,
28        predicates: Vec<GatePredicate>,
29    },
30}
31
32impl GatePredicate {
33    /// Evaluates the predicate against input value in a purely stateless manner.
34    pub fn evaluate(&self, input: &Value) -> bool {
35        match self {
36            Self::AlwaysTrue => true,
37            Self::AlwaysFalse => false,
38            Self::Equals(expected) => input == expected,
39            Self::NotEquals(expected) => input != expected,
40            Self::And(preds) => preds.iter().all(|p| p.evaluate(input)),
41            Self::Or(preds) => preds.iter().any(|p| p.evaluate(input)),
42            Self::Not(p) => !p.evaluate(input),
43            Self::Threshold { required, predicates } => {
44                let satisfied = predicates.iter().filter(|p| p.evaluate(input)).count();
45                satisfied >= *required
46            }
47        }
48    }
49}
50
51/// Evaluates a collection of named gates against an input state map.
52pub fn evaluate_gate_set(gates: &[(String, GatePredicate)], state: &Value) -> Vec<(String, bool)> {
53    gates
54        .iter()
55        .map(|(name, pred)| (name.clone(), pred.evaluate(state)))
56        .collect()
57}