1use crate::compiler::prelude::*;
2
3fn array(value: Value) -> Resolved {
4 match value {
5 v @ Value::Array(_) => Ok(v),
6 v => Err(format!("expected array, got {}", v.kind()).into()),
7 }
8}
9
10#[derive(Clone, Copy, Debug)]
11pub struct Array;
12
13impl Function for Array {
14 fn identifier(&self) -> &'static str {
15 "array"
16 }
17
18 fn parameters(&self) -> &'static [Parameter] {
19 &[Parameter {
20 keyword: "value",
21 kind: kind::ANY,
22 required: true,
23 }]
24 }
25
26 fn examples(&self) -> &'static [Example] {
27 &[
28 Example {
29 title: "valid",
30 source: "array([1,2,3])",
31 result: Ok("[1,2,3]"),
32 },
33 Example {
34 title: "invalid",
35 source: "array!(true)",
36 result: Err(
37 r#"function call error for "array" at (0:12): expected array, got boolean"#,
38 ),
39 },
40 ]
41 }
42
43 fn compile(
44 &self,
45 _state: &state::TypeState,
46 _ctx: &mut FunctionCompileContext,
47 arguments: ArgumentList,
48 ) -> Compiled {
49 let value = arguments.required("value");
50
51 Ok(ArrayFn { value }.as_expr())
52 }
53}
54
55#[derive(Debug, Clone)]
56struct ArrayFn {
57 value: Box<dyn Expression>,
58}
59
60impl FunctionExpression for ArrayFn {
61 fn resolve(&self, ctx: &mut Context) -> Resolved {
62 array(self.value.resolve(ctx)?)
63 }
64
65 fn type_def(&self, state: &state::TypeState) -> TypeDef {
66 self.value
67 .type_def(state)
68 .fallible_unless(Kind::array(Collection::any()))
69 .restrict_array()
70 }
71}