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
|
use std::fmt;
use std::time::Duration;
use util::IterExt;
use HeaderValue;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Seconds(Duration);
impl Seconds {
pub(crate) fn from_val(val: &HeaderValue) -> Option<Self> {
let secs = val.to_str().ok()?.parse().ok()?;
Some(Self::from_secs(secs))
}
pub(crate) fn from_secs(secs: u64) -> Self {
Self::from(Duration::from_secs(secs))
}
pub(crate) fn as_u64(&self) -> u64 {
self.0.as_secs()
}
}
impl super::TryFromValues for Seconds {
fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error>
where
I: Iterator<Item = &'i HeaderValue>,
{
values
.just_one()
.and_then(Seconds::from_val)
.ok_or_else(::Error::invalid)
}
}
impl<'a> From<&'a Seconds> for HeaderValue {
fn from(secs: &'a Seconds) -> HeaderValue {
secs.0.as_secs().into()
}
}
impl From<Duration> for Seconds {
fn from(dur: Duration) -> Seconds {
debug_assert!(dur.subsec_nanos() == 0);
Seconds(dur)
}
}
impl From<Seconds> for Duration {
fn from(secs: Seconds) -> Duration {
secs.0
}
}
impl fmt::Debug for Seconds {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}s", self.0.as_secs())
}
}
impl fmt::Display for Seconds {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0.as_secs(), f)
}
}
|