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
use std::{
fmt::{Display, Formatter, Result as FmtResult},
str::FromStr,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Period {
All,
Today,
Yesterday,
Week,
LastWeek,
Month,
LastMonth,
}
impl FromStr for Period {
type Err = String;
fn from_str(raw: &str) -> Result<Self, Self::Err> {
match raw {
"all" | "a" => Ok(Period::All),
"today" | "t" => Ok(Period::Today),
"yesterday" | "y" => Ok(Period::Yesterday),
"week" | "this week" | "w" | "tw" => Ok(Period::Week),
"last week" | "lastweek" | "lw" => Ok(Period::LastWeek),
"month" | "this month" | "m" | "tm" => Ok(Period::Month),
"last month" | "lastmonth" | "lm" => Ok(Period::LastMonth),
_ => Err("Time period not recognised.".into()),
}
}
}
impl Display for Period {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Period::All => write!(f, "All-Time"),
Period::Today => write!(f, "Today"),
Period::Yesterday => write!(f, "Yesterday"),
Period::Week => write!(f, "This Week"),
Period::LastWeek => write!(f, "Last Week"),
Period::Month => write!(f, "This Month"),
Period::LastMonth => write!(f, "Last Month"),
}
}
}