use chrono::{Duration as ChronoDuration, prelude::*};
use cron::Schedule;
use std::{error::Error, str::FromStr, time::Duration};
pub fn get_next_wait_duration(cron_expr: &str) -> Result<Duration, Box<dyn Error>> {
let schedule = Schedule::from_str(cron_expr)?;
let now = Local::now();
let next_occurrence = schedule
.upcoming(Local)
.next()
.ok_or("Could not find the next occurrence of the cron schedule.")?;
let chrono_duration: ChronoDuration = next_occurrence.signed_duration_since(now);
let wait_duration = Duration::new(
chrono_duration.num_seconds() as u64,
chrono_duration.num_nanoseconds().unwrap_or(0) as u32,
);
Ok(wait_duration)
}