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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::context::ValueNumber;
use crate::errors::{Error, Result};
use regex::Regex;
use serde_json::value::Value;

/// The tester function type definition
pub trait Test: Sync + Send {
    /// The tester function type definition
    fn test(&self, value: Option<&Value>, args: &[Value]) -> Result<bool>;
}

impl<F> Test for F
where
    F: Fn(Option<&Value>, &[Value]) -> Result<bool> + Sync + Send,
{
    fn test(&self, value: Option<&Value>, args: &[Value]) -> Result<bool> {
        self(value, args)
    }
}

/// Check that the number of args match what was expected
pub fn number_args_allowed(tester_name: &str, max: usize, args_len: usize) -> Result<()> {
    if max == 0 && args_len > max {
        return Err(Error::msg(format!(
            "Tester `{}` was called with some args but this test doesn't take args",
            tester_name
        )));
    }

    if args_len > max {
        return Err(Error::msg(format!(
            "Tester `{}` was called with {} args, the max number is {}",
            tester_name, args_len, max
        )));
    }

    Ok(())
}

/// Called to check if the Value is defined and return an Err if not
pub fn value_defined(tester_name: &str, value: Option<&Value>) -> Result<()> {
    if value.is_none() {
        return Err(Error::msg(format!(
            "Tester `{}` was called on an undefined variable",
            tester_name
        )));
    }

    Ok(())
}

/// Helper function to extract string from an Option<Value> to remove boilerplate
/// with tester error handling
pub fn extract_string<'a>(
    tester_name: &str,
    part: &str,
    value: Option<&'a Value>,
) -> Result<&'a str> {
    match value.and_then(Value::as_str) {
        Some(s) => Ok(s),
        None => Err(Error::msg(format!(
            "Tester `{}` was called {} that isn't a string",
            tester_name, part
        ))),
    }
}

/// Returns true if `value` is defined. Otherwise, returns false.
pub fn defined(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("defined", 0, params.len())?;

    Ok(value.is_some())
}

/// Returns true if `value` is undefined. Otherwise, returns false.
pub fn undefined(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("undefined", 0, params.len())?;

    Ok(value.is_none())
}

/// Returns true if `value` is a string. Otherwise, returns false.
pub fn string(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("string", 0, params.len())?;
    value_defined("string", value)?;

    match value {
        Some(Value::String(_)) => Ok(true),
        _ => Ok(false),
    }
}

/// Returns true if `value` is a number. Otherwise, returns false.
pub fn number(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("number", 0, params.len())?;
    value_defined("number", value)?;

    match value {
        Some(Value::Number(_)) => Ok(true),
        _ => Ok(false),
    }
}

/// Returns true if `value` is an odd number. Otherwise, returns false.
pub fn odd(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("odd", 0, params.len())?;
    value_defined("odd", value)?;

    match value.and_then(|v| v.to_number().ok()) {
        Some(f) => Ok(f % 2.0 != 0.0),
        _ => Err(Error::msg("Tester `odd` was called on a variable that isn't a number")),
    }
}

/// Returns true if `value` is an even number. Otherwise, returns false.
pub fn even(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("even", 0, params.len())?;
    value_defined("even", value)?;

    let is_odd = odd(value, params)?;
    Ok(!is_odd)
}

/// Returns true if `value` is divisible by the first param. Otherwise, returns false.
pub fn divisible_by(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("divisibleby", 1, params.len())?;
    value_defined("divisibleby", value)?;

    match value.and_then(|v| v.to_number().ok()) {
        Some(val) => match params.first().and_then(|v| v.to_number().ok()) {
            Some(p) => Ok(val % p == 0.0),
            None => Err(Error::msg(
                "Tester `divisibleby` was called with a parameter that isn't a number",
            )),
        },
        None => {
            Err(Error::msg("Tester `divisibleby` was called on a variable that isn't a number"))
        }
    }
}

/// Returns true if `value` can be iterated over in Tera (ie is an array/tuple or an object).
/// Otherwise, returns false.
pub fn iterable(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("iterable", 0, params.len())?;
    value_defined("iterable", value)?;

    Ok(value.unwrap().is_array() || value.unwrap().is_object())
}

/// Returns true if the given variable is an object (ie can be iterated over key, value).
/// Otherwise, returns false.
pub fn object(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("object", 0, params.len())?;
    value_defined("object", value)?;

    Ok(value.unwrap().is_object())
}

/// Returns true if `value` starts with the given string. Otherwise, returns false.
pub fn starting_with(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("starting_with", 1, params.len())?;
    value_defined("starting_with", value)?;

    let value = extract_string("starting_with", "on a variable", value)?;
    let needle = extract_string("starting_with", "with a parameter", params.first())?;
    Ok(value.starts_with(needle))
}

/// Returns true if `value` ends with the given string. Otherwise, returns false.
pub fn ending_with(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("ending_with", 1, params.len())?;
    value_defined("ending_with", value)?;

    let value = extract_string("ending_with", "on a variable", value)?;
    let needle = extract_string("ending_with", "with a parameter", params.first())?;
    Ok(value.ends_with(needle))
}

