use tokio::spawn;
use tokio::sync::mpsc::{channel, Sender};
use tokio::task::JoinHandle;

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

pub async fn drive_functor(
    output_tx: Sender<Box<Row>>,
    functor: Box<dyn Functor>,
) -> (Sender<Box<Row>>, JoinHandle<()>) {
    let (functor_tx, functor_rx) = channel(256);
    let mut functor = FunctorDriver::new(functor_rx, output_tx, functor);
    (functor_tx, spawn(async move { functor.run().await }))
}
