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
use std::time::SystemTime;
use util::HttpDate;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IfUnmodifiedSince(HttpDate);
derive_header! {
IfUnmodifiedSince(_),
name: IF_UNMODIFIED_SINCE
}
impl IfUnmodifiedSince {
pub fn precondition_passes(&self, last_modified: SystemTime) -> bool {
self.0 >= last_modified.into()
}
}
impl From<SystemTime> for IfUnmodifiedSince {
fn from(time: SystemTime) -> IfUnmodifiedSince {
IfUnmodifiedSince(time.into())
}
}
impl From<IfUnmodifiedSince> for SystemTime {
fn from(date: IfUnmodifiedSince) -> SystemTime {
date.0.into()
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn precondition_passes() {
let newer = SystemTime::now();
let exact = newer - Duration::from_secs(2);
let older = newer - Duration::from_secs(4);
let if_unmod = IfUnmodifiedSince::from(exact);
assert!(!if_unmod.precondition_passes(newer));
assert!(if_unmod.precondition_passes(exact));
assert!(if_unmod.precondition_passes(older));
}
}