vrl/stdlib/
is_ipv6.rs

1use crate::compiler::prelude::*;
2use std::net::Ipv6Addr;
3
4fn is_ipv6(value: &Value) -> Resolved {
5    let value_str = value.try_bytes_utf8_lossy()?;
6    Ok(value_str.parse::<Ipv6Addr>().is_ok().into())
7}
8
9#[derive(Clone, Copy, Debug)]
10pub struct IsIpv6;
11
12impl Function for IsIpv6 {
13    fn identifier(&self) -> &'static str {
14        "is_ipv6"
15    }
16
17    fn usage(&self) -> &'static str {
18        "Check if the string is a valid IPv6 address or not."
19    }
20
21    fn category(&self) -> &'static str {
22        Category::Ip.as_ref()
23    }
24
25    fn return_kind(&self) -> u16 {
26        kind::BOOLEAN
27    }
28
29    fn return_rules(&self) -> &'static [&'static str] {
30        &[
31            "Returns `true` if `value` is a valid IPv6 address.",
32            "Returns `false` if `value` is anything else.",
33        ]
34    }
35
36    fn parameters(&self) -> &'static [Parameter] {
37        const PARAMETERS: &[Parameter] = &[Parameter::required(
38            "value",
39            kind::BYTES,
40            "The IP address to check",
41        )];
42        PARAMETERS
43    }
44
45    fn examples(&self) -> &'static [Example] {
46        &[
47            example! {
48                title: "Valid IPv6 address",
49                source: r#"is_ipv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")"#,
50                result: Ok("true"),
51            },
52            example! {
53                title: "Valid IPv4 address",
54                source: r#"is_ipv6("10.0.102.37")"#,
55                result: Ok("false"),
56            },
57            example! {
58                title: "Arbitrary string",
59                source: r#"is_ipv6("foobar")"#,
60                result: Ok("false"),
61            },
62        ]
63    }
64
65    fn compile(
66        &self,
67        _state: &TypeState,
68        _ctx: &mut FunctionCompileContext,
69        arguments: ArgumentList,
70    ) -> Compiled {
71        let value = arguments.required("value");
72
73        Ok(IsIpv6Fn { value }.as_expr())
74    }
75}
76
77#[derive(Clone, Debug)]
78struct IsIpv6Fn {
79    value: Box<dyn Expression>,
80}
81
82impl FunctionExpression for IsIpv6Fn {
83    fn resolve(&self, ctx: &mut Context) -> Resolved {
84        self.value.resolve(ctx).and_then(|v| is_ipv6(&v))
85    }
86
87    fn type_def(&self, _: &TypeState) -> TypeDef {
88        TypeDef::boolean().infallible()
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use crate::value;
96
97    test_function![
98        is_ipv6 => IsIpv6;
99
100        not_string {
101            args: func_args![value: value!(42)],
102            want: Err("expected string, got integer"),
103            tdef: TypeDef::boolean().infallible(),
104        }
105
106        random_string {
107            args: func_args![value: value!("foobar")],
108            want: Ok(value!(false)),
109            tdef: TypeDef::boolean().infallible(),
110        }
111
112        ipv4_address {
113            args: func_args![value: value!("1.1.1.1")],
114            want: Ok(value!(false)),
115            tdef: TypeDef::boolean().infallible(),
116        }
117
118        ipv6_address_valid {
119            args: func_args![value: value!("2001:0db8:85a3:0000:0000:8a2e:0370:7334")],
120            want: Ok(value!(true)),
121            tdef: TypeDef::boolean().infallible(),
122        }
123
124        ipv6_address_invalid {
125            args: func_args![value: value!("2001:0db8:85a3:zzzz:0000:8a2e:0370:7334")],
126            want: Ok(value!(false)),
127            tdef: TypeDef::boolean().infallible(),
128        }
129    ];
130}