cel_interpreter/
macros.rs

1#[macro_export]
2macro_rules! impl_conversions {
3    // Capture pairs separated by commas, where each pair is separated by =>
4    ($($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> Handler<($($t,)*)> for F
67            where
68                F: Fn($($t,)*) -> R + Clone,
69                $($t: for<'a, 'context> $crate::FromContext<'a, 'context>,)*
70                R: IntoResolveResult,
71            {
72                fn call(self, _ftx: &mut FunctionContext) -> ResolveResult {
73                    $(
74                        let [<arg_ $t:lower>] = $t::from_context(_ftx)?;
75                    )*
76                    self($([<arg_ $t:lower>],)*).into_resolve_result()
77                }
78            }
79
80            impl<F, $($t,)* R> Handler<(WithFunctionContext, $($t,)*)> for F
81            where
82                F: Fn(&FunctionContext, $($t,)*) -> R + Clone,
83                $($t: for<'a, 'context> $crate::FromContext<'a, 'context>,)*
84                R: IntoResolveResult,
85            {
86                fn call(self, _ftx: &mut FunctionContext) -> ResolveResult {
87                    $(
88                        let [<arg_ $t:lower>] = $t::from_context(_ftx)?;
89                    )*
90                    self(_ftx, $([<arg_ $t:lower>],)*).into_resolve_result()
91                }
92            }
93        }
94    };
95}
96
97pub(crate) use impl_conversions;
98pub(crate) use impl_handler;