arithmetic_eval/env/
variable_map.rs

1//! Standard collections of variables.
2
3use core::{cmp::Ordering, fmt};
4
5use crate::{fns, Object, Value};
6
7/// Commonly used constants and functions from the [`fns` module](fns).
8///
9/// # Contents
10///
11/// - `true` and `false` Boolean constants.
12/// - Deferred initialization: [`defer`](fns::Defer).
13/// - Control flow functions: [`if`](fns::If), [`while`](fns::While).
14/// - Array functions: [`all`](fns::All), [`any`](fns::Any), [`filter`](fns::Filter), [`fold`](fns::Fold),
15///   [`map`](fns::Map), [`merge`](fns::Merge), [`push`](fns::Push). Available both as free functions
16///   and as fields in an `Array` object.
17#[derive(Debug, Clone, Copy, Default)]
18pub struct Prelude;
19
20impl Prelude {
21    /// Creates an iterator over contained values and the corresponding names.
22    pub fn iter<T>() -> impl Iterator<Item = (&'static str, Value<T>)>
23    where
24        T: 'static + Clone,
25    {
26        let array_object: Object<T> = Self::array_functions::<T>().collect();
27        [
28            ("false", Value::Bool(false)),
29            ("true", Value::Bool(true)),
30            ("defer", Value::native_fn(fns::Defer)),
31            ("if", Value::native_fn(fns::If)),
32            ("while", Value::native_fn(fns::While)),
33        ]
34        .into_iter()
35        .chain(Self::array_functions::<T>())
36        .chain([("Array", array_object.into())])
37    }
38
39    fn array_functions<T>() -> impl Iterator<Item = (&'static str, Value<T>)>
40    where
41        T: 'static + Clone,
42    {
43        [
44            ("all", Value::native_fn(fns::All)),
45            ("any", Value::native_fn(fns::Any)),
46            ("filter", Value::native_fn(fns::Filter)),
47            ("fold", Value::native_fn(fns::Fold)),
48            ("map", Value::native_fn(fns::Map)),
49            ("merge", Value::native_fn(fns::Merge)),
50            ("push", Value::native_fn(fns::Push)),
51        ]
52        .into_iter()
53    }
54}
55
56/// Container for assertion functions: `assert`, `assert_eq` and `assert_fails`.
57#[derive(Debug, Clone, Copy, Default)]
58pub struct Assertions;
59
60impl Assertions {
61    /// Creates an iterator over contained values and the corresponding names.
62    pub fn iter<T>() -> impl Iterator<Item = (&'static str, Value<T>)>
63    where
64        T: 'static + Clone + fmt::Display,
65    {
66        [
67            ("assert", Value::native_fn(fns::Assert)),
68            ("assert_eq", Value::native_fn(fns::AssertEq)),
69            (
70                "assert_fails",
71                Value::native_fn(fns::AssertFails::default()),
72            ),
73        ]
74        .into_iter()
75    }
76}
77
78/// Container with the comparison functions: `cmp`, `min` and `max`.
79#[derive(Debug, Clone, Copy, Default)]
80pub struct Comparisons;
81
82impl Comparisons {
83    /// Creates an iterator over contained values and the corresponding names.
84    pub fn iter<T>() -> impl Iterator<Item = (&'static str, Value<T>)> {
85        [
86            ("LESS", Value::opaque_ref(Ordering::Less)),
87            ("EQUAL", Value::opaque_ref(Ordering::Equal)),
88            ("GREATER", Value::opaque_ref(Ordering::Greater)),
89            ("cmp", Value::native_fn(fns::Compare::Raw)),
90            ("min", Value::native_fn(fns::Compare::Min)),
91            ("max", Value::native_fn(fns::Compare::Max)),
92        ]
93        .into_iter()
94    }
95}