vrl/stdlib/
is_timestamp.rs

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