From ec4ee605f498ad3469cca7b7145a78f49e3a33ac Mon Sep 17 00:00:00 2001 From: Florian Stecker Date: Sun, 1 Jan 2023 10:51:37 +0100 Subject: [PATCH] remove unneeded function --- src/lib.rs | 61 ------------------------------------------------------ 1 file changed, 61 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d483ca4..37c52d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,67 +43,6 @@ pub struct Cli { verbose: u8, } -/// runs the command if one of the following conditions is satisfied: -/// - `new_mail` is true -/// - the command hasn't been run yet -/// - `cli.interval` is set and the last run was at least `cli.interval` minus 1 second ago -/// -/// The return value is the duration to wait until command should be run again -/// (assuming no new mail arrives in the meantime). This is `cli.interval` if -/// the command was run, and `cli.interval` minus the time elapsed since the last run -/// in case the command was not run. -/// If `cli.interval` is `None`, the return value will be `None`. -pub fn run_command_if_needed(cli: &Cli, lastrun_mutex: &Mutex>, new_mail: bool) -> Option { - // locks the mutex until the end of the function, also preventing that the command is run concurrently - let mut last = lastrun_mutex.lock().unwrap(); - let run; - - if new_mail { - run = true; - } else { - match *last { - None => { - run = true; - }, - Some(last_inner) => { - match cli.interval { - None => { - run = false; - }, - Some(int) => { - let min_duration = Duration::from_secs(int - 1); - let duration = last_inner.elapsed().unwrap_or(Duration::ZERO); - if duration > min_duration { - run = true; - } else { - run = false; - } - } - } - } - } - } - - if run { - println!("Running command ..."); - Command::new(cli.command.as_os_str()) - .output() - .expect("command execution failed"); - println!("Command finished."); - - *last = Some(SystemTime::now()); - } - - // subtract the time already elapsed since last run - return cli.interval.map(|int| { - Duration::from_secs(int).saturating_sub( - last - .expect("command should have run at least once") - .elapsed() - .unwrap_or(Duration::ZERO)) - }); -} - #[derive(Debug)] struct Status { connected: bool,