grit_pattern_matcher/pattern/
float_constant.rs1use 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 FloatConstant {
11 pub value: f64,
12}
13
14impl FloatConstant {
15 pub fn new(value: f64) -> Self {
16 Self { value }
17 }
18}
19
20impl PatternName for FloatConstant {
21 fn name(&self) -> &'static str {
22 "DOUBLE_CONSTANT"
23 }
24}
25
26impl<Q: QueryContext> Matcher<Q> for FloatConstant {
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 let text = binding.text(&state.files, context.language())?;
35 let parsed_double = text.parse::<f64>()?;
36 Ok(parsed_double == self.value)
37 }
38}