use crate::Result;
pub trait Transformer: Send + Sync {
type Input: Send;
type Output: Send;
fn transform(&self, input: Self::Input) -> Result<Self::Output>;
fn transform_many(&self, inputs: Vec<Self::Input>) -> Result<Vec<Self::Output>> {
inputs.into_iter().map(|i| self.transform(i)).collect()
}
}
pub struct IdentityTransformer<T> {
_phantom: std::marker::PhantomData<T>,
}
impl<T> Default for IdentityTransformer<T> {
fn default() -> Self {
Self {
_phantom: std::marker::PhantomData,
}
}
}
impl<T> IdentityTransformer<T> {
pub fn new() -> Self {
Self::default()
}
}
impl<T: Send + Sync> Transformer for IdentityTransformer<T> {
type Input = T;
type Output = T;
fn transform(&self, input: Self::Input) -> Result<Self::Output> {
Ok(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_identity_transformer() {
let transformer = IdentityTransformer::<i32>::new();
let input = vec![1, 2, 3];
let output = transformer.transform_many(input.clone()).unwrap();
assert_eq!(input, output);
}
}