1use crate::expression::{
6 EvaluationContext, EvaluationError, EvaluationResult, Expression, ExpressionBase, Kind,
7 downcast,
8};
9use crate::parsing_context::{ParseResult, ParsingContext};
10use crate::type_::{self, Type};
11use crate::value::{Value, to_string, type_of};
12use mln_conversion::Convertible;
13
14fn is_comparable_type(t: &Type) -> bool {
15 *t == Type::Boolean || *t == Type::String || *t == Type::Number || *t == Type::Null || *t == Type::Value
16}
17
18fn is_comparable_runtime_type(t: &Type) -> bool {
19 *t == Type::Boolean || *t == Type::String || *t == Type::Number || *t == Type::Null
20}
21
22fn is_searchable_type(t: &Type) -> bool {
23 *t == Type::String || t.is_array() || *t == Type::Null || *t == Type::Value
24}
25
26fn is_searchable_runtime_type(t: &Type) -> bool {
27 *t == Type::String || t.is_array() || *t == Type::Null
28}
29
30pub struct In {
31 base: ExpressionBase,
32 needle: Box<dyn Expression>,
33 haystack: Box<dyn Expression>,
34}
35
36impl In {
37 pub fn new(needle: Box<dyn Expression>, haystack: Box<dyn Expression>) -> Self {
38 debug_assert!(is_comparable_type(needle.get_type()));
39 debug_assert!(is_searchable_type(haystack.get_type()));
40 In {
41 base: ExpressionBase::new(
42 Kind::In,
43 Type::Boolean,
44 needle.dependencies() | haystack.dependencies(),
45 ),
46 needle,
47 haystack,
48 }
49 }
50
51 pub fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
52 debug_assert!(value.is_array());
53
54 let length = value.array_length();
55 if length != 3 {
56 ctx.error(format!("Expected 2 arguments, but found {} instead.", length - 1));
57 return None;
58 }
59
60 let needle = ctx.parse(&value.array_member(1), 1, Some(Type::Value), None)?;
61 let haystack = ctx.parse(&value.array_member(2), 2, Some(Type::Value), None)?;
62
63 let needle_type = needle.get_type().clone();
64 let haystack_type = haystack.get_type().clone();
65
66 if !is_comparable_type(&needle_type) {
67 ctx.error(format!(
68 "Expected first argument to be of type boolean, string or number, but found {} instead.",
69 type_::to_string(&needle_type)
70 ));
71 return None;
72 }
73
74 if !is_searchable_type(&haystack_type) {
75 ctx.error(format!(
76 "Expected second argument to be of type array or string, but found {} instead.",
77 type_::to_string(&haystack_type)
78 ));
79 return None;
80 }
81 Some(Box::new(In::new(needle, haystack)))
82 }
83}
84
85impl Expression for In {
86 fn base(&self) -> &ExpressionBase {
87 &self.base
88 }
89
90 fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
91 let evaluated_haystack = self.haystack.evaluate(params)?;
92 let evaluated_needle = self.needle.evaluate(params)?;
93
94 let evaluated_needle_type = type_of(&evaluated_needle);
95 if !is_comparable_runtime_type(&evaluated_needle_type) {
96 return Err(EvaluationError::new(format!(
97 "Expected first argument to be of type boolean, string or number, but found {} instead.",
98 type_::to_string(&evaluated_needle_type)
99 )));
100 }
101
102 let evaluated_haystack_type = type_of(&evaluated_haystack);
103 if !is_searchable_runtime_type(&evaluated_haystack_type) {
104 return Err(EvaluationError::new(format!(
105 "Expected second argument to be of type array or string, but found {} instead.",
106 type_::to_string(&evaluated_haystack_type)
107 )));
108 }
109
110 if evaluated_needle_type == Type::Null || evaluated_haystack_type == Type::Null {
111 return Ok(Value::Bool(false));
112 }
113
114 if evaluated_haystack_type == Type::String {
115 let haystack_string = evaluated_haystack.as_str().unwrap();
116 let needle_string = to_string(&evaluated_needle);
117 Ok(Value::Bool(haystack_string.contains(needle_string.as_str())))
118 } else {
119 let haystack_array = evaluated_haystack.as_array().unwrap();
120 Ok(Value::Bool(haystack_array.contains(&evaluated_needle)))
121 }
122 }
123
124 fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a dyn Expression)) {
125 visit(&*self.needle);
126 visit(&*self.haystack);
127 }
128
129 fn equals(&self, e: &dyn Expression) -> bool {
130 if e.get_kind() == Kind::In {
131 let rhs = downcast::<In>(e).expect("In");
132 return self.needle.equals(&*rhs.needle) && self.haystack.equals(&*rhs.haystack);
133 }
134 false
135 }
136
137 fn possible_outputs(&self) -> Vec<Option<Value>> {
138 vec![Some(Value::Bool(true)), Some(Value::Bool(false))]
139 }
140
141 fn get_operator(&self) -> String {
142 "in".to_owned()
143 }
144}