1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::compiler::prelude::*;
use std::net::Ipv4Addr;

fn is_ipv4(value: Value) -> Resolved {
    let value_str = value.try_bytes_utf8_lossy()?;
    Ok(value_str.parse::<Ipv4Addr>().is_ok().into())
}

#[derive(Clone, Copy, Debug)]
pub struct IsIpv4;

impl Function for IsIpv4 {
    fn identifier(&self) -> &'static str {
        "is_ipv4"
    }

    fn parameters(&self) -> &'static [Parameter] {
        &[Parameter {
            keyword: "value",
            kind: kind::BYTES,
            required: true,
        }]
    }

    fn examples(&self) -> &'static [Example] {
        &[
            Example {
                title: "random string",
                source: r#"is_ipv4("foobar")"#,
                result: Ok("false"),
            },
            Example {
                title: "IPv4 address",
                source: r#"is_ipv4("1.1.1.1")"#,
                result: Ok("true"),
            },
            Example {
                title: "IPv6 address",
                source: r#"is_ipv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334")"#,
                result: Ok("false"),
            },
        ]
    }

    fn compile(
        &self,
        _state: &TypeState,
        _ctx: &mut FunctionCompileContext,
        arguments: ArgumentList,
    ) -> Compiled {
        let value = arguments.required("value");

        Ok(IsIpv4Fn { value }.as_expr())
    }
}

#[derive(Clone, Debug)]
struct IsIpv4Fn {
    value: Box<dyn Expression>,
}

impl FunctionExpression for IsIpv4Fn {
    fn resolve(&self, ctx: &mut Context) -> Resolved {
        self.value.resolve(ctx).and_then(is_ipv4)
    }

    fn type_def(&self, _: &TypeState) -> TypeDef {
        TypeDef::boolean().infallible()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::value;

    test_function![
        is_ipv4 => IsIpv4;

        not_string {
            args: func_args![value: value!(42)],
            want: Err("expected string, got integer"),
            tdef: TypeDef::boolean().infallible(),
        }

        random_string {
            args: func_args![value: value!("foobar")],
            want: Ok(value!(false)),
            tdef: TypeDef::boolean().infallible(),
        }

        ipv4_address_valid {
            args: func_args![value: value!("1.1.1.1")],
            want: Ok(value!(true)),
            tdef: TypeDef::boolean().infallible(),
        }

        ipv4_address_invalid {
            args: func_args![value: value!("1.1.1.314")],
            want: Ok(value!(false)),
            tdef: TypeDef::boolean().infallible(),
        }

        ipv6_address {
            args: func_args![value: value!("2001:0db8:85a3:0000:0000:8a2e:0370:7334")],
            want: Ok(value!(false)),
            tdef: TypeDef::boolean().infallible(),
        }
    ];
}