diff --git a/src/common/retry_after.rs b/src/common/retry_after.rs index bcde63b6..c8a55d7c 100644 --- a/src/common/retry_after.rs +++ b/src/common/retry_after.rs @@ -1,3 +1,4 @@ +use std::convert::TryFrom; use std::time::{Duration, SystemTime}; use http::HeaderValue; @@ -47,7 +48,7 @@ impl RetryAfter { RetryAfter(After::DateTime(time.into())) } - /// Create an `RetryAfter` header with a date value. + /// Create an `RetryAfter` header with a delay value. pub fn delay(dur: Duration) -> RetryAfter { RetryAfter(After::Delay(dur.into())) } @@ -81,6 +82,40 @@ impl<'a> From<&'a After> for HeaderValue { } } +impl From for RetryAfter { + fn from(value: SystemTime) -> Self { + Self::date(value) + } +} + +impl From for RetryAfter { + fn from(value: Duration) -> Self { + Self::delay(value) + } +} + +impl TryFrom for SystemTime { + type Error = Duration; + + fn try_from(value: RetryAfter) -> Result { + match value.0 { + After::DateTime(date) => Ok(date.into()), + After::Delay(delay) => Err(delay.into()), + } + } +} + +impl TryFrom for Duration { + type Error = SystemTime; + + fn try_from(value: RetryAfter) -> Result { + match value.0 { + After::DateTime(date) => Err(date.into()), + After::Delay(delay) => Ok(delay.into()), + } + } +} + #[cfg(test)] mod tests { use std::time::Duration;