/// Returns true if `value` contains the given argument. Otherwise, returns false.
pub fn containing(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("containing", 1, params.len())?;
    value_defined("containing", value)?;

    match value.unwrap() {
        Value::String(v) => {
            let needle = extract_string("containing", "with a parameter", params.first())?;
            Ok(v.contains(needle))
        }
        Value::Array(v) => Ok(v.contains(params.first().unwrap())),
        Value::Object(v) => {
            let needle = extract_string("containing", "with a parameter", params.first())?;
            Ok(v.contains_key(needle))
        }
        _ => Err(Error::msg("Tester `containing` can only be used on string, array or map")),
    }
}

/// Returns true if `value` is a string and matches the regex in the argument. Otherwise, returns false.
pub fn matching(value: Option<&Value>, params: &[Value]) -> Result<bool> {
    number_args_allowed("matching", 1, params.len())?;
    value_defined("matching", value)?;

    let value = extract_string("matching", "on a variable", value)?;
    let regex = extract_string("matching", "with a parameter", params.first())?;

    let regex = match Regex::new(regex) {
        Ok(regex) => regex,
        Err(err) => {
            return Err(Error::msg(format!(
                "Tester `matching`: Invalid regular expression: {}",
                err
            )));
        }
    };

    Ok(regex.is_match(value))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::{
        containing, defined, divisible_by, ending_with, iterable, matching, object, starting_with,
        string,
    };

    use serde_json::value::to_value;

    #[test]
    fn test_number_args_ok() {
        assert!(defined(None, &vec![]).is_ok())
    }

    #[test]
    fn test_too_many_args() {
        assert!(defined(None, &vec![to_value(1).unwrap()]).is_err())
    }

    #[test]
    fn test_value_defined() {
        assert!(string(None, &[]).is_err())
    }

    #[test]
    fn test_divisible_by() {
        let tests = vec![
            (1.0, 2.0, false),
            (4.0, 2.0, true),
            (4.0, 2.1, false),
            (10.0, 2.0, true),
            (10.0, 0.0, false),
        ];

        for (val, divisor, expected) in tests {
            assert_eq!(
                divisible_by(Some(&to_value(val).unwrap()), &[to_value(divisor).unwrap()],)
                    .unwrap(),
                expected
            );
        }
    }

    #[test]
    fn test_iterable() {
        assert_eq!(iterable(Some(&to_value(vec!["1"]).unwrap()), &[]).unwrap(), true);
        assert_eq!(iterable(Some(&to_value(1).unwrap()), &[]).unwrap(), false);
        assert_eq!(iterable(Some(&to_value("hello").unwrap()), &[]).unwrap(), false);
    }

    #[test]
    fn test_object() {
        let mut h = HashMap::new();
        h.insert("a", 1);
        assert_eq!(object(Some(&to_value(h).unwrap()), &[]).unwrap(), true);
        assert_eq!(object(Some(&to_value(1).unwrap()), &[]).unwrap(), false);
        assert_eq!(object(Some(&to_value("hello").unwrap()), &[]).unwrap(), false);
    }

    #[test]
    fn test_starting_with() {
        assert!(starting_with(
            Some(&to_value("helloworld").unwrap()),
            &[to_value("hello").unwrap()],
        )
        .unwrap());
        assert!(
            !starting_with(Some(&to_value("hello").unwrap()), &[to_value("hi").unwrap()],).unwrap()
        );
    }

    #[test]
    fn test_ending_with() {
        assert!(
            ending_with(Some(&to_value("helloworld").unwrap()), &[to_value("world").unwrap()],)
                .unwrap()
        );
        assert!(
            !ending_with(Some(&to_value("hello").unwrap()), &[to_value("hi").unwrap()],).unwrap()
        );
    }

    #[test]
    fn test_containing() {
        let mut map = HashMap::new();
        map.insert("hey", 1);

        let tests = vec![
            (to_value("hello world").unwrap(), to_value("hel").unwrap(), true),
            (to_value("hello world").unwrap(), to_value("hol").unwrap(), false),
            (to_value(vec![1, 2, 3]).unwrap(), to_value(3).unwrap(), true),
            (to_value(vec![1, 2, 3]).unwrap(), to_value(4).unwrap(), false),
            (to_value(map.clone()).unwrap(), to_value("hey").unwrap(), true),
            (to_value(map.clone()).unwrap(), to_value("ho").unwrap(), false),
        ];

        for (container, needle, expected) in tests {
            assert_eq!(containing(Some(&container), &[needle]).unwrap(), expected);
        }
    }

    #[test]
    fn test_matching() {
        let tests = vec![
            (to_value("abc").unwrap(), to_value("b").unwrap(), true),
            (to_value("abc").unwrap(), to_value("^b$").unwrap(), false),
            (
                to_value("Hello, World!").unwrap(),
                to_value(r"(?i)(hello\W\sworld\W)").unwrap(),
                true,
            ),
            (
                to_value("The date was 2018-06-28").unwrap(),
                to_value(r"\d{4}-\d{2}-\d{2}$").unwrap(),
                true,
            ),
        ];

        for (container, needle, expected) in tests {
            assert_eq!(matching(Some(&container), &[needle]).unwrap(), expected);
        }

        assert!(
            matching(Some(&to_value("").unwrap()), &[to_value("(Invalid regex").unwrap()]).is_err()
        );
    }
}