diff options
Diffstat (limited to 'third_party/rust/headers/src/util/seconds.rs')
-rw-r--r-- | third_party/rust/headers/src/util/seconds.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/third_party/rust/headers/src/util/seconds.rs b/third_party/rust/headers/src/util/seconds.rs new file mode 100644 index 0000000000..a1a9194b0c --- /dev/null +++ b/third_party/rust/headers/src/util/seconds.rs @@ -0,0 +1,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) + } +} |