Skip to main content

rama_http_headers/common/
access_control_max_age.rs

1use std::time::Duration;
2
3use crate::util::Seconds;
4
5/// `Access-Control-Max-Age` header, as defined on
6/// [mdn](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Max-Age).
7///
8/// The `Access-Control-Max-Age` header indicates how long the results of a
9/// preflight request can be cached in a preflight result cache.
10///
11/// # ABNF
12///
13/// ```text
14/// Access-Control-Max-Age = \"Access-Control-Max-Age\" \":\" delta-seconds
15/// ```
16///
17/// # Example values
18///
19/// * `531`
20///
21/// # Examples
22///
23/// ```
24/// use rama_http_headers::AccessControlMaxAge;
25///
26/// let max_age = AccessControlMaxAge::from_seconds(531);
27/// ```
28#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct AccessControlMaxAge(Seconds);
30
31derive_header! {
32    AccessControlMaxAge(_),
33    name: ACCESS_CONTROL_MAX_AGE
34}
35
36impl AccessControlMaxAge {
37    #[must_use]
38    #[inline(always)]
39    pub fn from_seconds(secs: u64) -> Self {
40        Self(Seconds::new(secs))
41    }
42
43    #[must_use]
44    #[inline(always)]
45    pub fn try_from_duration(dur: Duration) -> Option<Self> {
46        Seconds::try_from_duration(dur).map(Self)
47    }
48
49    #[must_use]
50    #[inline(always)]
51    pub fn from_duration_rounded(dur: Duration) -> Self {
52        Self(Seconds::from_duration_rounded(dur))
53    }
54
55    #[must_use]
56    pub fn as_secs(self) -> u64 {
57        self.0.into()
58    }
59}
60
61impl From<Seconds> for AccessControlMaxAge {
62    fn from(value: Seconds) -> Self {
63        Self(value)
64    }
65}
66
67impl From<AccessControlMaxAge> for Duration {
68    fn from(acma: AccessControlMaxAge) -> Self {
69        acma.0.into()
70    }
71}