grit_pattern_matcher/pattern/
callback_pattern.rs

1use super::{Matcher, PatternName, State};
2use crate::context::ExecContext;
3use crate::context::QueryContext;
4use grit_util::{error::GritResult, AnalysisLogs};
5use std::fmt::Debug;
6
7/// A callback pattern is used to reference against a callback function.
8/// The actual callback function is stored in the context, just the index is provided here.
9/// This is useful for adding new user-defined functions in Rust.
10pub struct CallbackPattern {
11    pub callback_index: usize,
12}
13
14impl Clone for CallbackPattern {
15    fn clone(&self) -> Self {
16        Self {
17            callback_index: self.callback_index,
18        }
19    }
20}
21
22impl CallbackPattern {
23    pub fn new(callback_index: usize) -> Self {
24        Self { callback_index }
25    }
26}
27
28impl PatternName for CallbackPattern {
29    fn name(&self) -> &'static str {
30        "CALLBACK_PATTERN"
31    }
32}
33
34impl Debug for CallbackPattern {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "CallbackPattern")
37    }
38}
39
40impl<Q: QueryContext> Matcher<Q> for CallbackPattern {
41    fn execute<'a>(
42        &'a self,
43        binding: &Q::ResolvedPattern<'a>,
44        state: &mut State<'a, Q>,
45        context: &'a Q::ExecContext<'a>,
46        logs: &mut AnalysisLogs,
47    ) -> GritResult<bool> {
48        context.call_callback(self, context, binding, state, logs)
49    }
50}