headers/common/age.rs
1use std::time::Duration;
2
3use crate::util::Seconds;
4
5/// `Age` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.1)
6///
7/// The "Age" header field conveys the sender's estimate of the amount of
8/// time since the response was generated or successfully validated at
9/// the origin server. Age values are calculated as specified in
10/// [Section 4.2.3](https://tools.ietf.org/html/rfc7234#section-4.2.3).
11///
12/// ## ABNF
13///
14/// ```text
15/// Age = delta-seconds
16/// ```
17///
18/// The Age field-value is a non-negative integer, representing time in
19/// seconds (see [Section 1.2.1](https://tools.ietf.org/html/rfc7234#section-1.2.1)).
20///
21/// The presence of an Age header field implies that the response was not
22/// generated or validated by the origin server for this request.
23/// However, lack of an Age header field does not imply the origin was
24/// contacted, since the response might have been received from an
25/// HTTP/1.0 cache that does not implement Age.
26///
27/// ## Example values
28///
29/// * `3600`
30///
31/// # Example
32///
33/// ```
34/// use headers::Age;
35///
36/// let len = Age::from_secs(60);
37/// ```
38#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
39pub struct Age(Seconds);
40
41derive_header! {
42 Age(_),
43 name: AGE
44}
45
46impl Age {
47 /// Creates a new `Age` header from the specified number of whole seconds.
48 pub fn from_secs(secs: u64) -> Self {
49 Self(Seconds::from_secs(secs))
50 }
51
52 /// Returns the number of seconds for this `Age` header.
53 pub fn as_secs(&self) -> u64 {
54 self.0.as_u64()
55 }
56}
57
58impl From<Duration> for Age {
59 fn from(dur: Duration) -> Self {
60 Age(Seconds::from(dur))
61 }
62}
63
64impl From<Age> for Duration {
65 fn from(age: Age) -> Self {
66 age.0.into()
67 }
68}