cel_interpreter/
macros.rs1#[macro_export]
2macro_rules! impl_conversions {
3    ($($target_type:ty => $value_variant:path),* $(,)?) => {
5        $(
6            impl FromValue for $target_type {
7                fn from_value(expr: &Value) -> Result<Self, ExecutionError> {
8                    if let $value_variant(v) = expr {
9                        Ok(v.clone())
10                    } else {
11                        Err(ExecutionError::UnexpectedType {
12                            got: format!("{:?}", expr),
13                            want: stringify!($target_type).to_string(),
14                        })
15                    }
16                }
17            }
18
19            impl FromValue for Option<$target_type> {
20                fn from_value(expr: &Value) -> Result<Self, ExecutionError> {
21                    match expr {
22                        Value::Null => Ok(None),
23                        $value_variant(v) => Ok(Some(v.clone())),
24                        _ => Err(ExecutionError::UnexpectedType {
25                            got: format!("{:?}", expr),
26                            want: stringify!($target_type).to_string(),
27                        }),
28                    }
29                }
30            }
31
32            impl From<$target_type> for Value {
33                fn from(value: $target_type) -> Self {
34                    $value_variant(value)
35                }
36            }
37
38            impl $crate::magic::IntoResolveResult for $target_type {
39                fn into_resolve_result(self) -> ResolveResult {
40                    Ok($value_variant(self))
41                }
42            }
43
44            impl $crate::magic::IntoResolveResult for Result<$target_type, ExecutionError> {
45                fn into_resolve_result(self) -> ResolveResult {
46                    self.map($value_variant)
47                }
48            }
49
50            impl<'a, 'context> FromContext<'a, 'context> for $target_type {
51                fn from_context(ctx: &'a mut FunctionContext<'context>) -> Result<Self, ExecutionError>
52                where
53                    Self: Sized,
54                {
55                    arg_value_from_context(ctx).and_then(|v| FromValue::from_value(&v))
56                }
57            }
58        )*
59    }
60}
61
62#[macro_export]
63macro_rules! impl_handler {
64    ($($t:ty),*) => {
65        paste::paste! {
66            impl<F, $($t,)* R> IntoFunction<($($t,)*)> for F
67            where
68                F: Fn($($t,)*) -> R + Send + Sync + 'static,
69                $($t: for<'a, 'context> $crate::FromContext<'a, 'context>,)*
70                R: IntoResolveResult,
71            {
72                fn into_function(self) -> Function {
73                    Box::new(move |_ftx| {
74                        $(
75                            let [<arg_ $t:lower>] = $t::from_context(_ftx)?;
76                        )*
77                        self($([<arg_ $t:lower>],)*).into_resolve_result()
78                    })
79                }
80            }
81
82            impl<F, $($t,)* R> IntoFunction<(WithFunctionContext, $($t,)*)> for F
83            where
84                F: Fn(&FunctionContext, $($t,)*) -> R + Send + Sync + 'static,
85                $($t: for<'a, 'context> $crate::FromContext<'a, 'context>,)*
86                R: IntoResolveResult,
87            {
88                fn into_function(self) -> Function {
89                    Box::new(move |_ftx| {
90                        $(
91                            let [<arg_ $t:lower>] = $t::from_context(_ftx)?;
92                        )*
93                        self(_ftx, $([<arg_ $t:lower>],)*).into_resolve_result()
94                    })
95                }
96            }
97        }
98    };
99}
100
101pub(crate) use impl_conversions;
102pub(crate) use impl_handler;