1use crate::compiler::prelude::*;
2use crate::value;
3
4#[derive(Clone, Copy, Debug)]
5pub struct IsArray;
6
7impl Function for IsArray {
8 fn identifier(&self) -> &'static str {
9 "is_array"
10 }
11
12 fn usage(&self) -> &'static str {
13 "Check if the `value`'s type is an array."
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 array.",
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 array.",
36 )];
37 PARAMETERS
38 }
39
40 fn examples(&self) -> &'static [Example] {
41 &[
42 example! {
43 title: "Valid array",
44 source: "is_array([1, 2, 3])",
45 result: Ok("true"),
46 },
47 example! {
48 title: "Non-matching type",
49 source: r#"is_array("a string")"#,
50 result: Ok("false"),
51 },
52 example! {
53 title: "Boolean",
54 source: "is_array(true)",
55 result: Ok("false"),
56 },
57 example! {
58 title: "Null",
59 source: "is_array(null)",
60 result: Ok("false"),
61 },
62 ]
63 }
64
65 fn compile(
66 &self,
67 _state: &state::TypeState,
68 _ctx: &mut FunctionCompileContext,
69 arguments: ArgumentList,
70 ) -> Compiled {
71 let value = arguments.required("value");
72
73 Ok(IsArrayFn { value }.as_expr())
74 }
75}
76
77#[derive(Clone, Debug)]
78struct IsArrayFn {
79 value: Box<dyn Expression>,
80}
81
82impl FunctionExpression for IsArrayFn {
83 fn resolve(&self, ctx: &mut Context) -> Resolved {
84 self.value.resolve(ctx).map(|v| value!(v.is_array()))
85 }
86
87 fn type_def(&self, _: &state::TypeState) -> TypeDef {
88 TypeDef::boolean().infallible()
89 }
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 test_function![
97 is_array => IsArray;
98
99 array {
100 args: func_args![value: value!([1, 2, 3])],
101 want: Ok(value!(true)),
102 tdef: TypeDef::boolean().infallible(),
103 }
104
105 integer {
106 args: func_args![value: value!(1789)],
107 want: Ok(value!(false)),
108 tdef: TypeDef::boolean().infallible(),
109 }
110 ];
111}