Skip to main content

rama_http_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 rama_http_headers::Age;
35///
36/// let len = Age::from_seconds(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    #[must_use]
48    #[inline(always)]
49    pub fn from_seconds(secs: u64) -> Self {
50        Self(Seconds::new(secs))
51    }
52
53    #[must_use]
54    #[inline(always)]
55    pub fn try_from_duration(dur: Duration) -> Option<Self> {
56        Seconds::try_from_duration(dur).map(Self)
57    }
58
59    #[must_use]
60    #[inline(always)]
61    pub fn from_duration_rounded(dur: Duration) -> Self {
62        Self(Seconds::from_duration_rounded(dur))
63    }
64
65    #[must_use]
66    pub fn as_secs(self) -> u64 {
67        self.0.into()
68    }
69}
70
71impl From<Age> for Duration {
72    fn from(age: Age) -> Self {
73        age.0.into()
74    }
75}