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