diff --git a/src/app.rs b/src/app.rs index b56d3c7..3825ac3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -41,7 +41,7 @@ impl App { current_widget: CurrentWidget::AccountList, acc_list: AccountList::new(), trx_table: TrxTable::new(), - new_account: Account::new(0, String::new(), 0.0, AccountType::Cash), + new_account: Account::new_empty(), new_transaction: Transaction::new_empty(), selected_transaction_input: TransactionInput::Type, selected_account_input: AccountInput::Asset, @@ -62,7 +62,7 @@ impl App { .iter() .filter(|x| x.get_id() == 1) .next() - .unwrap() + .unwrap_or(&TransactionType::new(0, String::new())) .get_description(), connection: con, }; @@ -112,7 +112,7 @@ impl App { if new { self.acc_list.add_account(ac); } - self.new_account = Account::new(0, String::new(), 0.0, AccountType::Cash); + self.new_account = Account::new_empty(); self.current_input = String::new(); self.selected_account_input = AccountInput::Asset; } @@ -152,7 +152,7 @@ impl App { self.selected_transaction_input = TransactionInput::Description; let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d") .expect("Faile to parse data"); - let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime + let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap_or_default(); // to account for my local datetime self.new_transaction .set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time))); } @@ -192,7 +192,7 @@ impl App { self.selected_transaction_input = TransactionInput::Amount; let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d") .expect("Faile to parse data"); - let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime + let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap_or_default(); // to account for my local datetime self.new_transaction .set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time))); } @@ -327,7 +327,7 @@ impl App { .iter() .filter(|x| x.get_id() == id) .next() - .unwrap() + .unwrap_or(&TransactionType::new(0, String::new())) .get_description(); } } diff --git a/src/entities/account.rs b/src/entities/account.rs index 31b294a..f88a81e 100644 --- a/src/entities/account.rs +++ b/src/entities/account.rs @@ -21,6 +21,14 @@ impl Account { ac_type: ac_type, } } + pub fn new_empty() -> Self { + return Account { + id: 0, + name: String::new(), + asset_price: 0.0, + ac_type: AccountType::Cash, + }; + } pub fn get_id(&self) -> i64 { return self.id; } @@ -37,7 +45,10 @@ impl Account { self.asset_price = asset_price; } pub fn get_ac_type(&self) -> AccountType { - return AccountType::try_from(self.ac_type as i64).unwrap(); + match AccountType::try_from(self.ac_type as i64) { + Ok(at) => return at, + Err(_) => AccountType::Cash, + } } pub fn set_type(&mut self, ac_type: AccountType) { self.ac_type = ac_type; diff --git a/src/entities/accountlist.rs b/src/entities/accountlist.rs index 9f2d4ee..dd92655 100644 --- a/src/entities/accountlist.rs +++ b/src/entities/accountlist.rs @@ -37,9 +37,12 @@ impl AccountList { pub fn selected_ac(&self) -> Account { if let Some(i) = self.state.selected() { - return self.accounts.get(i).unwrap().clone(); + match self.accounts.get(i) { + Some(a) => return a.clone(), + None => return Account::new_empty(), + } } - return Account::new(0, String::new(), 0.0, AccountType::Cash); + return Account::new_empty(); } pub fn add_account(&mut self, ac: Account) { diff --git a/src/entities/transaction.rs b/src/entities/transaction.rs index 024734a..f93b5b2 100644 --- a/src/entities/transaction.rs +++ b/src/entities/transaction.rs @@ -43,7 +43,7 @@ impl Transaction { id: 0, account_id: 0, amount: 0.0, - date: DateTime::from_timestamp(0, 0).unwrap(), + date: DateTime::from_timestamp(0, 0).unwrap_or_default(), description: "".to_string(), type_id: 0, tr_type: TransactionType::new(0, "".to_string()), diff --git a/src/entities/trxtable.rs b/src/entities/trxtable.rs index 39b7d43..caec287 100644 --- a/src/entities/trxtable.rs +++ b/src/entities/trxtable.rs @@ -32,7 +32,10 @@ impl TrxTable { pub fn selected_tr(&self) -> Transaction { if let Some(i) = self.state.selected() { - return self.trx.get(i).unwrap().clone(); + match self.trx.get(i) { + Some(t) => return t.clone(), + None => return Transaction::new_empty(), + } } return Transaction::new_empty(); } diff --git a/src/main.rs b/src/main.rs index 1cbf1e0..02fb034 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,7 +161,7 @@ where } KeyCode::Esc => { app.current_screen = CurrentScreen::Main; - app.new_account = Account::new(0, String::new(), 0.0, AccountType::Cash); + app.new_account = Account::new_empty(); } _ => {} },