diff --git a/src/context.rs b/src/context.rs index 85ee401..7f9fd38 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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() { diff --git a/src/func.rs b/src/func.rs index 6e567fa..8f8e431 100644 --- a/src/func.rs +++ b/src/func.rs @@ -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 { + 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(); @@ -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 } } diff --git a/src/grammar.pest b/src/grammar.pest index ad9ad5e..beab118 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -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 } @@ -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 } diff --git a/src/parser.rs b/src/parser.rs index f53d6c5..82f6f3e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -113,7 +113,6 @@ pub(crate) fn parse<'a>(input: &'a str) -> Result, 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 = vec![];