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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Defines the `Age` field for the `entries` table.
use arrayvec::ArrayString;
use serde::de::{self, Deserialize, Visitor};
use serde::ser::Serialize;
use std::cmp::Ordering;
use std::fmt::{self, Write};
use std::num;
use std::str::FromStr;
use crate::Date;
/// The reported age of the lifter at a given meet.
/// In the CSV file, approximate ages are reported with '.5' added.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Age {
/// The exact age of the lifter.
Exact(u8),
/// Either one of two ages, stored as the lower of the pair.
///
/// For example, an Approximate Age of 19 means "19 or 20".
///
/// Approximate Ages occur when derived from a BirthYear,
/// where it's not known whether the birthday occurred yet.
Approximate(u8),
/// No age specified.
None,
}
/// An opaque wrapper for `Age` that serializes to "23~" instead of "23.5".
#[derive(Copy, Clone, Debug)]
pub struct PrettyAge(Age);
impl From<Age> for PrettyAge {
fn from(a: Age) -> PrettyAge {
PrettyAge { 0: a }
}
}
impl From<Age> for f64 {
fn from(a: Age) -> f64 {
match a {
Age::Exact(a) => a.into(),
Age::Approximate(a) => f64::from(a) + 0.5,
Age::None => 0.0,
}
}
}
impl Age {
/// Convert from an i64. Used by the TOML deserializer.
pub fn from_i64(n: i64) -> Result<Self, &'static str> {
// Some of the CONFIG.toml files hardcode 999 to mean "max Age".
if n == 999 {
return Ok(Age::Exact(u8::max_value()));
}
if n < 0 {
return Err("Age may not be negative");
}
if n > (i64::from(u8::max_value())) {
return Err("Age can be at most 256");
}
Ok(Age::Exact(n as u8))
}
/// Convert from an f64. Used by the TOML deserializer.
pub fn from_f64(f: f64) -> Result<Self, num::ParseIntError> {
// Just use the from_str() implementation.
// This function is not called often, so it's OK to be slow.
let s = format!("{}", f);
s.parse::<Age>()
}
/// Given a BirthYear, calculates the approximate age of the lifter
/// on the given Date.
///
/// # Examples
///
/// ```
/// # use opltypes::{Age, Date};
/// let date = Date::from_parts(2019, 02, 16);
/// assert_eq!(Age::from_birthyear_on_date(1988, date), Age::Approximate(30));
/// ```
pub fn from_birthyear_on_date(birthyear: u32, on_date: Date) -> Self {
let on_year = on_date.year();
match on_year.cmp(&birthyear) {
Ordering::Less => Age::None,
Ordering::Greater => Age::Approximate((on_year - birthyear - 1) as u8),
Ordering::Equal => Age::Approximate(0),
}
}
/// Converts to an `Option<u8>`.
///
/// # Examples
///
/// ```
/// # use opltypes::Age;
/// assert_eq!(Age::Exact(23).to_u8_option(), Some(23));
/// assert_eq!(Age::Approximate(23).to_u8_option(), Some(23));
/// assert_eq!(Age::None.to_u8_option(), None);
/// ```
pub fn to_u8_option(self) -> Option<u8> {
match self {
Age::Exact(age) | Age::Approximate(age) => Some(age),
Age::None => None,
}
}
/// Whether the given Age is definitely less than another.
///
/// Because of Approximate Ages, this does not produce a deterministic ordering,
/// but it is still useful to determine whether one Age is out of range
/// of another, usually for purposes of error checking.
///
/// # Examples
///
/// ```
/// # use opltypes::Age;
/// let approx_18 = Age::Approximate(18); // "18 or 19"
/// let approx_19 = Age::Approximate(19); // "19 or 20"
/// let exact_20 = Age::Exact(20);
///
/// assert_eq!(approx_18.is_definitely_less_than(exact_20), true);
/// assert_eq!(approx_19.is_definitely_less_than(exact_20), false);
/// ```
pub fn is_definitely_less_than(self, other: Age) -> bool {
match self {
Age::Exact(age) => match other {
Age::Exact(other) => age < other,
Age::Approximate(other) => age < other,
Age::None => false,
},
Age::Approximate(age) => match other {
Age::Exact(other) => age + 1 < other,
Age::Approximate(other) => age + 1 < other,
Age::None => false,
},
Age::None => false,
}
}
/// Whether the given Age is definitely greater than another.
///
/// Because of Approximate Ages, this does not produce a deterministic ordering,
/// but it is still useful to determine whether one Age is out of range
/// of another, usually for purposes of error checking.
///
/// # Examples
///
/// ```
/// # use opltypes::Age;
/// let approx_17 = Age::Approximate(17); // "17 or 18"
/// let approx_19 = Age::Approximate(19); // "19 or 20"
/// let exact_18 = Age::Exact(18);
/// let exact_19 = Age::Exact(19);
///
/// assert_eq!(approx_17.is_definitely_greater_than(exact_18), false);
/// assert_eq!(approx_19.is_definitely_greater_than(exact_18), true);
/// assert_eq!(approx_19.is_definitely_greater_than(exact_19), false);
/// ```
pub fn is_definitely_greater_than(self, other: Age) -> bool {
match self {
Age::Exact(age) => match other {
Age::Exact(other) => age > other,
Age::Approximate(other) => age > other + 1,
Age::None => false,
},
Age::Approximate(age) => match other {
Age::Exact(other) => age > other,
Age::Approximate(other) => age > other + 1,
Age::None => false,
},
Age::None => false,
}
}
/// Whether the given Age is an Age::Exact.
///
/// # Examples
///
/// ```
/// # use opltypes::Age;
/// assert!(Age::Exact(23).is_exact());
/// assert!(!Age::Approximate(23).is_exact());
/// assert!(!Age::None.is_exact());
/// ```
pub fn is_exact(self) -> bool {
match self {
Age::Exact(_) => true,
Age::Approximate(_) | Age::None => false,
}
}
/// Whether the given Age is an Age::None.
///
/// # Examples
///
/// ```
/// # use opltypes::Age;
/// assert!(!Age::Exact(23).is_none());
/// assert!(!Age::Approximate(23).is_none());
/// assert!(Age::None.is_none());
/// ```
pub fn is_none(self) -> bool {
self == Age::None
}
/// Whether the given Age is not an Age::None.
pub fn is_some(self) -> bool {
self != Age::None
}
}
impl Default for Age {
fn default() -> Age {
Age::None
}
}
impl PartialOrd for Age {
fn partial_cmp(&self, other: &Age) -> Option<Ordering> {
match self {
Age::Exact(age) => match other {
Age::Exact(other_age) => Some(age.cmp(other_age)),
Age::Approximate(other_age) => {
if *other_age == u8::max_value() {
if *age == u8::max_value() {
Some(Ordering::Equal)
} else {
Some(Ordering::Less)
}
} else {
Some(age.cmp(&(other_age + 1)))
}
}
Age::None => Some(Ordering::Less),
},
Age::Approximate(age) => match other {
Age::Exact(other_age) => {
if *age == u8::max_value() {
if *other_age == u8::max_value() {
Some(Ordering::Equal)
} else {
Some(Ordering::Greater)
}
} else {
Some((age + 1).cmp(other_age))
}
}
Age::Approximate(other_age) => Some(age.cmp(other_age)),
Age::None => Some(Ordering::Less),
},
Age::None => match other {
Age::Exact(_) | Age::Approximate(_) => Some(Ordering::Greater),
Age::None => Some(Ordering::Equal),
},
}
}
}
impl fmt::Display for Age {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Age::Exact(n) => write!(f, "{}", n),
Age::Approximate(n) => write!(f, "{}~", n),
Age::None => Ok(()),
}
}
}
impl FromStr for Age {
type Err = num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Ok(Age::None);
}
// Some of the CONFIG.toml files hardcode 999 to mean "max Age".
if s == "999" {
return Ok(Age::Exact(u8::max_value()));
}
let v: Vec<&str> = s.split('.').collect();
if v.len() == 1 {
v[0].parse::<u8>().map(Age::Exact)
} else {
v[0].parse::<u8>().map(Age::Approximate)
}
}
}
impl Serialize for Age {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
// Largest possible string for a `u8` Age is "256.5", 5 characters.
let mut buf = ArrayString::<5>::new();
match *self {
Age::Exact(n) => write!(buf, "{}", n).expect("ArrayString overflow"),
Age::Approximate(n) => write!(buf, "{}.5", n).expect("ArrayString overflow"),
Age::None => (),
};
serializer.serialize_str(&buf)
}
}
struct AgeVisitor;
impl<'de> Visitor<'de> for AgeVisitor {
type Value = Age;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an age (23) or approximate age (23.5)")
}
fn visit_f64<E: de::Error>(self, value: f64) -> Result<Age, E> {
Age::from_f64(value).map_err(E::custom)
}
fn visit_i64<E: de::Error>(self, value: i64) -> Result<Age, E> {
Age::from_i64(value).map_err(E::custom)
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<Age, E> {
Age::from_str(value).map_err(E::custom)
}
}
impl<'de> Deserialize<'de> for Age {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Age, D::Error> {
deserializer.deserialize_str(AgeVisitor)
}
}
impl Serialize for PrettyAge {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
// Largest possible string for a `u8` Age is "256~", 4 characters.
let mut buf = ArrayString::<4>::new();
match self.0 {
Age::Exact(n) => write!(buf, "{}", n).expect("ArrayString overflow"),
Age::Approximate(n) => write!(buf, "{}~", n).expect("ArrayString overflow"),
Age::None => (),
};
serializer.serialize_str(&buf)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_age_display() {
let a = "29".parse::<Age>().unwrap();
assert_eq!(format!("{}", a), "29");
let a = "29.5".parse::<Age>().unwrap();
assert_eq!(format!("{}", a), "29~");
let a = "".parse::<Age>().unwrap();
assert_eq!(format!("{}", a), "");
}
#[test]
fn test_is_definitely_less_than() {
let approx_17 = Age::Approximate(17); // "17 or 18"
let approx_18 = Age::Approximate(18); // "18 or 19"
let approx_19 = Age::Approximate(19); // "19 or 20"
let exact_17 = Age::Exact(17);
let exact_18 = Age::Exact(18);
let exact_19 = Age::Exact(19);
// Lower approximates compared to higher approximates.
assert_eq!(approx_17.is_definitely_less_than(approx_17), false);
assert_eq!(approx_17.is_definitely_less_than(approx_18), false);
assert_eq!(approx_17.is_definitely_less_than(approx_19), true);
// Higher approximates compared to lower approximates.
assert_eq!(approx_19.is_definitely_less_than(approx_17), false);
assert_eq!(approx_19.is_definitely_less_than(approx_18), false);
assert_eq!(approx_19.is_definitely_less_than(approx_19), false);
// Lower exacts compared to higher approximates.
assert_eq!(exact_17.is_definitely_less_than(approx_17), false);
assert_eq!(exact_17.is_definitely_less_than(approx_18), true);
assert_eq!(exact_17.is_definitely_less_than(approx_19), true);
// Higher approximates compared to lower exacts.
assert_eq!(approx_19.is_definitely_less_than(exact_17), false);
assert_eq!(approx_19.is_definitely_less_than(exact_18), false);
assert_eq!(approx_19.is_definitely_less_than(exact_19), false);
// Lower approximates compared to higher exacts.
assert_eq!(approx_17.is_definitely_less_than(exact_17), false);
assert_eq!(approx_17.is_definitely_less_than(exact_18), false);
assert_eq!(approx_17.is_definitely_less_than(exact_19), true);
// Higher exacts compared to lower approximates.
assert_eq!(exact_19.is_definitely_less_than(approx_17), false);
assert_eq!(exact_19.is_definitely_less_than(approx_18), false);
assert_eq!(exact_19.is_definitely_less_than(approx_19), false);
}
#[test]
fn test_is_definitely_greater_than() {
let approx_17 = Age::Approximate(17); // "17 or 18"
let approx_18 = Age::Approximate(18); // "18 or 19"
let approx_19 = Age::Approximate(19); // "19 or 20"
let exact_17 = Age::Exact(17);
let exact_18 = Age::Exact(18);
let exact_19 = Age::Exact(19);
// Lower approximates compared to higher approximates.
assert_eq!(approx_17.is_definitely_greater_than(approx_17), false);
assert_eq!(approx_17.is_definitely_greater_than(approx_18), false);
assert_eq!(approx_17.is_definitely_greater_than(approx_19), false);
// Higher approximates compared to lower approximates.
assert_eq!(approx_19.is_definitely_greater_than(approx_17), true);
assert_eq!(approx_19.is_definitely_greater_than(approx_18), false);
assert_eq!(approx_19.is_definitely_greater_than(approx_19), false);
// Lower exacts compared to higher approximates.
assert_eq!(exact_17.is_definitely_greater_than(approx_17), false);
assert_eq!(exact_17.is_definitely_greater_than(approx_18), false);
assert_eq!(exact_17.is_definitely_greater_than(approx_19), false);
// Higher approximates compared to lower exacts.
assert_eq!(approx_19.is_definitely_greater_than(exact_17), true);
assert_eq!(approx_19.is_definitely_greater_than(exact_18), true);
assert_eq!(approx_19.is_definitely_greater_than(exact_19), false);
// Lower approximates compared to higher exacts.
assert_eq!(approx_17.is_definitely_greater_than(exact_17), false);
assert_eq!(approx_17.is_definitely_greater_than(exact_18), false);
assert_eq!(approx_17.is_definitely_greater_than(exact_19), false);
// Higher exacts compared to lower approximates.
assert_eq!(exact_19.is_definitely_greater_than(approx_17), true);
assert_eq!(exact_19.is_definitely_greater_than(approx_18), false);
assert_eq!(exact_19.is_definitely_greater_than(approx_19), false);
}
}