Skip to content

Commit

Permalink
Refactor All-function using generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
mitghi committed Mar 18, 2023
1 parent 712ec15 commit 6747f10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
28 changes: 0 additions & 28 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,34 +851,6 @@ impl<'a> Context<'a> {
}
},

(Filter::All, Some(tail)) => match *current.value {
Value::Object(ref _obj) => {}
Value::Bool(ref value) => {
let all = self.reduce_stack_to_all_truth() & (*value == true);
push_to_stack_or_produce!(
self.results,
self.stack,
tail,
Value::Bool(all)
);
}
Value::Array(ref array) => {
let mut all = self.reduce_stack_to_all_truth();
for value in array {
if value.is_boolean() {
all = all & (value.as_bool().unwrap() == true);
}
}
push_to_stack_or_produce!(
self.results,
self.stack,
tail,
Value::Bool(all)
);
}
_ => {}
},

(Filter::ArrayIndex(ref index), Some(tail)) => match *current.value {
Value::Array(ref array) => {
if *index < array.len() {
Expand Down
30 changes: 30 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,35 @@ impl Callable for Tail {
}
}

struct AllOnBoolean;
impl Callable for AllOnBoolean {
fn call(
&mut self,
_func: &Func,
value: &Value,
ctx: Option<&mut Context<'_>>,
) -> Result<Value, Error> {
match &value {
Value::Bool(ref value) => {
let all = ctx.unwrap().reduce_stack_to_all_truth();
return Ok(Value::Bool(all & (*value == true)));
}
Value::Array(ref array) => {
let mut all = ctx.unwrap().reduce_stack_to_all_truth();
for value in array {
if value.is_boolean() {
all = all & (value.as_bool().unwrap() == true);
}
}
return Ok(Value::Bool(all));
}
_ => {
return Err(Error::FuncEval("input is not reducable".to_owned()));
}
}
}
}

impl Default for FuncRegistry {
fn default() -> Self {
let mut output = FuncRegistry::new();
Expand All @@ -243,6 +272,7 @@ impl Default for FuncRegistry {
output.register("len", Box::new(LenFn));
output.register("head", Box::new(Head));
output.register("tail", Box::new(Tail));
output.register("all", Box::new(AllOnBoolean));
output
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ pick = {
((",") ~ (" ")* ~ (literal_keyed | (sub_expression_keyed | sub_expression_keyed_reversed) ))* ~
(" ")* ~ ")"
}
sub_expression = { path ~ (allFn | pickFn | filterFn | child | any_child | grouped_any | descendant_child | array_index | slice | array_to | array_from | fn)* }
sub_expression_reversed = { reverse_path ~ (allFn | pickFn | filterFn | grouped_any | child | any_child | descendant_child | array_index | slice | array_to | array_from | fn )* }
sub_expression = { path ~ (pickFn | filterFn | child | any_child | grouped_any | descendant_child | array_index | slice | array_to | array_from | fn)* }
sub_expression_reversed = { reverse_path ~ (pickFn | filterFn | grouped_any | child | any_child | descendant_child | array_index | slice | array_to | array_from | fn )* }
sub_expression_keyed = { sub_expression ~ as? }
sub_expression_keyed_reversed = { sub_expression_reversed ~ as? }

all = { sharp ~ "all" }
allFn = { slash ~ all }
pickFn = { slash ~ pick }
fnLit = { literal }
fnExpr = { sub_expression }
Expand All @@ -75,6 +72,6 @@ filterFn = { slash ~ filter }

expression = {
(path|reverse_path) ~
(allFn | pickFn | filterFn | grouped_any | child | any_child | descendant_child | array_index | slice | array_to | array_from | fn)* ~
(pickFn | filterFn | grouped_any | child | any_child | descendant_child | array_index | slice | array_to | array_from | fn)* ~
EOI
}
1 change: 0 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ pub(crate) fn parse<'a>(input: &'a str) -> Result<Vec<Filter>, pest::error::Erro
}
}
Rule::path | Rule::reverse_path => actions.push(Filter::Root),
Rule::allFn => actions.push(Filter::All),
Rule::grouped_any => {
let elem = token.into_inner().nth(1).unwrap().into_inner();
let mut values: Vec<String> = vec![];
Expand Down

0 comments on commit 6747f10

Please sign in to comment.