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