-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathall.rs
More file actions
132 lines (115 loc) · 4.16 KB
/
all.rs
File metadata and controls
132 lines (115 loc) · 4.16 KB
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
use crate::{
FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext, FunctionParam,
FunctionParamError, GetType, LhsValue, ParserSettings, Type,
};
use std::iter::once;
#[inline]
fn all_impl<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
let arg = args.next().expect("expected 1 argument, got 0");
if args.next().is_some() {
panic!("expected 1 argument, got {}", 2 + args.count());
}
match arg {
Ok(LhsValue::Array(arr)) => Some(LhsValue::Bool(
arr.into_iter().all(|lhs| bool::try_from(lhs).unwrap()),
)),
Err(Type::Array(ref arr)) if arr.get_type() == Type::Bool => None,
_ => unreachable!(),
}
}
/// A function which, given an array of bool, returns true if all of the
/// arguments are true, otherwise false.
///
/// It expects one argument and will error if given an incorrect number of
/// arguments or an argument of invalid type.
#[derive(Debug, Default)]
pub struct AllFunction {}
impl FunctionDefinition for AllFunction {
fn check_param(
&self,
_: &ParserSettings,
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
next_param: &FunctionParam<'_>,
_: Option<&mut FunctionDefinitionContext>,
) -> Result<(), FunctionParamError> {
match params.len() {
0 => {
next_param.expect_arg_kind(FunctionArgKind::Field)?;
next_param.expect_val_type(once(Type::Array(Type::Bool.into()).into()))?;
}
_ => unreachable!(),
}
Ok(())
}
fn return_type(
&self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<&FunctionDefinitionContext>,
) -> Type {
Type::Bool
}
fn arg_count(&self) -> (usize, Option<usize>) {
(1, Some(0))
}
fn compile<'s>(
&'s self,
_: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
_: Option<FunctionDefinitionContext>,
) -> Box<dyn for<'a> Fn(FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> + Sync + Send + 's> {
Box::new(all_impl)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Array;
#[test]
fn test_all_fn() {
// assert that all([]) is true
let arr = LhsValue::Array(Array::new(Type::Bool));
let mut args = vec![Ok(arr)].into_iter();
assert_eq!(Some(LhsValue::from(true)), all_impl(&mut args));
// assert that all([true]) is true
let arr = LhsValue::Array(Array::from_iter([true]));
let mut args = vec![Ok(arr)].into_iter();
assert_eq!(Some(LhsValue::from(true)), all_impl(&mut args));
// assert that all([false]) is false
let arr = LhsValue::Array(Array::from_iter([false]));
let mut args = vec![Ok(arr)].into_iter();
assert_eq!(Some(LhsValue::from(false)), all_impl(&mut args));
// assert that all([false, true]) is true
let arr = LhsValue::Array(Array::from_iter([false, true]));
let mut args = vec![Ok(arr)].into_iter();
assert_eq!(Some(LhsValue::from(false)), all_impl(&mut args));
// assert that all([true, true]) is true
let arr = LhsValue::Array(Array::from_iter([true, true]));
let mut args = vec![Ok(arr)].into_iter();
assert_eq!(Some(LhsValue::from(true)), all_impl(&mut args));
}
#[test]
#[should_panic(expected = "expected 1 argument, got 0")]
fn test_all_fn_no_args() {
let mut args = vec![].into_iter();
all_impl(&mut args);
}
#[test]
#[should_panic(expected = "expected 1 argument, got 2")]
fn test_all_fn_too_many_args() {
let arr = LhsValue::Array(Array::new(Type::Bool));
let mut args = vec![Ok(arr.clone()), Ok(arr.clone())].into_iter();
all_impl(&mut args);
}
#[test]
#[should_panic]
fn test_all_fn_bad_lhs_value() {
let mut args = vec![Ok(LhsValue::from(false))].into_iter();
all_impl(&mut args);
}
#[test]
#[should_panic]
fn test_all_fn_bad_lhs_arr_value() {
let arr = LhsValue::Array(Array::from_iter(["hello"]));
let mut args = vec![Ok(arr)].into_iter();
all_impl(&mut args);
}
}