Skip to main content

radiate_core/stats/expression/
traits.rs

1use crate::SelectExpr;
2use radiate_error::RadiateError;
3use radiate_utils::AnyValue;
4
5pub(crate) type ExprResult<'a> = Result<AnyValue<'a>, RadiateError>;
6
7pub trait Evaluate<T>
8where
9    T: ExprSelector,
10{
11    fn eval<'a>(&'a mut self, metrics: &T) -> ExprResult<'a>;
12}
13
14pub trait ExprSelector {
15    fn select(&self, expr: &SelectExpr) -> AnyValue<'static>;
16}
17
18impl ExprSelector for () {
19    fn select(&self, _expr: &SelectExpr) -> AnyValue<'static> {
20        AnyValue::Null
21    }
22}
23
24macro_rules! impl_select {
25    ($t:ty, $dtype:ident) => {
26        impl ExprSelector for $t {
27            fn select(&self, _expr: &SelectExpr) -> AnyValue<'static> {
28                AnyValue::$dtype(*self)
29            }
30        }
31    };
32}
33
34impl_select!(u8, UInt8);
35impl_select!(u16, UInt16);
36impl_select!(u32, UInt32);
37impl_select!(u64, UInt64);
38
39impl_select!(i8, Int8);
40impl_select!(i16, Int16);
41impl_select!(i32, Int32);
42impl_select!(i64, Int64);
43
44impl_select!(bool, Bool);
45
46impl_select!(f32, Float32);
47impl_select!(f64, Float64);