Skip to main content

rama_http_headers/common/
last_modified.rs

1use rama_core::telemetry::tracing;
2use rama_http_types::HeaderValue;
3
4use crate::util::HttpDate;
5use std::time::SystemTime;
6
7/// `Last-Modified` header, defined in
8/// [RFC7232](https://datatracker.ietf.org/doc/html/rfc7232#section-2.2)
9///
10/// The `Last-Modified` header field in a response provides a timestamp
11/// indicating the date and time at which the origin server believes the
12/// selected representation was last modified, as determined at the
13/// conclusion of handling the request.
14///
15/// # ABNF
16///
17/// ```text
18/// Expires = HTTP-date
19/// ```
20///
21/// # Example values
22///
23/// * `Sat, 29 Oct 1994 19:43:31 GMT`
24///
25/// # Example
26///
27/// ```
28/// use rama_http_headers::LastModified;
29/// use std::time::Duration;
30/// use rama_utils::time::now_system_time;
31///
32/// let modified = LastModified::from(
33///     now_system_time() - Duration::from_hours(24)
34/// );
35/// ```
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct LastModified(pub(super) HttpDate);
38
39impl crate::TypedHeader for LastModified {
40    fn name() -> &'static ::rama_http_types::header::HeaderName {
41        &::rama_http_types::header::LAST_MODIFIED
42    }
43}
44
45impl crate::HeaderDecode for LastModified {
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(LastModified)
51    }
52}
53
54impl crate::HeaderEncode for LastModified {
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 last-modified value as header: {err}");
60            }
61        }
62    }
63}
64
65impl From<SystemTime> for LastModified {
66    fn from(time: SystemTime) -> Self {
67        Self(time.into())
68    }
69}
70
71impl From<LastModified> for SystemTime {
72    fn from(date: LastModified) -> Self {
73        date.0.into()
74    }
75}