use arrow::array::{Array, ListArray};
use crate::Error;
pub trait Transform {
type Source: Array;
type Target: Array;
fn transform(&self, source: &Self::Source) -> Result<Option<Self::Target>, Error>;
fn then<T2>(self, next: T2) -> Then<Self, T2>
where
Self: Sized,
T2: Transform<Source = Self::Target>,
{
Then {
first: self,
second: next,
}
}
}
impl<T> Transform for T
where
T: Fn(&ListArray) -> Result<Option<ListArray>, Error>,
{
type Source = ListArray;
type Target = ListArray;
fn transform(&self, source: &Self::Source) -> Result<Option<Self::Target>, Error> {
(self)(source)
}
}
#[derive(Clone)]
pub struct Then<T1, T2> {
first: T1,
second: T2,
}
impl<T1, T2, M> Transform for Then<T1, T2>
where
T1: Transform<Target = M>,
T2: Transform<Source = M>,
M: Array,
{
type Source = T1::Source;
type Target = T2::Target;
fn transform(&self, source: &Self::Source) -> Result<Option<Self::Target>, Error> {
match self.first.transform(source)? {
Some(mid) => self.second.transform(&mid),
None => Ok(None),
}
}
}