From 89b28d0caefb3d1c665f3acff297da33588bd233 Mon Sep 17 00:00:00 2001 From: Lunush <76045303+lunush@users.noreply.github.com> Date: Sun, 30 May 2021 17:51:34 +0300 Subject: [PATCH] Improve input handling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 47 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3900dad..2bc3448 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -665,7 +665,7 @@ dependencies = [ [[package]] name = "rates" -version = "0.4.0" +version = "0.5.0" dependencies = [ "chrono", "directories", diff --git a/Cargo.toml b/Cargo.toml index 7c4064c..5d7c07c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rates" -version = "0.4.0" +version = "0.5.0" authors = ["lunush"] edition = "2018" description = "CLI tool to get currency exchange rates in your terminal" diff --git a/src/main.rs b/src/main.rs index 55bf56d..4534260 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,15 +174,18 @@ fn init_currency_data() -> (String, String) { #[derive(Debug, StructOpt)] #[structopt(name = "rates", about = "Get financial data in your terminal")] struct Args { - /// (Optional) Amount of currency to convert from + /// (Optional) Amount arg1: Option, /// Currency to convert from arg2: Option, - /// (Optional) Currency to convert to. Defaults to EUR if not provided + /// (Optional) "to" arg3: Option, + /// Currency to convert to. Defaults to EUR if not provided + arg4: Option, + /// Show only the result #[structopt(short = "s", long = "short")] short: bool, @@ -207,9 +210,10 @@ fn parse_args(args: &Args) -> ParsedArgs { let arg1 = args.arg1.clone(); let arg2 = args.arg2.clone(); let arg3 = args.arg3.clone(); + let arg4 = args.arg4.clone(); if arg1.is_none() { - panic!("At least one argument to convert from is expected"); + panic!("At least one argument to convert from is required"); } let is_arg1_num = arg1.clone().unwrap().parse::().is_ok(); @@ -223,11 +227,19 @@ fn parse_args(args: &Args) -> ParsedArgs { from = arg2.unwrap().to_uppercase(); - to = if let Some(arg) = arg3 { - arg.to_uppercase() - } else { - "EUR".to_owned() - }; + to = match arg3 { + Some(arg) => { + if arg == *"to" { + match arg4 { + Some(last_arg) => last_arg.to_uppercase(), + None => "EUR".to_owned(), + } + } else { + arg.to_uppercase() + } + } + None => "EUR".to_owned(), + } } else { amount = 1.0; @@ -237,11 +249,19 @@ fn parse_args(args: &Args) -> ParsedArgs { panic!("At least one argument to convert from is expected"); }; - to = if let Some(arg) = arg2 { - arg.to_uppercase() - } else { - "EUR".to_owned() - }; + to = match arg2 { + Some(arg) => { + if arg == *"to" { + match arg3 { + Some(last_arg) => last_arg.to_uppercase(), + None => "EUR".to_owned(), + } + } else { + arg.to_uppercase() + } + } + None => "EUR".to_owned(), + } } ParsedArgs { from, to, amount } @@ -261,6 +281,7 @@ fn main() { let digits = to_val.to_string().chars().collect::>(); + // BUG: Sometimes it crashes. It is not totally clear why yet. // If trim set to true, trim all decimals. Show some decimals otherwise. if trim { to_val = to_val.floor();