grit_pattern_matcher/pattern/
and.rs1use super::{
2 functions::{Evaluator, FuncEvaluation},
3 patterns::{Matcher, Pattern, PatternName},
4 predicates::Predicate,
5 State,
6};
7use crate::context::QueryContext;
8use grit_util::{error::GritResult, AnalysisLogs};
9
10#[derive(Debug, Clone)]
11pub struct And<Q: QueryContext> {
12 pub patterns: Vec<Pattern<Q>>,
13}
14
15impl<Q: QueryContext> And<Q> {
16 pub fn new(patterns: Vec<Pattern<Q>>) -> Self {
17 Self { patterns }
18 }
19}
20
21impl<Q: QueryContext> PatternName for And<Q> {
22 fn name(&self) -> &'static str {
23 "AND"
24 }
25}
26
27impl<Q: QueryContext> Matcher<Q> for And<Q> {
28 fn execute<'a>(
29 &'a self,
30 binding: &Q::ResolvedPattern<'a>,
31 state: &mut State<'a, Q>,
32 context: &'a Q::ExecContext<'a>,
33 logs: &mut AnalysisLogs,
34 ) -> GritResult<bool> {
35 for p in self.patterns.iter() {
36 if !p.execute(binding, state, context, logs)? {
37 return Ok(false);
38 };
39 }
40 Ok(true)
41 }
42}
43
44#[derive(Debug, Clone)]
45pub struct PrAnd<Q: QueryContext> {
46 pub predicates: Vec<Predicate<Q>>,
47}
48
49impl<Q: QueryContext> PrAnd<Q> {
50 pub fn new(predicates: Vec<Predicate<Q>>) -> Self {
51 Self { predicates }
52 }
53}
54
55impl<Q: QueryContext> PatternName for PrAnd<Q> {
56 fn name(&self) -> &'static str {
57 "PREDICATE_AND"
58 }
59}
60
61impl<Q: QueryContext> Evaluator<Q> for PrAnd<Q> {
62 fn execute_func<'a>(
63 &'a self,
64 state: &mut State<'a, Q>,
65 context: &'a Q::ExecContext<'a>,
66 logs: &mut AnalysisLogs,
67 ) -> GritResult<FuncEvaluation<Q>> {
68 for p in self.predicates.iter() {
69 let res = p.execute_func(state, context, logs)?;
70 match res.predicator {
71 true => {}
72 false => return Ok(res),
73 };
74 }
75 Ok(FuncEvaluation {
76 predicator: true,
77 ret_val: None,
78 })
79 }
80}