use std::{error::Error, pin::Pin, str::FromStr}; use crate::scheduler::{task_func::TaskFunc, tasks::CronTask}; use chrono::{DateTime, Utc}; use cron::Schedule; pub struct BasicTask { schedule: Schedule, func: Box, } impl BasicTask { pub fn new(func: F, cron_expr: &str) -> Result> where F: TaskFunc, { Ok(BasicTask { schedule: Schedule::from_str(cron_expr)?, func: Box::new(func), }) } pub fn to_task(self) -> Box { Box::new(self) } } impl CronTask for BasicTask { fn get_next_execution(&self) -> Option> { self.schedule.upcoming(Utc).next() } fn call(&self) -> Pin + Send + '_>> { Box::pin(async { (self.func)() }) } }