1use mln_conversion::Convertible;
4
5use crate::expression::{EvalResult, EvaluationContext, EvaluationError, Expr, Node};
6use crate::parsing_context::{ParseResult, ParsingContext};
7use crate::type_::{self, Type};
8use crate::value::{Value, to_string, type_of};
9
10fn is_comparable_type(t: &Type) -> bool {
11 *t == Type::Boolean || *t == Type::String || *t == Type::Number || *t == Type::Null || *t == Type::Value
12}
13
14fn is_comparable_runtime_type(t: &Type) -> bool {
15 *t == Type::Boolean || *t == Type::String || *t == Type::Number || *t == Type::Null
16}
17
18fn is_searchable_type(t: &Type) -> bool {
19 *t == Type::String || t.is_array() || *t == Type::Null || *t == Type::Value
20}
21
22fn is_searchable_runtime_type(t: &Type) -> bool {
23 *t == Type::String || t.is_array() || *t == Type::Null
24}
25
26#[derive(Clone, PartialEq, mln_wire::Wire)]
28pub(crate) struct In {
29 needle: Expr,
30 haystack: Expr,
31}
32
33impl In {
34 pub(crate) fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
35 debug_assert!(value.is_array());
36
37 let length = value.array_length();
38 if length != 3 {
39 ctx.error(format!("Expected 2 arguments, but found {} instead.", length - 1));
40 return None;
41 }
42
43 let needle = ctx.parse(&value.array_member(1), 1, Some(Type::Value), None)?;
44 let haystack = ctx.parse(&value.array_member(2), 2, Some(Type::Value), None)?;
45
46 let needle_type = needle.get_type();
47 let haystack_type = haystack.get_type();
48
49 if !is_comparable_type(&needle_type) {
50 ctx.error(format!(
51 "Expected first argument to be of type boolean, string or number, but found {} instead.",
52 type_::to_string(&needle_type)
53 ));
54 return None;
55 }
56
57 if !is_searchable_type(&haystack_type) {
58 ctx.error(format!(
59 "Expected second argument to be of type array or string, but found {} instead.",
60 type_::to_string(&haystack_type)
61 ));
62 return None;
63 }
64 Some(Expr::new(Node::In(Box::new(In { needle, haystack }))))
65 }
66
67 pub(crate) fn eval_bool(&self, params: &EvaluationContext<'_>) -> EvalResult<bool> {
68 let evaluated_haystack = self.haystack.evaluate_ref(params)?;
69 let evaluated_needle = self.needle.evaluate_ref(params)?;
70
71 let evaluated_needle_type = type_of(&evaluated_needle);
72 if !is_comparable_runtime_type(&evaluated_needle_type) {
73 return Err(EvaluationError::new(format!(
74 "Expected first argument to be of type boolean, string or number, but found {} instead.",
75 type_::to_string(&evaluated_needle_type)
76 )));
77 }
78
79 let evaluated_haystack_type = type_of(&evaluated_haystack);
80 if !is_searchable_runtime_type(&evaluated_haystack_type) {
81 return Err(EvaluationError::new(format!(
82 "Expected second argument to be of type array or string, but found {} instead.",
83 type_::to_string(&evaluated_haystack_type)
84 )));
85 }
86
87 if evaluated_needle_type == Type::Null || evaluated_haystack_type == Type::Null {
88 return Ok(false);
89 }
90
91 match &*evaluated_haystack {
92 Value::String(haystack_string) => Ok(haystack_string.contains(to_string(&evaluated_needle).as_str())),
93 Value::Array(haystack_array) => Ok(haystack_array.contains(&evaluated_needle)),
94 _ => Ok(false),
95 }
96 }
97
98 pub(crate) fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a Expr)) {
99 visit(&self.needle);
100 visit(&self.haystack);
101 }
102}