use tokio::sync::mpsc::{Receiver, Sender};

use crate::chopper::functor::Functor;
use crate::chopper::types::Row;

pub struct FunctorDriver {
    rx: Receiver<Box<Row>>,
    tx: Sender<Box<Row>>,
    functor: Box<dyn Functor>,
}

impl FunctorDriver {
    pub fn new(
        rx: Receiver<Box<Row>>,
        tx: Sender<Box<Row>>,
        functor: Box<dyn Functor>,
    ) -> FunctorDriver {
        FunctorDriver { rx, tx, functor }
    }

    pub async fn run(&mut self) {
        while let Some(row) = self.rx.recv().await {
            self.functor.write(row, &mut self.tx).await.unwrap()
        }
    }
}
