Skip to content

Commit 2e4771c

Browse files
committed
Add Age::from_secs
1 parent f123396 commit 2e4771c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/cache/age.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ use std::time::Duration;
1818
/// #
1919
/// use http_types::Response;
2020
/// use http_types::cache::Age;
21-
/// use std::time::Duration;
2221
///
23-
/// let age = Age::new(Duration::from_secs(12));
22+
/// let age = Age::from_secs(12);
2423
///
2524
/// let mut res = Response::new(200);
2625
/// age.apply(&mut res);
2726
///
2827
/// let age = Age::from_headers(res)?.unwrap();
29-
/// assert_eq!(age, Age::new(Duration::from_secs(12)));
28+
/// assert_eq!(age, Age::from_secs(12));
3029
/// #
3130
/// # Ok(()) }
3231
/// ```
@@ -41,6 +40,17 @@ impl Age {
4140
Self { dur }
4241
}
4342

43+
/// Create a new instance of `Age` from secs.
44+
pub fn from_secs(secs: u64) -> Self {
45+
let dur = Duration::from_secs(secs);
46+
Self { dur }
47+
}
48+
49+
/// Get the duration from the header.
50+
pub fn duration(&self) -> Duration {
51+
self.dur
52+
}
53+
4454
/// Create an instance of `Age` from a `Headers` instance.
4555
///
4656
/// # Implementation note

src/cache/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
//! - [MDN: HTTP Caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching)
1010
//! - [MDN: HTTP Conditional Requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests)
1111
12-
mod cache_control;
1312
mod age;
13+
mod cache_control;
1414

15+
pub use age::Age;
1516
pub use cache_control::CacheControl;
1617
pub use cache_control::CacheDirective;
17-
pub use age::Age;

0 commit comments

Comments
 (0)