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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{DurationError, DurationHuman};

#[derive(Default)]
pub struct DurationHumanValidator {
    pub min: DurationHuman,
    pub default: DurationHuman,
    pub max: DurationHuman,
}

impl DurationHumanValidator {
    /// Create a new validator, with the given minimal, default and maximal durations
    ///
    /// ## Panics
    /// If any value < 1s, or if not: `minimal_nanos` <= `default_nanos` <= `maximal_nanos`
    #[must_use]
    pub const fn new(minimal_nanos: u64, default_nanos: u64, maximal_nanos: u64) -> Self {
        assert!(minimal_nanos <= default_nanos && default_nanos <= maximal_nanos);
        assert!(minimal_nanos >= DurationHuman::SEC);
        assert!(default_nanos >= DurationHuman::SEC);
        assert!(maximal_nanos >= DurationHuman::SEC);

        Self {
            min: DurationHuman::new(minimal_nanos),
            default: DurationHuman::new(default_nanos),
            max: DurationHuman::new(maximal_nanos),
        }
    }

    const fn try_new(
        minimal_nanos: u64,
        default_nanos: u64,
        maximal_nanos: u64,
    ) -> Result<Self, DurationError> {
        if minimal_nanos < DurationHuman::SEC {
            Err(DurationError::DurationValidationMinMustBeMoreThanOneSecond)
        } else if default_nanos < DurationHuman::SEC {
            Err(DurationError::DurationValidationDefaultMustBeMoreThanOneSecond)
        } else {
            Ok(Self {
                min: DurationHuman::new(minimal_nanos),
                default: DurationHuman::new(default_nanos),
                max: DurationHuman::new(maximal_nanos),
            })
        }
    }

    /// To be used as a `validate_parser` for clap
    ///
    /// ```compile_error
    ///  validate_parser = {|lifetime: &str|duration_range.parse_and_validate(lifetime)}
    /// ```
    /// # Errors
    ///
    /// Will return `Err` if duration is not within the given range
    /// permission to read it.
    pub fn parse_and_validate(&self, duration: &str) -> Result<DurationHuman, DurationError> {
        let duration_in_nanos = DurationHuman::try_from(duration)?;

        if self.contains(&duration_in_nanos) {
            Ok(duration_in_nanos)
        } else {
            Err(DurationError::DurationMustLieBetween {
                range: self.to_string(),
            })
        }
    }

    #[must_use]
    pub fn contains(&self, duration: &DurationHuman) -> bool {
        self.min <= *duration && *duration <= self.max
    }
}

impl From<&DurationHumanValidator> for (u64, u64, u64) {
    fn from(duration: &DurationHumanValidator) -> Self {
        (
            (&duration.min).into(),
            (&duration.default).into(),
            (&duration.max).into(),
        )
    }
}

impl From<&DurationHumanValidator> for (String, String, String) {
    fn from(duration: &DurationHumanValidator) -> Self {
        (
            duration.min.to_string(),
            duration.default.to_string(),
            duration.max.to_string(),
        )
    }
}

impl TryFrom<(DurationHuman, DurationHuman)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (DurationHuman, DurationHuman)) -> Result<Self, Self::Error> {
        let (minimal, maximal) = &value;
        if minimal > maximal {
            Err(DurationError::DurationValidationMinMustBeLessOrEqualMax {
                minimal: minimal.to_string(),
                maximal: maximal.to_string(),
            })
        } else {
            let (minimal_ms, maximal_ms): (u64, u64) = (minimal.into(), maximal.into());
            Self::try_new(minimal_ms, minimal_ms, maximal_ms)
        }
    }
}

impl TryFrom<(DurationHuman, DurationHuman, DurationHuman)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (DurationHuman, DurationHuman, DurationHuman)) -> Result<Self, Self::Error> {
        let (minimal, default, maximal) = &value;
        if minimal > default || maximal < default {
            Err(DurationError::DurationValidationMustBeOrdered {
                minimal: minimal.to_string(),
                default: default.to_string(),
                maximal: maximal.to_string(),
            })
        } else {
            let (minimal_ms, default_ms, maximal_ms): (u64, u64, u64) =
                (minimal.into(), default.into(), maximal.into());
            Self::try_new(minimal_ms, default_ms, maximal_ms)
        }
    }
}

impl TryFrom<(u64, u64, u64)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (u64, u64, u64)) -> Result<Self, Self::Error> {
        let min_def_max: (DurationHuman, DurationHuman, DurationHuman) = (
            (DurationHuman::from(value.0)),
            (DurationHuman::from(value.1)),
            (DurationHuman::from(value.2)),
        );
        Self::try_from(min_def_max)
    }
}

impl TryFrom<(&str, &str, &str)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (&str, &str, &str)) -> Result<Self, Self::Error> {
        let min_def_max: (DurationHuman, DurationHuman, DurationHuman) = (
            (DurationHuman::try_from(value.0)?),
            (DurationHuman::try_from(value.1)?),
            (DurationHuman::try_from(value.2)?),
        );

        Self::try_from(min_def_max)
    }
}

impl TryFrom<(u64, u64)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (u64, u64)) -> Result<Self, Self::Error> {
        let min_max: (DurationHuman, DurationHuman) = (
            (DurationHuman::from(value.0)),
            (DurationHuman::from(value.1)),
        );
        Self::try_from(min_max)
    }
}

impl TryFrom<(&str, &str)> for DurationHumanValidator {
    type Error = DurationError;

    fn try_from(value: (&str, &str)) -> Result<Self, Self::Error> {
        let min_max: (DurationHuman, DurationHuman) = (
            (DurationHuman::try_from(value.0)?),
            (DurationHuman::try_from(value.1)?),
        );

        Self::try_from(min_max)
    }
}