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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.

//! Maps duration strings to millisecond values.

use std::convert::{From, TryFrom};

use regex::Regex;
use serde::de::{Deserialize, Deserializer, Error as SerdeError, Unexpected};

use crate::types::error::{AppError, AppErrorKind, AppResult};

#[cfg(test)]
mod test;

// Durations are measured in milliseconds, to play nicely with
// the rest of the FxA ecosystem
const SECOND: u64 = 1000;
const MINUTE: u64 = SECOND * 60;
const HOUR: u64 = MINUTE * 60;
const DAY: u64 = HOUR * 24;
const WEEK: u64 = DAY * 7;
const MONTH: u64 = DAY * 30;
const YEAR: u64 = DAY * 365;

lazy_static! {
    static ref DURATION_FORMAT: Regex =
        Regex::new("^(?:([0-9]+) )?(second|minute|hour|day|week|month|year)s?$").unwrap();
}

/// A duration type
/// represented in milliseconds,
/// for compatibility with
/// the rest of the FxA ecosystem.
///
/// Can be deserialized from duration strings
/// of the format `"{number} {period}"`,
/// e.g. `"1 hour"` or `"10 minutes"`.
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
pub struct Duration(pub u64);

impl<'d> Deserialize<'d> for Duration {
    /// Validate and deserialize a
    /// duration from a string
    /// of the format `"{number} {period}"`,
    /// e.g. `"1 hour"` or `"10 minutes"`.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'d>,
    {
        let value: String = Deserialize::deserialize(deserializer)?;
        Duration::try_from(value.as_str())
            .map(From::from)
            .map_err(|_| D::Error::invalid_value(Unexpected::Str(&value), &"duration"))
    }
}

impl From<Duration> for u64 {
    fn from(value: Duration) -> u64 {
        value.0
    }
}

impl<'v> TryFrom<&'v str> for Duration {
    type Error = AppError;

    fn try_from(value: &str) -> AppResult<Duration> {
        fn fail(value: &str) -> AppResult<Duration> {
            Err(AppErrorKind::InvalidDuration(value.to_string()))?
        }

        if let Some(matches) = DURATION_FORMAT.captures(value) {
            if let Ok(multiplier) = matches.get(1).map_or(Ok(1), |m| m.as_str().parse::<u64>()) {
                return match matches.get(2).map_or("", |m| m.as_str()) {
                    "second" => Ok(Duration(multiplier * SECOND)),
                    "minute" => Ok(Duration(multiplier * MINUTE)),
                    "hour" => Ok(Duration(multiplier * HOUR)),
                    "day" => Ok(Duration(multiplier * DAY)),
                    "week" => Ok(Duration(multiplier * WEEK)),
                    "month" => Ok(Duration(multiplier * MONTH)),
                    "year" => Ok(Duration(multiplier * YEAR)),
                    _ => fail(value),
                };
            }
        }

        fail(value)
    }
}