Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add more conversions for RetryAfter #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/common/retry_after.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::time::{Duration, SystemTime};

use http::HeaderValue;
Expand Down Expand Up @@ -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()))
}
Expand Down Expand Up @@ -81,6 +82,40 @@ impl<'a> From<&'a After> for HeaderValue {
}
}

impl From<SystemTime> for RetryAfter {
fn from(value: SystemTime) -> Self {
Self::date(value)
}
}

impl From<Duration> for RetryAfter {
fn from(value: Duration) -> Self {
Self::delay(value)
}
}

impl TryFrom<RetryAfter> for SystemTime {
type Error = Duration;

fn try_from(value: RetryAfter) -> Result<Self, Self::Error> {
match value.0 {
After::DateTime(date) => Ok(date.into()),
After::Delay(delay) => Err(delay.into()),
}
}
}

impl TryFrom<RetryAfter> for Duration {
type Error = SystemTime;

fn try_from(value: RetryAfter) -> Result<Self, Self::Error> {
match value.0 {
After::DateTime(date) => Err(date.into()),
After::Delay(delay) => Ok(delay.into()),
}
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;
Expand Down