fluxcap/
constants.rs

1#![deny(warnings)]
2
3pub fn weekday(d: &str) -> Option<u32> {
4    match d {
5        "sunday"    | "sundays"    | "sun" => Some(0),
6        "monday"    | "mondays"    | "mon" => Some(1),
7        "tuesday"   | "tuesdays"   | "tue" => Some(2),
8        "wednesday" | "wednesdays" | "wed" => Some(3),
9        "thursday"  | "thursdays"  | "thu" => Some(4),
10        "friday"    | "fridays"    | "fri" => Some(5),
11        "saturday"  | "saturdays"  | "sat" => Some(6),
12        _           => None
13    }
14}
15
16pub fn month(m: &str) -> Option<u32> {
17    match m {
18        "january"   |  "jan"    => Some(1),
19        "february"  |  "feb"    => Some(2),
20        "march"     |  "mar"    => Some(3),
21        "april"     |  "apr"    => Some(4),
22        "may"       => Some(5),
23        "june"      |  "jun"    => Some(6),
24        "july"      |  "jul"    => Some(7),
25        "august"    |  "aug"    => Some(8),
26        "september" |  "sep"    => Some(9),
27        "october"   |  "oct"    => Some(10),
28        "november"  |  "nov"    => Some(11),
29        "december"  |  "dec"    => Some(12),
30        _           => None
31    }
32}
33
34pub fn ordinal(n: &str) -> Option<u32> {
35    static ORD: [&str;31] = [
36        "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
37        "eigth", "ninth", "thenth", "eleventh", "twelveth", "thirteenth",
38        "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth",
39        "nineteenth", "twentieth", "twenty-first", "twenty-second",
40        "twenty-third", "twenty-fourth", "twenty-fith", "twenty-sixth",
41        "twenty-seventh", "twenty-eigth", "twenty-ninth", "thirtieth",
42        "thirty-first",
43    ];
44    ORD.iter()
45       .enumerate()
46       .filter_map(|(i, txt)| if *txt == n { Some((i+1) as u32) } else { None })
47       .next()
48}
49
50pub fn short_ordinal(n: &str) -> Option<u32> {
51    use std::str::FromStr;
52    let num = n.chars().take_while(|d| d.is_numeric()).collect::<String>();
53    match &n[num.len()..] {
54        "st"|"nd"|"rd"|"th" => u32::from_str(&num).ok(),
55        _ => None
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::{ordinal, short_ordinal};
62    #[test]
63    fn test_short_ordinal() {
64        assert_eq!(short_ordinal("22nd"), Some(22));
65        assert_eq!(short_ordinal("43rd"), Some(43));
66        assert_eq!(short_ordinal("5ht"), None);
67    }
68    #[test]
69    fn test_ordinal() {
70        assert_eq!(ordinal("twenty-fourth"), Some(24));
71        assert_eq!(ordinal("twelveth"), Some(12));
72    }
73}