rustutils_sleep/
lib.rs

1use clap::Parser;
2use rustutils_runnable::Runnable;
3use std::error::Error;
4use std::thread::sleep;
5use std::time::Duration;
6
7/// Pause for a specified amount of time.
8///
9/// The argument is assumed to be in seconds, however a suffix may be used to
10/// indicate otherwise. Use `h` to pause for a specified amount of hours, `m`
11/// for minutes, or `d` for days.
12#[derive(Parser, Clone, Debug)]
13#[clap(author, version, about, long_about = None)]
14pub struct Sleep {
15    /// Time to sleep for
16    #[clap(parse(try_from_str = parse_duration))]
17    time: Duration,
18}
19
20pub fn parse_duration(value: &str) -> Result<Duration, humantime::DurationError> {
21    value
22        .parse()
23        .map(|seconds: u64| Duration::from_secs(seconds))
24        .or_else(|_| humantime::parse_duration(value))
25}
26
27impl Runnable for Sleep {
28    fn run(&self) -> Result<(), Box<dyn Error>> {
29        sleep(self.time);
30        Ok(())
31    }
32}