Skip to main content

rama_http_headers/common/
expires.rs

1use std::time::SystemTime;
2
3use rama_core::telemetry::tracing;
4use rama_http_types::HeaderValue;
5
6use crate::util::HttpDate;
7
8/// `Expires` header, defined in [RFC7234](https://datatracker.ietf.org/doc/html/rfc7234#section-5.3)
9///
10/// The `Expires` header field gives the date/time after which the
11/// response is considered stale.
12///
13/// The presence of an Expires field does not imply that the original
14/// resource will change or cease to exist at, before, or after that
15/// time.
16///
17/// # ABNF
18///
19/// ```text
20/// Expires = HTTP-date
21/// ```
22///
23/// # Example values
24/// * `Thu, 01 Dec 1994 16:00:00 GMT`
25///
26/// # Example
27///
28/// ```
29/// use rama_http_headers::Expires;
30/// use rama_utils::time::now_system_time;
31/// use std::time::Duration;
32///
33/// let time = now_system_time() + Duration::from_hours(24);
34/// let expires = Expires::from(time);
35/// ```
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct Expires(HttpDate);
38
39impl crate::TypedHeader for Expires {
40    fn name() -> &'static ::rama_http_types::header::HeaderName {
41        &::rama_http_types::header::EXPIRES
42    }
43}
44
45impl crate::HeaderDecode for Expires {
46    fn decode<'i, I>(values: &mut I) -> Result<Self, crate::Error>
47    where
48        I: Iterator<Item = &'i ::rama_http_types::header::HeaderValue>,
49    {
50        crate::util::TryFromValues::try_from_values(values).map(Expires)
51    }
52}
53
54impl crate::HeaderEncode for Expires {
55    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
56        match HeaderValue::try_from(&self.0) {
57            Ok(value) => values.extend(::std::iter::once(value)),
58            Err(err) => {
59                tracing::debug!("failed to encode expires value as header: {err}");
60            }
61        }
62    }
63}
64
65impl From<SystemTime> for Expires {
66    fn from(time: SystemTime) -> Self {
67        Self(time.into())
68    }
69}
70
71impl From<Expires> for SystemTime {
72    fn from(date: Expires) -> Self {
73        date.0.into()
74    }
75}