use std::sync::Arc; use anyhow::Result; use tokio::sync::{ Mutex, mpsc::{Receiver, Sender}, }; use crate::{ database::Database, model::{Mail, MailQuery}, }; struct MailProcessor { rx: Receiver, tx: Sender, db: Arc>, } // TODO: Store first then forward. On complete forward remove from db, otherwise save db with the // emails to send to. impl MailProcessor { async fn run(&mut self) -> Result<()> { // TODO: Check againts self.rx.is_closed() instead to stop the program before consuming all // of the queue. Store the emails not yet processed and restore the queue upon startup. while let Some(mail) = self.rx.recv().await { // TODO: should this be replaced with a send to another actor to pipeline the work? Is // the complexity worth the speedup? match self.do_processing(mail).await { Ok(_) => todo!(), Err(_) => todo!(), } } Ok(()) } async fn do_processing(&mut self, mail: Mail) -> Result<()> { let mut db_guard = self.db.lock().await; db_guard.execute(MailQuery::Insert(&mail))?; drop(db_guard); todo!(); } } mod tests { // TODO: Think of a way to test }