Skip to content

Commit 746715d

Browse files
committed
fix: handle invaild amount & red logs
1 parent 5c7720f commit 746715d

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed

Cargo.lock

+46-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
inquire = "0.7.5"
8+
termion = "4.0.0"
89

src/main.rs

+37-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::process::exit;
2+
use termion::color;
3+
24
mod split;
35
use inquire::{InquireError, Select, Text};
46
use split::*;
@@ -21,7 +23,7 @@ fn main() {
2123

2224
match selected_prompt {
2325
Ok(choice) => handle_choice(choice, &mut all_users, &mut all_transactions),
24-
Err(_) => println!("There was an error, please try again"),
26+
Err(_) => print_error_in_red("There was an error, please try again"),
2527
}
2628
println!();
2729
}
@@ -34,32 +36,32 @@ fn handle_choice(choice: &str, all_users: &mut Vec<User>, all_transactions: &mut
3436
"3. Split Bill" => split_bill(all_transactions),
3537
"4. View all Transactions" => view_all_expenses(all_transactions),
3638
"5. Exit" => handle_exit(),
37-
_ => println!("Invalid choice!"),
39+
_ => print_error_in_red("Invalid choice!"),
3840
}
3941
}
4042

4143
fn create_group(all_users: &mut Vec<User>) {
4244
let no_of_people = Text::new("Enter the number of people:").prompt();
4345
match no_of_people {
44-
Ok(no_of_people) => {
45-
let mut no_of_people = no_of_people.parse::<u8>().unwrap();
46-
if no_of_people > 0 {
47-
while no_of_people > 0 {
46+
Ok(no_of_people) => match no_of_people.parse::<u32>().as_mut() {
47+
Ok(no_of_people) => {
48+
while *no_of_people > 0 {
4849
let username = Text::new("Enter the user name:").prompt();
4950
match username {
5051
Ok(username) => {
5152
let user = User::create_user(&username);
5253
all_users.push(user);
5354
}
54-
Err(_) => println!("Error in creating user"),
55+
Err(_) => print_error_in_red("Error in creating user"),
5556
}
56-
no_of_people -= 1;
57+
*no_of_people -= 1;
5758
}
59+
println!("------------------Group Created----------------------");
5860
}
59-
}
60-
Err(_) => println!("Error occurred while taking number of people"),
61+
Err(_) => print_error_in_red("Please Select valid group number"),
62+
},
63+
Err(_) => print_error_in_red("Error occurred while taking number of people"),
6164
}
62-
println!("------------------Group Created----------------------");
6365
}
6466

6567
fn add_expense(all_users: &mut Vec<User>, all_transactions: &mut Transactions) {
@@ -86,22 +88,25 @@ fn add_expense(all_users: &mut Vec<User>, all_transactions: &mut Transactions) {
8688
let amount = Text::new("Enter the amount of expense:").prompt();
8789
match amount {
8890
Ok(amount) => {
89-
let amount = amount.parse::<u64>().unwrap();
90-
let from = User::create_user(selected_user);
91-
let to = User::create_user(giver_name);
92-
93-
let tx = Transaction::new(from, to, amount);
94-
all_transactions.add(tx);
91+
let amount = amount.parse::<u64>();
92+
match amount {
93+
Ok(amount) => {
94+
let from = User::create_user(selected_user);
95+
let to = User::create_user(giver_name);
96+
let tx = Transaction::new(from, to, amount);
97+
all_transactions.add(tx)
98+
}
99+
Err(_) => print_error_in_red("Amount is not valid"),
100+
};
95101
}
96-
Err(_) => println!("Error in entering amount"),
102+
Err(_) => print_error_in_red("Error in entering amount"),
97103
}
98104
}
99-
Err(_) => println!("Error in selecting giver"),
105+
Err(_) => print_error_in_red("Error in selecting giver"),
100106
}
101107
}
102-
Err(_) => println!("Error in selecting payer"),
108+
Err(_) => print_error_in_red("Error in selecting payer"),
103109
}
104-
println!("Added Expense...");
105110
}
106111

107112
fn split_bill(all_transactions: &mut Transactions) {
@@ -110,11 +115,21 @@ fn split_bill(all_transactions: &mut Transactions) {
110115
let selected_tx = all_transactions.split_bill();
111116
selected_tx.display();
112117
}
118+
113119
fn view_all_expenses(all_transactions: &mut Transactions) {
114120
all_transactions.display();
115121
}
116122

117123
fn handle_exit() {
118-
println!("Exiting CLI...");
124+
print_error_in_red("Exiting CLI...");
119125
exit(0);
120126
}
127+
128+
fn print_error_in_red(msg: &str) {
129+
println!(
130+
"{}{}{}",
131+
color::Fg(color::Red),
132+
msg,
133+
color::Fg(color::Reset)
134+
);
135+
}

0 commit comments

Comments
 (0)