code cleanup

This commit is contained in:
thatscringebro
2026-04-02 13:36:36 -04:00
parent c009f84038
commit ba1b5349db
6 changed files with 29 additions and 12 deletions

View File

@@ -41,7 +41,7 @@ impl App {
current_widget: CurrentWidget::AccountList, current_widget: CurrentWidget::AccountList,
acc_list: AccountList::new(), acc_list: AccountList::new(),
trx_table: TrxTable::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(), new_transaction: Transaction::new_empty(),
selected_transaction_input: TransactionInput::Type, selected_transaction_input: TransactionInput::Type,
selected_account_input: AccountInput::Asset, selected_account_input: AccountInput::Asset,
@@ -62,7 +62,7 @@ impl App {
.iter() .iter()
.filter(|x| x.get_id() == 1) .filter(|x| x.get_id() == 1)
.next() .next()
.unwrap() .unwrap_or(&TransactionType::new(0, String::new()))
.get_description(), .get_description(),
connection: con, connection: con,
}; };
@@ -112,7 +112,7 @@ impl App {
if new { if new {
self.acc_list.add_account(ac); 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.current_input = String::new();
self.selected_account_input = AccountInput::Asset; self.selected_account_input = AccountInput::Asset;
} }
@@ -152,7 +152,7 @@ impl App {
self.selected_transaction_input = TransactionInput::Description; self.selected_transaction_input = TransactionInput::Description;
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d") let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
.expect("Faile to parse data"); .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 self.new_transaction
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time))); .set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
} }
@@ -192,7 +192,7 @@ impl App {
self.selected_transaction_input = TransactionInput::Amount; self.selected_transaction_input = TransactionInput::Amount;
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d") let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
.expect("Faile to parse data"); .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 self.new_transaction
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time))); .set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
} }
@@ -327,7 +327,7 @@ impl App {
.iter() .iter()
.filter(|x| x.get_id() == id) .filter(|x| x.get_id() == id)
.next() .next()
.unwrap() .unwrap_or(&TransactionType::new(0, String::new()))
.get_description(); .get_description();
} }
} }

View File

@@ -21,6 +21,14 @@ impl Account {
ac_type: ac_type, 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 { pub fn get_id(&self) -> i64 {
return self.id; return self.id;
} }
@@ -37,7 +45,10 @@ impl Account {
self.asset_price = asset_price; self.asset_price = asset_price;
} }
pub fn get_ac_type(&self) -> AccountType { 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) { pub fn set_type(&mut self, ac_type: AccountType) {
self.ac_type = ac_type; self.ac_type = ac_type;

View File

@@ -37,9 +37,12 @@ impl AccountList {
pub fn selected_ac(&self) -> Account { pub fn selected_ac(&self) -> Account {
if let Some(i) = self.state.selected() { 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) { pub fn add_account(&mut self, ac: Account) {

View File

@@ -43,7 +43,7 @@ impl Transaction {
id: 0, id: 0,
account_id: 0, account_id: 0,
amount: 0.0, amount: 0.0,
date: DateTime::from_timestamp(0, 0).unwrap(), date: DateTime::from_timestamp(0, 0).unwrap_or_default(),
description: "".to_string(), description: "".to_string(),
type_id: 0, type_id: 0,
tr_type: TransactionType::new(0, "".to_string()), tr_type: TransactionType::new(0, "".to_string()),

View File

@@ -32,7 +32,10 @@ impl TrxTable {
pub fn selected_tr(&self) -> Transaction { pub fn selected_tr(&self) -> Transaction {
if let Some(i) = self.state.selected() { 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(); return Transaction::new_empty();
} }

View File

@@ -161,7 +161,7 @@ where
} }
KeyCode::Esc => { KeyCode::Esc => {
app.current_screen = CurrentScreen::Main; app.current_screen = CurrentScreen::Main;
app.new_account = Account::new(0, String::new(), 0.0, AccountType::Cash); app.new_account = Account::new_empty();
} }
_ => {} _ => {}
}, },