mirror of
				https://github.com/lov3b/ecb-rates.git
				synced 2025-11-03 23:00:21 +01:00 
			
		
		
		
	Add order by option
This commit is contained in:
		
							
								
								
									
										19
									
								
								src/cli.rs
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								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
 | 
			
		||||
 
 | 
			
		||||
@@ -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::<Vec<_>>()
 | 
			
		||||
 
 | 
			
		||||
@@ -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)));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,18 @@
 | 
			
		||||
use crate::cli::SortBy;
 | 
			
		||||
 | 
			
		||||
pub trait TableTrait<'a> {
 | 
			
		||||
    type Header;
 | 
			
		||||
    type ColumnLeft;
 | 
			
		||||
    type ColumnRight;
 | 
			
		||||
    type RowLeft;
 | 
			
		||||
 | 
			
		||||
    fn new(header: Option<Self::Header>, column_left: Self::ColumnLeft, column_right: Self::ColumnRight) -> Self;
 | 
			
		||||
    fn new(
 | 
			
		||||
        header: Option<Self::Header>,
 | 
			
		||||
        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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user