-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathcontains_all.rs
171 lines (149 loc) · 5.37 KB
/
contains_all.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::compiler::prelude::*;
use crate::stdlib::string_utils::convert_to_string;
fn contains_all(value: Value, substrings: Value, case_sensitive: Option<Value>) -> Resolved {
let case_sensitive = match case_sensitive {
Some(v) => v.try_boolean()?,
None => true,
};
let value_string = convert_to_string(value, !case_sensitive)?;
let substring_values = substrings.try_array()?;
for substring_value in substring_values {
let substring = convert_to_string(substring_value, !case_sensitive)?;
if !value_string.contains(&substring) {
return Ok(false.into());
}
}
Ok(true.into())
}
#[derive(Clone, Copy, Debug)]
pub struct ContainsAll;
impl Function for ContainsAll {
fn identifier(&self) -> &'static str {
"contains_all"
}
fn parameters(&self) -> &'static [Parameter] {
&[
Parameter {
keyword: "value",
kind: kind::BYTES,
required: true,
},
Parameter {
keyword: "substrings",
kind: kind::ARRAY,
required: true,
},
Parameter {
keyword: "case_sensitive",
kind: kind::BOOLEAN,
required: false,
},
]
}
fn compile(
&self,
_state: &state::TypeState,
_ctx: &mut FunctionCompileContext,
arguments: ArgumentList,
) -> Compiled {
let value = arguments.required("value");
let substrings = arguments.required("substrings");
let case_sensitive = arguments.optional("case_sensitive");
Ok(ContainsAllFn {
value,
substrings,
case_sensitive,
}
.as_expr())
}
fn examples(&self) -> &'static [Example] {
&[
Example {
title: "contains_all true",
source: r#"contains_all("The Needle In The Haystack", ["Needle", "Haystack"])"#,
result: Ok("true"),
},
Example {
title: "contains_all false",
source: r#"contains_all("the NEEDLE in the haystack", ["needle", "haystack"])"#,
result: Ok("false"),
},
]
}
}
#[derive(Clone, Debug)]
struct ContainsAllFn {
value: Box<dyn Expression>,
substrings: Box<dyn Expression>,
case_sensitive: Option<Box<dyn Expression>>,
}
impl FunctionExpression for ContainsAllFn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
let value = self.value.resolve(ctx)?;
let substrings = self.substrings.resolve(ctx)?;
let case_sensitive = self
.case_sensitive
.as_ref()
.map(|expr| expr.resolve(ctx))
.transpose()?;
contains_all(value, substrings, case_sensitive)
}
fn type_def(&self, state: &TypeState) -> TypeDef {
let substring_type_def = self.substrings.type_def(state);
let collection = substring_type_def.as_array().expect("must be an array");
let bytes_collection = Collection::from_unknown(Kind::bytes());
TypeDef::boolean().maybe_fallible(bytes_collection.is_superset(collection).is_err())
}
}
#[cfg(test)]
mod tests {
use crate::value;
use super::*;
test_function![
contains_all => ContainsAll;
no {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["the", "duck"])],
want: Ok(value!(false)),
tdef: TypeDef::boolean().infallible(),
}
substring_type {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!([1, 2])],
want: Err("expected string, got integer"),
tdef: TypeDef::boolean().fallible(),
}
yes {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["The Needle", "Needle In"])],
want: Ok(value!(true)),
tdef: TypeDef::boolean().infallible(),
}
case_sensitive_yes {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["Needle", "Haystack"])],
want: Ok(value!(true)),
tdef: TypeDef::boolean().infallible(),
}
case_sensitive_no {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["needle", "haystack"])],
want: Ok(value!(false)),
tdef: TypeDef::boolean().infallible(),
}
case_insensitive_no {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["thread", "haystack"]),
case_sensitive: false],
want: Ok(value!(false)),
tdef: TypeDef::boolean().infallible(),
}
case_insensitive_yes {
args: func_args![value: value!("The Needle In The Haystack"),
substrings: value!(["needle", "haystack"]),
case_sensitive: false],
want: Ok(value!(true)),
tdef: TypeDef::boolean().infallible(),
}
];
}