tytanic_filter/eval/
func.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
172
173
174
175
176
use std::fmt::{self, Debug};
use std::sync::Arc;

use ecow::eco_vec;

use super::{Context, Error, TryFromValue, Type, Value};

/// The backing implementation for a [`Func`].
type FuncImpl<T> = Arc<dyn Fn(&Context<T>, &[Value<T>]) -> Result<Value<T>, Error>>;

/// A function value, this can be called with a set of positional arguments to
/// produce a value. This is most commonly used as a constructor for tests sets.
#[derive(Clone)]
pub struct Func<T>(FuncImpl<T>);

impl<T> Func<T> {
    /// Create a new function with the given implementation.
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(&Context<T>, &[Value<T>]) -> Result<Value<T>, Error> + 'static,
    {
        Self(Arc::new(f) as _)
    }

    /// Call the given function with the given context and arguments.
    pub fn call(&self, ctx: &Context<T>, args: &[Value<T>]) -> Result<Value<T>, Error> {
        (self.0)(ctx, args)
    }
}

impl<T> Debug for Func<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Func").field(&..).finish()
    }
}

impl<T> Func<T> {
    /// Ensure there are no args.
    pub fn expect_no_args(id: &str, _ctx: &Context<T>, args: &[Value<T>]) -> Result<(), Error> {
        if args.is_empty() {
            Ok(())
        } else {
            Err(Error::InvalidArgumentCount {
                func: id.into(),
                expected: 0,
                is_min: false,
                found: args.len(),
            })
        }
    }

    /// Extract an exact number of values from the given arguments. Validates the
    /// types of all arguments.
    pub fn expect_args_exact<V: TryFromValue<T> + Debug, const N: usize>(
        func: &str,
        _ctx: &Context<T>,
        args: &[Value<T>],
    ) -> Result<[V; N], Error>
    where
        T: Clone,
    {
        if args.len() < N {
            return Err(Error::InvalidArgumentCount {
                func: func.into(),
                expected: N,
                is_min: false,
                found: args.len(),
            });
        }

        Ok(args
            .iter()
            .take(N)
            .cloned()
            .map(V::try_from_value)
            .collect::<Result<Vec<_>, _>>()?
            .try_into()
            .expect("we checked both min and max of the args"))
    }

    /// Extract a variadic number of values with a minimum amount given arguments.
    /// Validates the types of all arguments.
    pub fn expect_args_min<V: TryFromValue<T> + Debug, const N: usize>(
        func: &str,
        _ctx: &Context<T>,
        args: &[Value<T>],
    ) -> Result<([V; N], Vec<V>), Error>
    where
        T: Clone,
    {
        if args.len() < N {
            return Err(Error::InvalidArgumentCount {
                func: func.into(),
                expected: N,
                is_min: true,
                found: args.len(),
            });
        }

        let min = args
            .iter()
            .take(N)
            .cloned()
            .map(V::try_from_value)
            .collect::<Result<Vec<_>, _>>()?
            .try_into()
            .expect("we checked both min and max of the args");

        Ok((
            min,
            args[N..]
                .iter()
                .cloned()
                .map(V::try_from_value)
                .collect::<Result<_, _>>()?,
        ))
    }
}

impl<T> TryFromValue<T> for Func<T> {
    fn try_from_value(value: Value<T>) -> Result<Self, Error> {
        Ok(match value {
            Value::Func(set) => set,
            _ => {
                return Err(Error::TypeMismatch {
                    expected: eco_vec![Type::Func],
                    found: value.as_type(),
                })
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::Num;

    const NUM: Num = Num(0);
    const VAL: Value<()> = Value::Num(NUM);

    #[test]
    fn test_expect_args_variadic_min_length() {
        let ctx = Context::new();

        assert_eq!(
            Func::expect_args_min::<Num, 0>("f", &ctx, &[]).unwrap(),
            ([], vec![]),
        );
        assert_eq!(
            Func::expect_args_min("f", &ctx, &[VAL]).unwrap(),
            ([], vec![NUM]),
        );
        assert_eq!(
            Func::expect_args_min("f", &ctx, &[VAL, VAL]).unwrap(),
            ([], vec![NUM, NUM]),
        );

        assert!(Func::expect_args_min::<Num, 1>("f", &ctx, &[]).is_err());
        assert_eq!(
            Func::expect_args_min("f", &ctx, &[VAL]).unwrap(),
            ([NUM], vec![]),
        );
        assert_eq!(
            Func::expect_args_min("f", &ctx, &[VAL, VAL]).unwrap(),
            ([NUM], vec![NUM]),
        );

        assert!(Func::expect_args_min::<Num, 2>("f", &ctx, &[]).is_err());
        assert!(Func::expect_args_min::<Num, 2>("f", &ctx, &[VAL]).is_err(),);
        assert_eq!(
            Func::expect_args_min("f", &ctx, &[VAL, VAL]).unwrap(),
            ([NUM, NUM], vec![]),
        );
    }
}