grit_pattern_matcher/pattern/
boolean_constant.rs

1use super::{
2    patterns::{Matcher, PatternName},
3    resolved_pattern::ResolvedPattern,
4    state::State,
5};
6use crate::context::{ExecContext, QueryContext};
7use grit_util::{error::GritResult, AnalysisLogs};
8
9#[derive(Debug, Clone)]
10pub struct BooleanConstant {
11    pub value: bool,
12}
13
14impl BooleanConstant {
15    pub fn new(value: bool) -> Self {
16        Self { value }
17    }
18}
19
20impl PatternName for BooleanConstant {
21    fn name(&self) -> &'static str {
22        "BOOLEAN_CONSTANT"
23    }
24}
25
26impl<Q: QueryContext> Matcher<Q> for BooleanConstant {
27    fn execute<'a>(
28        &'a self,
29        binding: &Q::ResolvedPattern<'a>,
30        state: &mut State<'a, Q>,
31        context: &'a Q::ExecContext<'a>,
32        _logs: &mut AnalysisLogs,
33    ) -> GritResult<bool> {
34        binding
35            .is_truthy(state, context.language())
36            .map(|truthiness| truthiness == self.value)
37    }
38}