mirror of
https://github.com/lov3b/ecb-rates.git
synced 2025-07-07 20:10:30 +02:00
Simplify down to show-days
This commit is contained in:
59
src/cli/cli_t.rs
Normal file
59
src/cli/cli_t.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use clap::{arg, Parser, ValueEnum};
|
||||
|
||||
use super::{ShowDays, SortBy};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(author, version, about)]
|
||||
|
||||
pub struct Cli {
|
||||
/// Which currencies do you want to fetch rates for?
|
||||
#[arg(long = "currencies", short = 'c')]
|
||||
pub currencies: Vec<String>,
|
||||
|
||||
#[arg(value_enum, default_value_t = FormatOption::Plain)]
|
||||
pub command: FormatOption,
|
||||
|
||||
/// Don't show time in output
|
||||
#[arg(long = "no-time")]
|
||||
pub no_time: bool,
|
||||
|
||||
/// Print currencies in a compact single line
|
||||
#[arg(long = "compact")]
|
||||
pub compact: bool,
|
||||
|
||||
/// Override the cache
|
||||
#[arg(long = "no-cache")]
|
||||
pub no_cache: bool,
|
||||
|
||||
/// Force color in output. Normally it will disable color in pipes
|
||||
#[arg(long = "force-color")]
|
||||
pub force_color: bool,
|
||||
|
||||
/// Sort by the currency name (in alphabetical order), or by the rate value (low -> high)
|
||||
#[arg(value_enum, long = "sort-by", default_value_t = SortBy::Currency)]
|
||||
pub sort_by: SortBy,
|
||||
|
||||
/// Recalculate to the perspective from an included currency
|
||||
#[arg(long = "perspective", short = 'p')]
|
||||
pub perspective: Option<String>,
|
||||
|
||||
/// Invert the rate
|
||||
#[arg(long = "invert", short = 'i')]
|
||||
pub should_invert: bool,
|
||||
|
||||
//// Max decimals to keep in price.
|
||||
#[arg(long = "max-decimals", short = 'd', default_value_t = 5)]
|
||||
pub max_decimals: u8,
|
||||
|
||||
/// Amount of data
|
||||
#[arg(default_value_t = ShowDays::Days(1), long="show_days", short='s')]
|
||||
pub show_days: ShowDays,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, ValueEnum)]
|
||||
pub enum FormatOption {
|
||||
/// JSON output
|
||||
Json,
|
||||
/// Plain line-by-line output (with extra flags)
|
||||
Plain,
|
||||
}
|
7
src/cli/mod.rs
Normal file
7
src/cli/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
mod cli_t;
|
||||
mod since;
|
||||
mod sort_by;
|
||||
|
||||
pub use cli_t::{Cli, FormatOption};
|
||||
pub use since::ShowDays;
|
||||
pub use sort_by::SortBy;
|
54
src/cli/since.rs
Normal file
54
src/cli/since.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use crate::View;
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ShowDays {
|
||||
Days(usize),
|
||||
All,
|
||||
}
|
||||
|
||||
impl FromStr for ShowDays {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.eq_ignore_ascii_case("all") {
|
||||
return Ok(ShowDays::All);
|
||||
}
|
||||
s.parse::<usize>().map(ShowDays::Days).map_err(|_| {
|
||||
format!(
|
||||
"Invalid value for since: '{}'. Use a positive integer or 'start'.",
|
||||
s
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
impl fmt::Display for ShowDays {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ShowDays::Days(days) => write!(f, "{}", days),
|
||||
ShowDays::All => write!(f, "all"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ShowDays {
|
||||
/// None represents infinity
|
||||
pub fn to_option(&self) -> Option<usize> {
|
||||
match self {
|
||||
ShowDays::Days(d) => Some(*d),
|
||||
ShowDays::All => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_view(&self) -> Option<View> {
|
||||
match self {
|
||||
ShowDays::Days(d) => match d {
|
||||
0 => None,
|
||||
1 => Some(View::TODAY),
|
||||
2..=90 => Some(View::HistDays90),
|
||||
91.. => Some(View::HistDaysAll),
|
||||
},
|
||||
ShowDays::All => Some(View::HistDaysAll),
|
||||
}
|
||||
}
|
||||
}
|
16
src/cli/sort_by.rs
Normal file
16
src/cli/sort_by.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use clap::ValueEnum;
|
||||
|
||||
#[derive(Debug, ValueEnum, Clone)]
|
||||
pub enum SortBy {
|
||||
Currency,
|
||||
Rate,
|
||||
}
|
||||
|
||||
impl SortBy {
|
||||
pub fn get_comparer(&self) -> fn(&(&str, f64), &(&str, f64)) -> std::cmp::Ordering {
|
||||
match self {
|
||||
Self::Currency => |a, b| a.0.cmp(&b.0),
|
||||
Self::Rate => |a, b| a.1.total_cmp(&b.1),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user