|
| 1 | +use cubenativeutils::wrappers::serializer::{ |
| 2 | + NativeDeserialize, NativeDeserializer, NativeSerialize, |
| 3 | +}; |
| 4 | +use cubenativeutils::wrappers::{inner_types::InnerTypes, NativeString}; |
| 5 | +use cubenativeutils::wrappers::{NativeContextHolder, NativeFunction}; |
| 6 | +use cubenativeutils::wrappers::{NativeContextHolderRef, NativeObjectHandle}; |
| 7 | +use cubenativeutils::CubeError; |
| 8 | +use std::any::Any; |
| 9 | +use std::rc::Rc; |
| 10 | + |
| 11 | +pub trait FilterParamsCallback { |
| 12 | + fn call(&self, filter_params: &Vec<String>) -> Result<String, CubeError>; |
| 13 | + fn as_any(self: Rc<Self>) -> Rc<dyn Any>; |
| 14 | + fn clone_to_context( |
| 15 | + &self, |
| 16 | + context_ref: &dyn NativeContextHolderRef, |
| 17 | + ) -> Result<Rc<dyn FilterParamsCallback>, CubeError>; |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Clone)] |
| 21 | +pub struct NativeFilterParamsCallback<IT: InnerTypes> { |
| 22 | + native_object: NativeObjectHandle<IT>, |
| 23 | +} |
| 24 | + |
| 25 | +impl<IT: InnerTypes> NativeFilterParamsCallback<IT> { |
| 26 | + pub fn new(native_object: NativeObjectHandle<IT>) -> Self { |
| 27 | + Self { native_object } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<IT: InnerTypes> FilterParamsCallback for NativeFilterParamsCallback<IT> { |
| 32 | + fn call(&self, filter_params: &Vec<String>) -> Result<String, CubeError> { |
| 33 | + let func = self.native_object.to_function()?; |
| 34 | + let context = NativeContextHolder::<IT>::new(self.native_object.get_context()); |
| 35 | + let args = filter_params |
| 36 | + .iter() |
| 37 | + .map(|param| param.to_native(context.clone())) |
| 38 | + .collect::<Result<Vec<_>, _>>()?; |
| 39 | + let res = func.call(args)?; |
| 40 | + String::from_native(res).map_err(|_| { |
| 41 | + CubeError::user("Callback for FILTER_PARAMS should return string".to_string()) |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + fn as_any(self: Rc<Self>) -> Rc<dyn Any> { |
| 46 | + self |
| 47 | + } |
| 48 | + fn clone_to_context( |
| 49 | + &self, |
| 50 | + context_ref: &dyn NativeContextHolderRef, |
| 51 | + ) -> Result<Rc<dyn FilterParamsCallback>, CubeError> { |
| 52 | + Ok(Rc::new(Self { |
| 53 | + native_object: self.native_object.try_clone_to_context_ref(context_ref)?, |
| 54 | + })) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<IT: InnerTypes> NativeSerialize<IT> for NativeFilterParamsCallback<IT> { |
| 59 | + fn to_native( |
| 60 | + &self, |
| 61 | + _context: NativeContextHolder<IT>, |
| 62 | + ) -> Result<NativeObjectHandle<IT>, CubeError> { |
| 63 | + Ok(self.native_object.clone()) |
| 64 | + } |
| 65 | +} |
| 66 | +impl<IT: InnerTypes> NativeDeserialize<IT> for NativeFilterParamsCallback<IT> { |
| 67 | + fn from_native(native_object: NativeObjectHandle<IT>) -> Result<Self, CubeError> { |
| 68 | + Ok(Self::new(native_object)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<IT: InnerTypes> std::fmt::Debug for NativeFilterParamsCallback<IT> { |
| 73 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 74 | + write!(f, "NativeFilterParamsCallback") |
| 75 | + } |
| 76 | +} |
0 commit comments