Skip to main content

tower_embed_core/headers/
last_modified.rs

1use std::time::{Duration, SystemTime};
2
3/// `Last-Modified` header.
4#[derive(Clone, Copy, Debug)]
5pub struct LastModified(pub SystemTime);
6
7impl LastModified {
8    /// Creates a new [`LastModified`] from a UNIX timestamp.
9    pub fn from_unix_timestamp(seconds: u64) -> Option<Self> {
10        SystemTime::UNIX_EPOCH
11            .checked_add(Duration::from_secs(seconds))
12            .map(Self)
13    }
14}
15
16impl super::Header for LastModified {
17    fn header_name() -> http::HeaderName {
18        http::header::LAST_MODIFIED
19    }
20
21    fn decode(value: &http::HeaderValue) -> Option<Self> {
22        let value_str = value.to_str().ok()?;
23        let http_date = httpdate::parse_http_date(value_str).ok()?;
24        Some(LastModified(http_date))
25    }
26
27    fn encode(self) -> http::HeaderValue {
28        let value_string = httpdate::fmt_http_date(self.0);
29        http::HeaderValue::from_str(&value_string).unwrap()
30    }
31}