dysk_cli/
timeout.rs

1use {
2    std::time::Duration,
3    std::str::FromStr,
4};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct Timeout(Option<Duration>);
8
9impl Timeout {
10    pub fn as_duration(&self) -> Option<Duration> {
11        self.0
12    }
13    fn try_read(s: &str) -> Option<Self> {
14        if s == "none" || s == "no" {
15            return Some(Self(None));
16        }
17        if let Some(n) = s.strip_suffix("ms") {
18            if let Ok(n) = n.parse::<u64>() {
19                return Some(Self(Some(Duration::from_millis(n))));
20            }
21        } else if let Some(n) = s.strip_suffix("s") {
22            if let Ok(n) = n.parse::<u64>() {
23                return Some(Self(Some(Duration::from_secs(n))));
24            }
25        }
26        None
27    }
28}
29
30impl FromStr for Timeout {
31    type Err = &'static str;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        Self::try_read(s)
35            .ok_or(r#"Invalid timeout, expected "none" or <number>[s|ms]"#)
36    }
37}
38