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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::ops::{Bound, RangeBounds};
use std::time;
pub struct TimerangeBound<T> {
obj: T,
start: Option<time::SystemTime>,
end: Option<time::SystemTime>,
}
fn unwrap_bound(b: Bound<&'_ time::SystemTime>) -> Option<time::SystemTime> {
match b {
Bound::Included(x) => Some(*x),
Bound::Excluded(x) => Some(*x),
_ => None,
}
}
impl<T> TimerangeBound<T> {
pub fn new<U>(obj: T, range: U) -> Self
where
U: RangeBounds<time::SystemTime>,
{
let start = unwrap_bound(range.start_bound());
let end = unwrap_bound(range.end_bound());
Self { obj, start, end }
}
#[must_use]
pub fn extend_tolerance(self, d: time::Duration) -> Self {
let end = self.end.map(|t| t + d);
Self { end, ..self }
}
#[must_use]
pub fn extend_pre_tolerance(self, d: time::Duration) -> Self {
let start = self.start.map(|t| t - d);
Self { start, ..self }
}
#[cfg(feature = "experimental-api")]
pub fn dangerously_into_parts(self) -> (T, (Bound<time::SystemTime>, Bound<time::SystemTime>)) {
(
self.obj,
(
self.start.map(Bound::Included).unwrap_or(Bound::Unbounded),
self.end.map(Bound::Included).unwrap_or(Bound::Unbounded),
),
)
}
#[cfg(feature = "experimental-api")]
pub fn dangerously_peek(&self) -> &T {
&self.obj
}
}
impl<T> crate::Timebound<T> for TimerangeBound<T> {
type Error = crate::TimeValidityError;
fn is_valid_at(&self, t: &time::SystemTime) -> Result<(), Self::Error> {
use crate::TimeValidityError;
if let Some(start) = self.start {
if let Ok(d) = start.duration_since(*t) {
return Err(TimeValidityError::NotYetValid(d));
}
}
if let Some(end) = self.end {
if let Ok(d) = t.duration_since(end) {
return Err(TimeValidityError::Expired(d));
}
}
Ok(())
}
fn dangerously_assume_timely(self) -> T {
self.obj
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{TimeValidityError, Timebound};
use std::time::{Duration, SystemTime};
#[test]
fn test_bounds() {
let one_day = Duration::new(86400, 0);
let mixminion_v0_0_1 = SystemTime::UNIX_EPOCH + 12059 * one_day;
let tor_v0_0_2pre13 = SystemTime::UNIX_EPOCH + 12344 * one_day;
let cussed_nougat = SystemTime::UNIX_EPOCH + 14093 * one_day;
let tor_v0_4_4_5 = SystemTime::UNIX_EPOCH + 18520 * one_day;
let today = SystemTime::UNIX_EPOCH + 18527 * one_day;
let tr = TimerangeBound::new((), ..tor_v0_4_4_5);
assert_eq!(tr.start, None);
assert_eq!(tr.end, Some(tor_v0_4_4_5));
assert!(tr.is_valid_at(&mixminion_v0_0_1).is_ok());
assert!(tr.is_valid_at(&tor_v0_0_2pre13).is_ok());
assert_eq!(
tr.is_valid_at(&today),
Err(TimeValidityError::Expired(7 * one_day))
);
let tr = TimerangeBound::new((), tor_v0_0_2pre13..=tor_v0_4_4_5);
assert_eq!(tr.start, Some(tor_v0_0_2pre13));
assert_eq!(tr.end, Some(tor_v0_4_4_5));
assert_eq!(
tr.is_valid_at(&mixminion_v0_0_1),
Err(TimeValidityError::NotYetValid(285 * one_day))
);
assert!(tr.is_valid_at(&cussed_nougat).is_ok());
assert_eq!(
tr.is_valid_at(&today),
Err(TimeValidityError::Expired(7 * one_day))
);
let tr = tr
.extend_pre_tolerance(5 * one_day)
.extend_tolerance(2 * one_day);
assert_eq!(tr.start, Some(tor_v0_0_2pre13 - 5 * one_day));
assert_eq!(tr.end, Some(tor_v0_4_4_5 + 2 * one_day));
let tr = TimerangeBound::new((), tor_v0_4_4_5..);
assert_eq!(tr.start, Some(tor_v0_4_4_5));
assert_eq!(tr.end, None);
assert_eq!(
tr.is_valid_at(&cussed_nougat),
Err(TimeValidityError::NotYetValid(4427 * one_day))
);
assert!(tr.is_valid_at(&today).is_ok());
}
#[test]
fn test_checking() {
let one_day = Duration::new(86400, 0);
let de = SystemTime::UNIX_EPOCH + one_day * 7580;
let cz_sk = SystemTime::UNIX_EPOCH + one_day * 8401;
let eu = SystemTime::UNIX_EPOCH + one_day * 8705;
let za = SystemTime::UNIX_EPOCH + one_day * 8882;
let tr = TimerangeBound::new("Hello world", cz_sk..eu);
assert!(tr.check_valid_at(&za).is_err());
let tr = TimerangeBound::new("Hello world", cz_sk..za);
assert_eq!(tr.check_valid_at(&eu), Ok("Hello world"));
let tr = TimerangeBound::new("hello world", de..);
assert_eq!(tr.check_valid_now(), Ok("hello world"));
let tr = TimerangeBound::new("hello world", ..za);
assert!(tr.check_valid_now().is_err());
let tr = TimerangeBound::new("hello world", de..);
assert_eq!(tr.check_valid_at_opt(None), Ok("hello world"));
let tr = TimerangeBound::new("hello world", de..);
assert_eq!(
tr.check_valid_at_opt(Some(SystemTime::now())),
Ok("hello world")
);
let tr = TimerangeBound::new("hello world", ..za);
assert!(tr.check_valid_at_opt(None).is_err());
}
#[cfg(feature = "experimental-api")]
#[test]
fn test_dangerous() {
let t1 = SystemTime::now();
let t2 = t1 + Duration::from_secs(60 * 525600);
let tr = TimerangeBound::new("cups of coffee", t1..=t2);
assert_eq!(tr.dangerously_peek(), &"cups of coffee");
let (a, b) = tr.dangerously_into_parts();
assert_eq!(a, "cups of coffee");
assert_eq!(b.0, Bound::Included(t1));
assert_eq!(b.1, Bound::Included(t2));
}
}