From 1628a4b23858a350e53fec1216364824110bf595 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Wed, 8 Jan 2025 16:16:15 +0100 Subject: [PATCH] Add order by option --- src/cli.rs | 19 +++++++++++++++++++ src/main.rs | 2 +- src/table/table_owned.rs | 7 +++++-- src/table/table_ref.rs | 6 ++++-- src/table/table_trait.rs | 10 ++++++++-- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 89efc1a..955c1dd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index 53858ac..c9c4dd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,7 +114,7 @@ async fn main() -> ExitCode { .iter() .map(|x| { let mut t: TableRef = x.into(); - t.sort(); + t.sort(&cli.sort_by); t.to_string() }) .collect::>() diff --git a/src/table/table_owned.rs b/src/table/table_owned.rs index a57ab25..34aa372 100644 --- a/src/table/table_owned.rs +++ b/src/table/table_owned.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use crate::cli::SortBy; use crate::models::ExchangeRateResult; use super::table_display::helper_table_print; @@ -47,8 +48,10 @@ impl<'a> TableTrait<'a> for Table { self.rows.push((row_left, row_right)); } - fn sort(&mut self) { - self.rows.sort_by(|a, b| a.1.total_cmp(&b.1)); + fn sort(&mut self, sort_by: &SortBy) { + let comparer = sort_by.get_comparer(); + self.rows + .sort_by(|a, b| comparer(&(&a.0, a.1), &(&b.0, b.1))); } } diff --git a/src/table/table_ref.rs b/src/table/table_ref.rs index 1121244..122321c 100644 --- a/src/table/table_ref.rs +++ b/src/table/table_ref.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use crate::cli::SortBy; use crate::models::ExchangeRateResult; use super::table_display::helper_table_print; @@ -49,8 +50,9 @@ impl<'a> TableTrait<'a> for TableRef<'a> { self.rows.push((row_left, row_right)); } - fn sort(&mut self) { - self.rows.sort_by(|a, b| a.1.total_cmp(&b.1)); + fn sort(&mut self, sort_by: &SortBy) { + let comparer = sort_by.get_comparer(); + self.rows.sort_by(comparer); } } diff --git a/src/table/table_trait.rs b/src/table/table_trait.rs index c68a3ad..4c363f6 100644 --- a/src/table/table_trait.rs +++ b/src/table/table_trait.rs @@ -1,12 +1,18 @@ +use crate::cli::SortBy; + pub trait TableTrait<'a> { type Header; type ColumnLeft; type ColumnRight; type RowLeft; - fn new(header: Option, column_left: Self::ColumnLeft, column_right: Self::ColumnRight) -> Self; + fn new( + header: Option, + column_left: Self::ColumnLeft, + column_right: Self::ColumnRight, + ) -> Self; fn disable_header(&mut self); fn set_header(&mut self, header: Self::Header); fn add_row(&mut self, row_left: Self::RowLeft, row_right: f64); - fn sort(&mut self); + fn sort(&mut self, sort_by: &SortBy); }