1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::fmt::{self, Display};
use std::str::FromStr;
use std::time::SystemTime;
use httpdate::HttpDate as InnerDate;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct HttpDate(InnerDate);
impl FromStr for HttpDate {
type Err = ::Error;
fn from_str(s: &str) -> ::Result<HttpDate> {
InnerDate::from_str(s)
.map(HttpDate)
.map_err(|_| ::Error::Header)
}
}
impl Display for HttpDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl From<SystemTime> for HttpDate {
fn from(sys: SystemTime) -> HttpDate {
HttpDate(sys.into())
}
}
impl From<HttpDate> for SystemTime {
fn from(date: HttpDate) -> SystemTime {
date.0.into()
}
}
#[cfg(test)]
mod tests {
use std::time::{SystemTime, Duration};
use super::HttpDate;
macro_rules! test_parse {
($function: ident, $date: expr) => {
#[test]
fn $function() {
let nov_07 = HttpDate((
SystemTime::UNIX_EPOCH + Duration::new(784198117, 0)
).into());
assert_eq!($date.parse::<HttpDate>().unwrap(), nov_07);
}
};
}
test_parse!(test_imf_fixdate, "Mon, 07 Nov 1994 08:48:37 GMT");
test_parse!(test_rfc_850, "Monday, 07-Nov-94 08:48:37 GMT");
test_parse!(test_asctime, "Mon Nov 7 08:48:37 1994");
#[test]
fn test_no_date() {
assert!("this-is-no-date".parse::<HttpDate>().is_err());
}
}