Skip to content

Commit

Permalink
Fix wrong naming in FilterAST
Browse files Browse the repository at this point in the history
Add doc for FilterAST
Update README
  • Loading branch information
mitghi committed Mar 11, 2023
1 parent 17af201 commit 0dc4a09
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct Output {
let output: Option<Output> = values.from_index(0);
```

Jetro can be used in Web Browser by compiling down to WASM. Visit [Jetro Web](https://jetro.io)
to try it online.

# example

Jetro combines access path with functions which operate on those values matched within the pipeline.
Expand Down
46 changes: 36 additions & 10 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,34 @@ pub enum FilterInner {

#[allow(dead_code)]
#[derive(Debug, PartialEq, Clone)]
// FilterAST represents multi filter in form of
// abstract syntax tree. It represents the following
// structure:
//
// Filter
// .______________|_____________.
// | | |
// Left(FilterInner) operator Right(FilterAST)
// |
// Filter(Operator)
// |
// ...
//
// The operator operates at least two filters.
// The left inner filter, evaluates single filter
// expression.
// The right filter is recursive definition of
// the same structure.
//
// In case of odd arrity of filters, the inner
// right most FilterAST contains a No-Op operator
// with its left filter set, and its right filter
// set to None.
//
// Filter with arrity one, represents the same structure
// with No-Op opeator, therefore evaluates to left expression.
pub struct FilterAST {
pub operand: FilterLogicalOp,
pub operator: FilterLogicalOp,
pub left: Option<Rc<RefCell<FilterInner>>>,
pub right: Option<Rc<RefCell<FilterAST>>>,
}
Expand Down Expand Up @@ -378,23 +404,23 @@ impl FilterInner {
impl FilterAST {
pub fn new(left: FilterInner) -> Self {
Self {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(left))),
right: None,
}
}

pub fn set_operand(&mut self, operand: FilterLogicalOp) {
self.operand = operand;
pub fn set_operator(&mut self, operator: FilterLogicalOp) {
self.operator = operator;
}

pub fn link_right(
&mut self,
statement: FilterInner,
operand: FilterLogicalOp,
operator: FilterLogicalOp,
) -> Rc<RefCell<Self>> {
let mut rhs: Self = Self::new(statement);
rhs.set_operand(operand);
rhs.set_operator(operator);

let inner = Rc::new(RefCell::new(rhs));
let output = inner.clone();
Expand All @@ -404,14 +430,14 @@ impl FilterAST {
}

pub fn eval(&self, value: &Value) -> bool {
if self.operand == FilterLogicalOp::None && self.right.is_none() {
if self.operator == FilterLogicalOp::None && self.right.is_none() {
return self.left.clone().unwrap().borrow_mut().eval(&value);
}

let lhs = self.left.clone().unwrap().borrow_mut().eval(&value);
let rhs = self.right.clone().unwrap().borrow_mut().eval(&value);

match self.operand {
match self.operator {
FilterLogicalOp::And => return lhs && rhs,
FilterLogicalOp::Or => return lhs || rhs,
_ => todo!("inconsistent state in filter comp"),
Expand All @@ -420,14 +446,14 @@ impl FilterAST {

#[allow(dead_code)]
pub fn repr(&self) -> String {
if self.operand == FilterLogicalOp::None && self.right.is_none() {
if self.operator == FilterLogicalOp::None && self.right.is_none() {
return format!("Filter({:?})", &self.left);
}
let rhs = &self.right.clone().unwrap().clone();
return format!(
"Filter({:?}) {:?} {:?}",
&self.left,
&self.operand,
&self.operator,
rhs.borrow().repr(),
);
}
Expand Down
20 changes: 10 additions & 10 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub(crate) fn parse<'a>(input: &'a str) -> Result<Vec<Filter>, pest::error::Erro
Rule::logical_cmp => {
let op = FilterLogicalOp::get(elem.as_str()).unwrap();
let node = current.clone().unwrap();
node.borrow_mut().set_operand(op.clone());
node.borrow_mut().set_operator(op.clone());
}
_ => {
todo!("implement unmatched arm");
Expand Down Expand Up @@ -485,7 +485,7 @@ mod test {
Filter::Root,
Filter::Descendant("meows".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some".to_string(),
op: FilterOp::Eq,
Expand Down Expand Up @@ -523,7 +523,7 @@ mod test {
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "is_furry".to_string(),
op: FilterOp::Eq,
Expand All @@ -543,7 +543,7 @@ mod test {
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some_value".to_string(),
op: FilterOp::Eq,
Expand All @@ -564,7 +564,7 @@ mod test {
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some_value".to_string(),
op: FilterOp::Eq,
Expand Down Expand Up @@ -603,22 +603,22 @@ mod test {
};

root = Rc::new(RefCell::new(FilterAST::new(lhs)));
root.borrow_mut().set_operand(op.clone());
root.borrow_mut().set_operator(op.clone());

current = root.clone();

let new_node = current.borrow_mut().link_right(rhs, FilterLogicalOp::None);
current = new_node;

current.borrow_mut().set_operand(FilterLogicalOp::Or);
current.borrow_mut().set_operator(FilterLogicalOp::Or);

let new_node = current
.borrow_mut()
.link_right(standalone_lhs, FilterLogicalOp::None);
current = new_node;
_ = current;

assert_eq!(root.borrow().operand, FilterLogicalOp::And);
assert_eq!(root.borrow().operator, FilterLogicalOp::And);

let lhs = &root.borrow().left.clone().unwrap().clone();
match *lhs.borrow() {
Expand Down Expand Up @@ -714,7 +714,7 @@ mod test {
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some_key".to_string(),
op: FilterOp::Almost,
Expand All @@ -735,7 +735,7 @@ mod test {
Filter::Root,
Filter::Child("foo".to_string()),
Filter::MultiFilter(Rc::new(RefCell::new(FilterAST {
operand: FilterLogicalOp::None,
operator: FilterLogicalOp::None,
left: Some(Rc::new(RefCell::new(FilterInner::Cond {
left: "some_key".to_string(),
op: FilterOp::NotEq,
Expand Down

0 comments on commit 0dc4a09

Please sign in to comment.