Add order by option

This commit is contained in:
2025-01-08 16:16:15 +01:00
parent 642db1dd6d
commit 1628a4b238
5 changed files with 37 additions and 7 deletions

View File

@@ -29,11 +29,21 @@ pub struct Cli {
#[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", short = 's', default_value_t = SortBy::Currency)]
pub sort_by: SortBy,
/// Amount of data
#[arg(value_enum, default_value_t = Resolution::TODAY, long="resolution", short='r')]
pub resolution: Resolution,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum SortBy {
Currency,
Rate,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Resolution {
TODAY,
@@ -51,6 +61,15 @@ impl Resolution {
}
}
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),
}
}
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum FormatOption {
/// JSON output