2023-10-19 17:26:52 +00:00
|
|
|
use crate::SharedString;
|
2023-10-19 17:03:10 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use collections::{HashMap, HashSet};
|
2023-10-19 17:26:52 +00:00
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
pub trait Action: Any + Send + Sync {
|
|
|
|
fn partial_eq(&self, action: &dyn Action) -> bool;
|
|
|
|
fn boxed_clone(&self) -> Box<dyn Action>;
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
}
|
2023-10-19 17:03:10 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2023-10-19 17:26:52 +00:00
|
|
|
pub struct ActionContext {
|
|
|
|
set: HashSet<SharedString>,
|
|
|
|
map: HashMap<SharedString, SharedString>,
|
2023-10-19 17:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 17:26:52 +00:00
|
|
|
impl ActionContext {
|
2023-10-19 17:03:10 +00:00
|
|
|
pub fn new() -> Self {
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContext {
|
2023-10-19 17:03:10 +00:00
|
|
|
set: HashSet::default(),
|
|
|
|
map: HashMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.set.clear();
|
|
|
|
self.map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extend(&mut self, other: &Self) {
|
|
|
|
for v in &other.set {
|
|
|
|
self.set.insert(v.clone());
|
|
|
|
}
|
|
|
|
for (k, v) in &other.map {
|
|
|
|
self.map.insert(k.clone(), v.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:26:52 +00:00
|
|
|
pub fn add_identifier<I: Into<SharedString>>(&mut self, identifier: I) {
|
2023-10-19 17:03:10 +00:00
|
|
|
self.set.insert(identifier.into());
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:26:52 +00:00
|
|
|
pub fn add_key<S1: Into<SharedString>, S2: Into<SharedString>>(&mut self, key: S1, value: S2) {
|
2023-10-19 17:03:10 +00:00
|
|
|
self.map.insert(key.into(), value.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
2023-10-19 17:26:52 +00:00
|
|
|
pub enum ActionContextPredicate {
|
|
|
|
Identifier(SharedString),
|
|
|
|
Equal(SharedString, SharedString),
|
|
|
|
NotEqual(SharedString, SharedString),
|
|
|
|
Child(Box<ActionContextPredicate>, Box<ActionContextPredicate>),
|
|
|
|
Not(Box<ActionContextPredicate>),
|
|
|
|
And(Box<ActionContextPredicate>, Box<ActionContextPredicate>),
|
|
|
|
Or(Box<ActionContextPredicate>, Box<ActionContextPredicate>),
|
2023-10-19 17:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 17:26:52 +00:00
|
|
|
impl ActionContextPredicate {
|
2023-10-19 17:03:10 +00:00
|
|
|
pub fn parse(source: &str) -> Result<Self> {
|
|
|
|
let source = Self::skip_whitespace(source);
|
|
|
|
let (predicate, rest) = Self::parse_expr(source, 0)?;
|
|
|
|
if let Some(next) = rest.chars().next() {
|
|
|
|
Err(anyhow!("unexpected character {next:?}"))
|
|
|
|
} else {
|
|
|
|
Ok(predicate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:26:52 +00:00
|
|
|
pub fn eval(&self, contexts: &[ActionContext]) -> bool {
|
2023-10-19 17:03:10 +00:00
|
|
|
let Some(context) = contexts.first() else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
match self {
|
2023-10-19 17:26:52 +00:00
|
|
|
Self::Identifier(name) => context.set.contains(&name),
|
2023-10-19 17:03:10 +00:00
|
|
|
Self::Equal(left, right) => context
|
|
|
|
.map
|
2023-10-19 17:26:52 +00:00
|
|
|
.get(&left)
|
2023-10-19 17:03:10 +00:00
|
|
|
.map(|value| value == right)
|
|
|
|
.unwrap_or(false),
|
|
|
|
Self::NotEqual(left, right) => context
|
|
|
|
.map
|
2023-10-19 17:26:52 +00:00
|
|
|
.get(&left)
|
2023-10-19 17:03:10 +00:00
|
|
|
.map(|value| value != right)
|
|
|
|
.unwrap_or(true),
|
|
|
|
Self::Not(pred) => !pred.eval(contexts),
|
|
|
|
Self::Child(parent, child) => parent.eval(&contexts[1..]) && child.eval(contexts),
|
|
|
|
Self::And(left, right) => left.eval(contexts) && right.eval(contexts),
|
|
|
|
Self::Or(left, right) => left.eval(contexts) || right.eval(contexts),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_expr(mut source: &str, min_precedence: u32) -> anyhow::Result<(Self, &str)> {
|
|
|
|
type Op =
|
2023-10-19 17:26:52 +00:00
|
|
|
fn(ActionContextPredicate, ActionContextPredicate) -> Result<ActionContextPredicate>;
|
2023-10-19 17:03:10 +00:00
|
|
|
|
|
|
|
let (mut predicate, rest) = Self::parse_primary(source)?;
|
|
|
|
source = rest;
|
|
|
|
|
|
|
|
'parse: loop {
|
|
|
|
for (operator, precedence, constructor) in [
|
|
|
|
(">", PRECEDENCE_CHILD, Self::new_child as Op),
|
|
|
|
("&&", PRECEDENCE_AND, Self::new_and as Op),
|
|
|
|
("||", PRECEDENCE_OR, Self::new_or as Op),
|
|
|
|
("==", PRECEDENCE_EQ, Self::new_eq as Op),
|
|
|
|
("!=", PRECEDENCE_EQ, Self::new_neq as Op),
|
|
|
|
] {
|
|
|
|
if source.starts_with(operator) && precedence >= min_precedence {
|
|
|
|
source = Self::skip_whitespace(&source[operator.len()..]);
|
|
|
|
let (right, rest) = Self::parse_expr(source, precedence + 1)?;
|
|
|
|
predicate = constructor(predicate, right)?;
|
|
|
|
source = rest;
|
|
|
|
continue 'parse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((predicate, source))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_primary(mut source: &str) -> anyhow::Result<(Self, &str)> {
|
|
|
|
let next = source
|
|
|
|
.chars()
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| anyhow!("unexpected eof"))?;
|
|
|
|
match next {
|
|
|
|
'(' => {
|
|
|
|
source = Self::skip_whitespace(&source[1..]);
|
|
|
|
let (predicate, rest) = Self::parse_expr(source, 0)?;
|
|
|
|
if rest.starts_with(')') {
|
|
|
|
source = Self::skip_whitespace(&rest[1..]);
|
|
|
|
Ok((predicate, source))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("expected a ')'"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'!' => {
|
|
|
|
let source = Self::skip_whitespace(&source[1..]);
|
|
|
|
let (predicate, source) = Self::parse_expr(&source, PRECEDENCE_NOT)?;
|
2023-10-19 17:26:52 +00:00
|
|
|
Ok((ActionContextPredicate::Not(Box::new(predicate)), source))
|
2023-10-19 17:03:10 +00:00
|
|
|
}
|
|
|
|
_ if next.is_alphanumeric() || next == '_' => {
|
|
|
|
let len = source
|
|
|
|
.find(|c: char| !(c.is_alphanumeric() || c == '_'))
|
|
|
|
.unwrap_or(source.len());
|
|
|
|
let (identifier, rest) = source.split_at(len);
|
|
|
|
source = Self::skip_whitespace(rest);
|
|
|
|
Ok((
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::Identifier(identifier.to_string().into()),
|
2023-10-19 17:03:10 +00:00
|
|
|
source,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
_ => Err(anyhow!("unexpected character {next:?}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn skip_whitespace(source: &str) -> &str {
|
|
|
|
let len = source
|
|
|
|
.find(|c: char| !c.is_whitespace())
|
|
|
|
.unwrap_or(source.len());
|
|
|
|
&source[len..]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_or(self, other: Self) -> Result<Self> {
|
|
|
|
Ok(Self::Or(Box::new(self), Box::new(other)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_and(self, other: Self) -> Result<Self> {
|
|
|
|
Ok(Self::And(Box::new(self), Box::new(other)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_child(self, other: Self) -> Result<Self> {
|
|
|
|
Ok(Self::Child(Box::new(self), Box::new(other)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_eq(self, other: Self) -> Result<Self> {
|
|
|
|
if let (Self::Identifier(left), Self::Identifier(right)) = (self, other) {
|
|
|
|
Ok(Self::Equal(left, right))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("operands must be identifiers"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_neq(self, other: Self) -> Result<Self> {
|
|
|
|
if let (Self::Identifier(left), Self::Identifier(right)) = (self, other) {
|
|
|
|
Ok(Self::NotEqual(left, right))
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("operands must be identifiers"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRECEDENCE_CHILD: u32 = 1;
|
|
|
|
const PRECEDENCE_OR: u32 = 2;
|
|
|
|
const PRECEDENCE_AND: u32 = 3;
|
|
|
|
const PRECEDENCE_EQ: u32 = 4;
|
|
|
|
const PRECEDENCE_NOT: u32 = 5;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-10-19 17:26:52 +00:00
|
|
|
use super::ActionContextPredicate::{self, *};
|
2023-10-19 17:03:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_identifiers() {
|
|
|
|
// Identifiers
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("abc12").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Identifier("abc12".into())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("_1a").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Identifier("_1a".into())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_negations() {
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("!abc").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Not(Box::new(Identifier("abc".into())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse(" ! ! abc").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Not(Box::new(Not(Box::new(Identifier("abc".into())))))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_equality_operators() {
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a == b").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Equal("a".into(), "b".into())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("c!=d").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
NotEqual("c".into(), "d".into())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("c == !d")
|
2023-10-19 17:03:10 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"operands must be identifiers"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_boolean_operators() {
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a || b").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Or(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(Identifier("b".into()))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a || !b && c").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Or(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Not(Box::new(Identifier("b".into())))),
|
|
|
|
Box::new(Identifier("c".into()))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a && b || c&&d").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Or(
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(Identifier("b".into()))
|
|
|
|
)),
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Identifier("c".into())),
|
|
|
|
Box::new(Identifier("d".into()))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a == b && c || d == e && f").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Or(
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Equal("a".into(), "b".into())),
|
|
|
|
Box::new(Identifier("c".into()))
|
|
|
|
)),
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Equal("d".into(), "e".into())),
|
|
|
|
Box::new(Identifier("f".into()))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a && b && c && d").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
And(
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(And(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(Identifier("b".into()))
|
|
|
|
)),
|
|
|
|
Box::new(Identifier("c".into())),
|
|
|
|
)),
|
|
|
|
Box::new(Identifier("d".into()))
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_parenthesized_expressions() {
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse("a && (b == c || d != e)").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
And(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(Or(
|
|
|
|
Box::new(Equal("b".into(), "c".into())),
|
|
|
|
Box::new(NotEqual("d".into(), "e".into())),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-10-19 17:26:52 +00:00
|
|
|
ActionContextPredicate::parse(" ( a || b ) ").unwrap(),
|
2023-10-19 17:03:10 +00:00
|
|
|
Or(
|
|
|
|
Box::new(Identifier("a".into())),
|
|
|
|
Box::new(Identifier("b".into())),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|