1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::fmt;
use std::str::FromStr;
use std::time::Instant;

/// Absolute time.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Time(f64);

impl Time {
    /// Convert into seconds.
    pub fn as_secs(self) -> f64 {
        self.0
    }

    /// Wrap time with a given duration.
    pub fn wrap_around(self, t: Time) -> Self {
        Time(self.0 % t.0)
    }

    /// Offset a time by a given amount of time.
    pub fn offset(self, t: Time) -> Self {
        Time(self.0 + t.0)
    }
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.0.fmt(f)
    }
}

impl From<DurationSpec> for Time {
    fn from(spec: DurationSpec) -> Self {
        Time(spec.mins as f64 * 60. + spec.secs as f64)
    }
}

/// Monotonic time.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Monotonic(Instant);

impl Monotonic {
    pub fn now() -> Self {
        Monotonic(Instant::now())
    }

    pub fn elapsed_secs(&self) -> Time {
        let dur = self.0.elapsed();
        let secs = dur.as_secs() as f64;
        let nanos = dur.subsec_nanos() as f64;

        Time(secs + nanos * 1e-9)
    }
}

/// A way to specify a duration with minutes and seconds.
///
/// The string format is the following:
///
///   - `MmSs`, if you want minutes (e.g. `3m43s`).
///   - `Ss`, if you only have seconds (e.g. `23s`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DurationSpec {
    mins: u8,
    secs: u8,
}

impl Default for DurationSpec {
    fn default() -> Self {
        DurationSpec { mins: 0, secs: 0 }
    }
}

impl FromStr for DurationSpec {
    type Err = DurationSpecError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.ends_with('s') {
            if s.ends_with('m') {
                // only minutes
                let mins = s
                    .trim_end_matches('m')
                    .parse()
                    .map_err(|_| DurationSpecError::CannotParseMinutes)?;

                Ok(DurationSpec { mins, secs: 0 })
            } else {
                Err(DurationSpecError::MissingSecondsSuffix)
            }
        } else if s.contains('m') {
            // the first argument represents minutes, so let’s take them out first
            let mut iter = s.split('m');
            let mins = iter
                .next()
                .and_then(|x| x.parse().ok())
                .ok_or(DurationSpecError::CannotParseMinutes)?;
            let secs = iter
                .next()
                .and_then(|x| x.trim_end_matches('s').parse().ok())
                .ok_or(DurationSpecError::CannotParseSeconds)?;

            Ok(DurationSpec { mins, secs })
        } else {
            // only seconds
            let secs = s
                .trim_end_matches('s')
                .parse()
                .map_err(|_| DurationSpecError::CannotParseSeconds)?;

            Ok(DurationSpec { mins: 0, secs })
        }
    }
}

/// Possible error than can occurr while parsing a `DurationSpec` from a string.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DurationSpecError {
    MissingSecondsSuffix,
    CannotParseMinutes,
    CannotParseSeconds,
}

impl fmt::Display for DurationSpecError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            DurationSpecError::MissingSecondsSuffix => f.write_str("missing the seconds suffix"),
            DurationSpecError::CannotParseMinutes => f.write_str("cannot parse minutes"),
            DurationSpecError::CannotParseSeconds => f.write_str("cannot parse seconds"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_duration_spec() {
        assert_eq!(
            "1s".parse::<DurationSpec>().unwrap(),
            DurationSpec { mins: 0, secs: 1 }
        );
        assert_eq!(
            "2m".parse::<DurationSpec>().unwrap(),
            DurationSpec { mins: 2, secs: 0 }
        );
        assert_eq!(
            "3m12s".parse::<DurationSpec>().unwrap(),
            DurationSpec { mins: 3, secs: 12 }
        );
        assert_eq!(
            "3m12".parse::<DurationSpec>(),
            Err(DurationSpecError::MissingSecondsSuffix)
        );
    }
}