ecb_rates/cli/
since.rs

1use crate::View;
2use std::{fmt, str::FromStr};
3
4#[derive(Debug, Clone, Copy)]
5pub enum ShowDays {
6    Days(usize),
7    All,
8}
9
10impl FromStr for ShowDays {
11    type Err = String;
12
13    fn from_str(s: &str) -> Result<Self, Self::Err> {
14        if s.eq_ignore_ascii_case("all") {
15            return Ok(ShowDays::All);
16        }
17        s.parse::<usize>().map(ShowDays::Days).map_err(|_| {
18            format!(
19                "Invalid value for since: '{}'. Use a positive integer or 'start'.",
20                s
21            )
22        })
23    }
24}
25impl fmt::Display for ShowDays {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            ShowDays::Days(days) => write!(f, "{}", days),
29            ShowDays::All => write!(f, "all"),
30        }
31    }
32}
33
34impl ShowDays {
35    /// None represents infinity
36    pub fn to_option(&self) -> Option<usize> {
37        match self {
38            ShowDays::Days(d) => Some(*d),
39            ShowDays::All => None,
40        }
41    }
42
43    pub fn to_view(&self) -> Option<View> {
44        match self {
45            ShowDays::Days(d) => match d {
46                0 => None,
47                1 => Some(View::TODAY),
48                2..=90 => Some(View::HistDays90),
49                91.. => Some(View::HistDaysAll),
50            },
51            ShowDays::All => Some(View::HistDaysAll),
52        }
53    }
54}