vrl/stdlib/
is_regex.rs

1use crate::compiler::prelude::*;
2use crate::value;
3
4#[derive(Clone, Copy, Debug)]
5pub struct IsRegex;
6
7impl Function for IsRegex {
8    fn identifier(&self) -> &'static str {
9        "is_regex"
10    }
11
12    fn usage(&self) -> &'static str {
13        "Check if `value`'s type is a regex."
14    }
15
16    fn category(&self) -> &'static str {
17        Category::Type.as_ref()
18    }
19
20    fn return_kind(&self) -> u16 {
21        kind::BOOLEAN
22    }
23
24    fn return_rules(&self) -> &'static [&'static str] {
25        &[
26            "Returns `true` if `value` is a regex.",
27            "Returns `false` if `value` is anything else.",
28        ]
29    }
30
31    fn parameters(&self) -> &'static [Parameter] {
32        const PARAMETERS: &[Parameter] = &[Parameter::required(
33            "value",
34            kind::ANY,
35            "The value to check if it is a regex.",
36        )];
37        PARAMETERS
38    }
39
40    fn examples(&self) -> &'static [Example] {
41        &[
42            example! {
43                title: "Valid regex",
44                source: r"is_regex(r'pattern')",
45                result: Ok("true"),
46            },
47            example! {
48                title: "Non-matching type",
49                source: r#"is_regex("a string")"#,
50                result: Ok("false"),
51            },
52            example! {
53                title: "Null value",
54                source: "is_regex(null)",
55                result: Ok("false"),
56            },
57        ]
58    }
59
60    fn compile(
61        &self,
62        _state: &state::TypeState,
63        _ctx: &mut FunctionCompileContext,
64        arguments: ArgumentList,
65    ) -> Compiled {
66        let value = arguments.required("value");
67
68        Ok(IsRegexFn { value }.as_expr())
69    }
70}
71
72#[derive(Clone, Debug)]
73struct IsRegexFn {
74    value: Box<dyn Expression>,
75}
76
77impl FunctionExpression for IsRegexFn {
78    fn resolve(&self, ctx: &mut Context) -> Resolved {
79        self.value.resolve(ctx).map(|v| value!(v.is_regex()))
80    }
81
82    fn type_def(&self, _: &state::TypeState) -> TypeDef {
83        TypeDef::boolean().infallible()
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90    use regex::Regex;
91
92    test_function![
93        is_regex => IsRegex;
94
95        bytes {
96            args: func_args![value: value!("foobar")],
97            want: Ok(value!(false)),
98            tdef: TypeDef::boolean().infallible(),
99        }
100
101        regex {
102            args: func_args![value: value!(Regex::new(r"\d+").unwrap())],
103            want: Ok(value!(true)),
104            tdef: TypeDef::boolean().infallible(),
105        }
106    ];
107}