clm_rust_core/layer0/
gates.rs1use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(tag = "type", content = "config")]
10pub enum GatePredicate {
11 AlwaysTrue,
13 AlwaysFalse,
15 Equals(Value),
17 NotEquals(Value),
19 And(Vec<GatePredicate>),
21 Or(Vec<GatePredicate>),
23 Not(Box<GatePredicate>),
25 Threshold {
27 required: usize,
28 predicates: Vec<GatePredicate>,
29 },
30}
31
32impl GatePredicate {
33 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
51pub 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}