vrl/stdlib/
is_integer.rs

1use crate::compiler::prelude::*;
2use crate::value;
3
4#[derive(Clone, Copy, Debug)]
5pub struct IsInteger;
6
7impl Function for IsInteger {
8    fn identifier(&self) -> &'static str {
9        "is_integer"
10    }
11
12    fn usage(&self) -> &'static str {
13        "Check if the `value`'s type is an integer."
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 an integer.",
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 an integer.",
36        )];
37        PARAMETERS
38    }
39
40    fn examples(&self) -> &'static [Example] {
41        &[
42            example! {
43                title: "Valid integer",
44                source: "is_integer(1)",
45                result: Ok("true"),
46            },
47            example! {
48                title: "Non-matching type",
49                source: r#"is_integer("a string")"#,
50                result: Ok("false"),
51            },
52            example! {
53                title: "Null",
54                source: "is_integer(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(IsIntegerFn { value }.as_expr())
69    }
70}
71
72#[derive(Clone, Debug)]
73struct IsIntegerFn {
74    value: Box<dyn Expression>,
75}
76
77impl FunctionExpression for IsIntegerFn {
78    fn resolve(&self, ctx: &mut Context) -> Resolved {
79        self.value.resolve(ctx).map(|v| value!(v.is_integer()))
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
91    test_function![
92        is_integer => IsInteger;
93
94        bytes {
95            args: func_args![value: value!("foobar")],
96            want: Ok(value!(false)),
97            tdef: TypeDef::boolean().infallible(),
98        }
99
100        integer {
101            args: func_args![value: value!(1789)],
102            want: Ok(value!(true)),
103            tdef: TypeDef::boolean().infallible(),
104        }
105    ];
106}