pub enum ContextExpr {
And(Box<ContextExpr>, Box<ContextExpr>),
Or(Box<ContextExpr>, Box<ContextExpr>),
Not(Box<ContextExpr>),
HasAttribute(String),
Compare {
key: String,
op: CompareOp,
value: String,
},
True,
False,
}Expand description
Context expression for ABAC evaluation
This enum represents a boolean expression tree that can be evaluated against a context (attribute map) to determine if conditions are met.
§Design Rationale
- Recursive Structure: Allows complex nested conditions
- Type Safety: Rust’s type system prevents malformed expressions
- Deterministic: No floating point, no random operations
- Serializable: Can be stored in policy files (YAML/JSON)
§Example
extern crate alloc;
use core_policy::context_expr::{ContextExpr, CompareOp};
use alloc::collections::BTreeMap;
use alloc::string::ToString;
let expr = ContextExpr::And(
Box::new(ContextExpr::Compare {
key: "role".into(),
op: CompareOp::Equal,
value: "admin".into(),
}),
Box::new(ContextExpr::Compare {
key: "department".into(),
op: CompareOp::Equal,
value: "IT".into(),
}),
);
let mut context = BTreeMap::new();
context.insert("role".to_string(), "admin".to_string());
context.insert("department".to_string(), "IT".to_string());
assert!(expr.evaluate(&context, 0).unwrap());Variants§
And(Box<ContextExpr>, Box<ContextExpr>)
Logical AND (both operands must be true)
Or(Box<ContextExpr>, Box<ContextExpr>)
Logical OR (at least one operand must be true)
Not(Box<ContextExpr>)
Logical NOT (negates the operand)
HasAttribute(String)
Check if an attribute exists in the context
Compare
Compare an attribute value with a constant
Fields
True
Always true (useful for testing and default cases)
False
Always false
Implementations§
Source§impl ContextExpr
impl ContextExpr
Sourcepub fn evaluate(
&self,
context: &BTreeMap<String, String>,
depth: usize,
) -> Result<bool>
pub fn evaluate( &self, context: &BTreeMap<String, String>, depth: usize, ) -> Result<bool>
Evaluate this expression against a context
§Arguments
context- Attribute map to evaluate againstdepth- Current recursion depth (prevents stack overflow)
§Returns
Ok(true)- Expression evaluates to trueOk(false)- Expression evaluates to falseErr(PolicyError::ExpressionTooDeep)- Recursion limit exceeded
§Example
extern crate alloc;
use core_policy::context_expr::{ContextExpr, CompareOp};
use alloc::collections::BTreeMap;
use alloc::string::ToString;
let expr = ContextExpr::Compare {
key: "role".into(),
op: CompareOp::Equal,
value: "admin".into(),
};
let mut context = BTreeMap::new();
context.insert("role".to_string(), "admin".to_string());
assert!(expr.evaluate(&context, 0).unwrap());Sourcepub fn parse(input: &str) -> Result<Self>
pub fn parse(input: &str) -> Result<Self>
Parse a context expression from a string
§Grammar (simplified)
expr ::= or_expr
or_expr ::= and_expr (OR and_expr)*
and_expr ::= not_expr (AND not_expr)*
not_expr ::= NOT primary | primary
primary ::= HAS key | key op value | (expr) | TRUE | FALSE
op ::= == | != | < | <= | > | >=§Examples
use core_policy::context_expr::ContextExpr;
let expr = ContextExpr::parse("role == \"admin\"").unwrap();
let expr = ContextExpr::parse("role == \"admin\" AND department == \"IT\"").unwrap();
let expr = ContextExpr::parse("(role == \"admin\" OR role == \"moderator\") AND active == \"true\"").unwrap();
let expr = ContextExpr::parse("NOT (status == \"banned\")").unwrap();
let expr = ContextExpr::parse("HAS role").unwrap();§Errors
PolicyError::InvalidExpression- Syntax error in expressionPolicyError::ExpressionTooLong- Expression exceeds MAX_EXPR_LENGTH
Trait Implementations§
Source§impl Clone for ContextExpr
impl Clone for ContextExpr
Source§fn clone(&self) -> ContextExpr
fn clone(&self) -> ContextExpr
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ContextExpr
impl Debug for ContextExpr
Source§impl<'de> Deserialize<'de> for ContextExpr
impl<'de> Deserialize<'de> for ContextExpr
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl PartialEq for ContextExpr
impl PartialEq for ContextExpr
Source§impl Serialize for ContextExpr
impl Serialize for ContextExpr
impl Eq for ContextExpr
impl StructuralPartialEq for ContextExpr
Auto Trait Implementations§
impl Freeze for ContextExpr
impl RefUnwindSafe for ContextExpr
impl Send for ContextExpr
impl Sync for ContextExpr
impl Unpin for ContextExpr
impl UnwindSafe for ContextExpr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.