grit_pattern_matcher/pattern/
like.rs1use super::{
2 patterns::{Matcher, Pattern, PatternName},
3 State,
4};
5use crate::context::QueryContext;
6use core::fmt::Debug;
7use grit_util::error::{GritPatternError, GritResult};
8use grit_util::AnalysisLogs;
9
10#[allow(dead_code)]
11#[derive(Debug, Clone)]
12pub struct Like<Q: QueryContext> {
13 pub like: Pattern<Q>,
14 pub threshold: Pattern<Q>,
15}
16
17impl<Q: QueryContext> Like<Q> {
18 pub fn new(like: Pattern<Q>, threshold: Pattern<Q>) -> Self {
19 Self { like, threshold }
20 }
21}
22
23impl<Q: QueryContext> PatternName for Like<Q> {
24 fn name(&self) -> &'static str {
25 "LIKE"
26 }
27}
28
29impl<Q: QueryContext> Matcher<Q> for Like<Q> {
30 #[cfg(feature = "embeddings")]
31 fn execute<'a>(
32 &'a self,
33 binding: &Q::ResolvedPattern<'a>,
34 state: &mut State<'a, Q>,
35 context: &'a Q::ExecContext<'a>,
36 logs: &mut AnalysisLogs,
37 ) -> GritResult<bool> {
38 use crate::errors::debug;
39
40 let snippet = self.like.text(state, context, logs)?;
41 let code = binding.text(&state.files)?.to_string();
42 let model = embeddings::embed::EmbeddingModel::VoyageCode2;
43 let similarity =
44 model.similarity(snippet, code, context.runtime, |s| debug(logs, state, s))? as f64;
45 Ok(similarity > self.threshold.float(state, context, logs)?)
46 }
47
48 #[cfg(not(feature = "embeddings"))]
49 fn execute<'a>(
50 &'a self,
51 _binding: &Q::ResolvedPattern<'a>,
52 _state: &mut State<'a, Q>,
53 _context: &'a Q::ExecContext<'a>,
54 _logs: &mut AnalysisLogs,
55 ) -> GritResult<bool> {
56 Err(GritPatternError::new(
57 "Like only available under the embeddings feature",
58 ))
59 }
60}