DRY + error handling
This commit is contained in:
236
src/app.rs
236
src/app.rs
@@ -154,135 +154,27 @@ impl App {
|
||||
}
|
||||
|
||||
pub fn next_tr_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
self.selected_transaction_input = TransactionInput::Amount;
|
||||
self.new_transaction.set_type(
|
||||
self.current_input
|
||||
.parse::<i64>()
|
||||
.expect("Failed to parse data"),
|
||||
&self.connection,
|
||||
);
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
self.selected_transaction_input = TransactionInput::Date;
|
||||
self.new_transaction.set_amount(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
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_or_default(); // to account for my local datetime
|
||||
self.new_transaction
|
||||
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
||||
}
|
||||
TransactionInput::Description => {
|
||||
self.selected_transaction_input = TransactionInput::Asset;
|
||||
self.new_transaction.set_desc(self.current_input.clone())
|
||||
}
|
||||
TransactionInput::Asset => {
|
||||
self.new_transaction.set_asset_amnt(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
}
|
||||
self.set_tr_from_input();
|
||||
self.selected_transaction_input = match self.selected_transaction_input {
|
||||
TransactionInput::Type => TransactionInput::Amount,
|
||||
TransactionInput::Amount => TransactionInput::Date,
|
||||
TransactionInput::Date => TransactionInput::Description,
|
||||
TransactionInput::Description => TransactionInput::Asset,
|
||||
TransactionInput::Asset => TransactionInput::Asset,
|
||||
};
|
||||
self.set_current_input_tr();
|
||||
}
|
||||
pub fn previous_tr_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
self.new_transaction.set_type(
|
||||
self.current_input
|
||||
.parse::<i64>()
|
||||
.expect("Failed to parse data"),
|
||||
&self.connection,
|
||||
);
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
self.selected_transaction_input = TransactionInput::Type;
|
||||
self.new_transaction.set_amount(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
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_or_default(); // to account for my local datetime
|
||||
self.new_transaction
|
||||
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
||||
}
|
||||
TransactionInput::Description => {
|
||||
self.selected_transaction_input = TransactionInput::Date;
|
||||
self.new_transaction.set_desc(self.current_input.clone());
|
||||
}
|
||||
TransactionInput::Asset => {
|
||||
self.selected_transaction_input = TransactionInput::Description;
|
||||
self.new_transaction.set_asset_amnt(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
}
|
||||
self.set_tr_from_input();
|
||||
self.selected_transaction_input = match self.selected_transaction_input {
|
||||
TransactionInput::Type => TransactionInput::Type,
|
||||
TransactionInput::Amount => TransactionInput::Type,
|
||||
TransactionInput::Date => TransactionInput::Amount,
|
||||
TransactionInput::Description => TransactionInput::Date,
|
||||
TransactionInput::Asset => TransactionInput::Description,
|
||||
};
|
||||
self.set_current_input_tr();
|
||||
}
|
||||
pub fn next_ac_input(&mut self) {
|
||||
match self.selected_account_input {
|
||||
AccountInput::Asset => {
|
||||
self.selected_account_input = AccountInput::Name;
|
||||
self.new_account.set_asset_price(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
AccountInput::Name => {
|
||||
self.selected_account_input = AccountInput::Type;
|
||||
self.new_account.set_name(self.current_input.clone());
|
||||
}
|
||||
AccountInput::Type => {
|
||||
self.new_account.set_type(AccountType::from_usize(
|
||||
self.current_input
|
||||
.parse::<usize>()
|
||||
.expect("Failed to parse data"),
|
||||
));
|
||||
}
|
||||
}
|
||||
self.set_current_input_ac();
|
||||
}
|
||||
pub fn previous_ac_input(&mut self) {
|
||||
match self.selected_account_input {
|
||||
AccountInput::Asset => {
|
||||
self.new_account.set_asset_price(
|
||||
self.current_input
|
||||
.parse::<f64>()
|
||||
.expect("Failed to parse data"),
|
||||
);
|
||||
}
|
||||
AccountInput::Name => {
|
||||
self.new_account.set_name(self.current_input.clone());
|
||||
self.selected_account_input = AccountInput::Asset;
|
||||
}
|
||||
AccountInput::Type => {
|
||||
self.new_account.set_type(AccountType::from_usize(
|
||||
self.current_input
|
||||
.parse::<usize>()
|
||||
.expect("Failed to parse data"),
|
||||
));
|
||||
self.selected_account_input = AccountInput::Name;
|
||||
}
|
||||
}
|
||||
self.set_current_input_ac();
|
||||
}
|
||||
pub fn set_current_input_tr(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
@@ -304,6 +196,74 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn set_tr_from_input(&mut self) {
|
||||
match self.selected_transaction_input {
|
||||
TransactionInput::Type => {
|
||||
let type_id = match self.current_input.parse::<i64>() {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
0
|
||||
}
|
||||
};
|
||||
self.new_transaction.set_type(type_id, &self.connection);
|
||||
}
|
||||
TransactionInput::Amount => {
|
||||
let amnt = match self.current_input.parse::<f64>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
0.0
|
||||
}
|
||||
};
|
||||
self.new_transaction.set_amount(amnt);
|
||||
}
|
||||
TransactionInput::Date => {
|
||||
let date = match NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d") {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
NaiveDate::default()
|
||||
}
|
||||
};
|
||||
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)));
|
||||
}
|
||||
TransactionInput::Description => {
|
||||
self.new_transaction.set_desc(self.current_input.clone());
|
||||
}
|
||||
TransactionInput::Asset => {
|
||||
let ast = match self.current_input.parse::<f64>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
0.0
|
||||
}
|
||||
};
|
||||
self.new_transaction.set_asset_amnt(ast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_ac_input(&mut self) {
|
||||
self.set_ac_from_input();
|
||||
self.selected_account_input = match self.selected_account_input {
|
||||
AccountInput::Asset => AccountInput::Name,
|
||||
AccountInput::Name => AccountInput::Type,
|
||||
AccountInput::Type => AccountInput::Type,
|
||||
};
|
||||
self.set_current_input_ac();
|
||||
}
|
||||
pub fn previous_ac_input(&mut self) {
|
||||
self.set_ac_from_input();
|
||||
self.selected_account_input = match self.selected_account_input {
|
||||
AccountInput::Asset => AccountInput::Asset,
|
||||
AccountInput::Name => AccountInput::Asset,
|
||||
AccountInput::Type => AccountInput::Name,
|
||||
};
|
||||
self.set_current_input_ac();
|
||||
}
|
||||
pub fn set_current_input_ac(&mut self) {
|
||||
match self.selected_account_input {
|
||||
AccountInput::Asset => {
|
||||
@@ -318,6 +278,34 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn set_ac_from_input(&mut self) {
|
||||
match self.selected_account_input {
|
||||
AccountInput::Asset => {
|
||||
let ast = match self.current_input.parse::<f64>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
0.0
|
||||
}
|
||||
};
|
||||
self.new_account.set_asset_price(ast);
|
||||
}
|
||||
AccountInput::Name => {
|
||||
self.new_account.set_name(self.current_input.clone());
|
||||
}
|
||||
AccountInput::Type => {
|
||||
let ac_type = match self.current_input.parse::<usize>() {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
self.error = e.to_string();
|
||||
0
|
||||
}
|
||||
};
|
||||
self.new_account.set_type(AccountType::from_usize(ac_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_tab(&mut self) {
|
||||
if self.selected_tab < self.tabs.len() {
|
||||
self.selected_tab += 1;
|
||||
|
||||
@@ -68,6 +68,7 @@ where
|
||||
{
|
||||
app.error = String::new();
|
||||
app.error_showing = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
match app.current_screen {
|
||||
|
||||
Reference in New Issue
Block a user