diff --git a/src/app.rs b/src/app.rs index fbbcd8b..88f2210 100644 --- a/src/app.rs +++ b/src/app.rs @@ -97,3 +97,9 @@ impl<'a> States<'a> { } } } + +impl<'a> Default for States<'a> { + fn default() -> Self { + Self::new() + } +} diff --git a/src/db/connection.rs b/src/db/connection.rs index 2db7dc8..27968a6 100644 --- a/src/db/connection.rs +++ b/src/db/connection.rs @@ -42,7 +42,7 @@ impl DB { transactions.append(&mut temp_transactions); } - return Ok(()); + Ok(()) } // @@ -64,7 +64,7 @@ impl DB { transactions.append(&mut temp_transactions); } - return Ok(()); + Ok(()) } pub async fn get_all_buckets(&mut self) -> Result<(), Error> { @@ -85,6 +85,6 @@ impl DB { transactions.append(&mut temp_transactions); } - return Ok(()); + Ok(()) } } diff --git a/src/db/data_cache.rs b/src/db/data_cache.rs index f482d25..5e0ae88 100644 --- a/src/db/data_cache.rs +++ b/src/db/data_cache.rs @@ -35,6 +35,12 @@ impl DataCache { } } } - return None; + None + } +} + +impl Default for DataCache { + fn default() -> Self { + Self::new() } } diff --git a/src/db/tables.rs b/src/db/tables.rs index c455a1f..42fafbb 100644 --- a/src/db/tables.rs +++ b/src/db/tables.rs @@ -1,3 +1,4 @@ +use core::fmt; use std::borrow::Cow; use sqlx::{postgres::types::PgMoney, FromRow}; @@ -76,19 +77,21 @@ pub struct PartialTransaction { impl Transaction { pub fn get_header() -> String { - return format!(" {:<7} | {:<9} | {:<10}", "Id", "Amount", "Date"); - } - - pub fn to_string(&self) -> String { - return format!( - " T{:0>6} | {:>9} | {:>10}", - self.trns_id, - self.trns_amount.to_decimal(2).to_string(), - self.trns_date.to_string() - ); + format!(" {:<7} | {:<9} | {:<10}", "Id", "Amount", "Date") } pub fn as_str(&self) -> Cow { - return self.to_string().into(); + self.to_string().into() } } + +impl fmt::Display for Transaction { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, " T{:0>6} | {:>9} | {:>10}", + self.trns_id, + self.trns_amount.to_decimal(2).to_string(), + self.trns_date.to_string() + ) + } +} + diff --git a/src/main.rs b/src/main.rs index 93d45d1..f6390ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,29 +55,26 @@ pub fn poll_events(app: &mut App) -> AppResult<()> { // 'q' needs to be handled at the top level so it can't be // accidentally handed to a dead end if let KeyCode::Char('q') = key.code { - if let Some(_) = app.states.nav_state.message.clone() { + if app.states.nav_state.message.is_some() { app.states.nav_state.message = None; + } else if let ActiveFrame::Navigation = app.states.active_frame { + app.running = false; } else { - if let ActiveFrame::Navigation = app.states.active_frame { - app.running = false; - } else { - app.states.active_frame = ActiveFrame::Navigation; - } + app.states.active_frame = ActiveFrame::Navigation; } } else if let KeyCode::Esc = key.code { - if let Some(_) = app.states.nav_state.message.clone() { + if app.states.nav_state.message.is_some() { app.states.nav_state.message = None; + } else if let ActiveFrame::Navigation = app.states.active_frame { + app.running = false; } else { - if let ActiveFrame::Navigation = app.states.active_frame { - app.running = false; - } else { - app.states.active_frame = ActiveFrame::Navigation; - } + app.states.active_frame = ActiveFrame::Navigation; } } if let KeyCode::Char('r') = key.code { app.refresh(); } + match app.states.active_frame { ActiveFrame::Navigation => { NavigationState::handle_event(key, app); @@ -93,7 +90,7 @@ pub fn poll_events(app: &mut App) -> AppResult<()> { } } } - return Ok(()); + Ok(()) } pub fn init_logger(output_file: String) { diff --git a/src/ui.rs b/src/ui.rs index e20c78f..e75b9ba 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,8 +1,5 @@ -use crate::{ - app::{ActiveFrame, App}, - uis::{render_history_tab, render_manual_transaction}, -}; -use log; +// use log; + use ratatui::{ backend::Backend, layout::{Constraint, Direction, Layout}, @@ -11,7 +8,10 @@ use ratatui::{ Frame, }; -use crate::uis::{render_navigation_frame, render_quick_transaction}; +use crate::{ + app::{ActiveFrame, App}, + uis::{render_history_tab, render_manual_transaction, render_navigation_frame, render_quick_transaction}, +}; pub fn render(f: &mut Frame, app: &mut App) { let size = f.size(); diff --git a/src/uis/history.rs b/src/uis/history.rs index 918f2bd..4ceaf18 100644 --- a/src/uis/history.rs +++ b/src/uis/history.rs @@ -55,6 +55,12 @@ impl HistoryState { } } +impl Default for HistoryState { + fn default() -> Self { + Self::new() + } +} + pub fn render_history_tab(f: &mut Frame, body_rect: Rect, app: &mut App) { let split_body = Layout::default() .direction(Direction::Horizontal) @@ -78,14 +84,14 @@ pub fn render_history_tab(f: &mut Frame, body_rect: Rect, app: &m lines.push(Line::from(vec![ Span::styled("Amount: ", ident_style), Span::styled( - format!("{}", selected_item.trns_amount.to_decimal(2).to_string()), + selected_item.trns_amount.to_decimal(2).to_string(), value_style, ), ])); lines.push(Line::from(vec![ Span::styled("Transaction Date: ", ident_style), Span::styled( - format!("{}", selected_item.trns_date.to_string()), + selected_item.trns_date.to_string(), value_style, ), ])); diff --git a/src/uis/manual_transaction.rs b/src/uis/manual_transaction.rs index ac7f261..f62bf5b 100644 --- a/src/uis/manual_transaction.rs +++ b/src/uis/manual_transaction.rs @@ -14,7 +14,7 @@ use crate::{ }; use chrono::prelude::Local; -use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; +use crossterm::event::{KeyCode, KeyEvent}; pub struct ManualTransactionState { pub focus: Option, @@ -95,7 +95,7 @@ impl ManualTransactionState { } ManualDataFocus::Date => { if let Some(ref mut x) = app.states.manual_transactions.date { - if x.len() == 0 { + if x.is_empty() { x.push_str(&format!("+{}", Local::now().format("%m-%d-%Y"))); } } @@ -158,7 +158,7 @@ impl ManualTransactionState { }, KeyCode::Char(value) => match app.states.manual_transactions.focus.unwrap() { ManualDataFocus::Amount => { - if value.is_digit(10) || value == '.' { + if value.is_ascii_digit() || value == '.' { if let Some(ref mut s) = app.states.manual_transactions.amount { s.push(value); } else { @@ -174,7 +174,7 @@ impl ManualTransactionState { } } ManualDataFocus::Date => { - if value.is_digit(10) { + if value.is_ascii_digit() { if let Some(ref mut s) = app.states.manual_transactions.date { if s.len() < 10 { s.push(value); @@ -193,26 +193,26 @@ impl ManualTransactionState { } if let Some(ManualDataFocus::Date) = app.states.manual_transactions.focus { if let Some(ref mut x) = app.states.manual_transactions.date { - if x.starts_with("+") { + if x.starts_with('+') { x.clear(); } } } - } else { - match event.code { - KeyCode::Enter => { - app.states.manual_transactions.focus = Some(ManualDataFocus::Account); - } - _ => {} - }; + } else if event.code == KeyCode::Enter{ + app.states.manual_transactions.focus = Some(ManualDataFocus::Account); } } } +impl Default for ManualTransactionState { + fn default() -> Self { + Self::new() + } +} + + pub fn render_manual_transaction(f: &mut Frame, body_rect: Rect, app: &App) { // Render the custom tab bar - let mut constraints: Vec = vec![]; - let constraints: Vec = vec![ Constraint::Length(1), Constraint::Length(1), // account diff --git a/src/uis/navigation_frame.rs b/src/uis/navigation_frame.rs index 5227575..47b6aa4 100644 --- a/src/uis/navigation_frame.rs +++ b/src/uis/navigation_frame.rs @@ -63,6 +63,12 @@ impl<'a> NavigationState<'a> { } } +impl<'a> Default for NavigationState<'a> { + fn default() -> Self { + Self::new() + } +} + pub fn render_navigation_frame( f: &mut Frame, status_rect: Rect, @@ -114,7 +120,7 @@ pub fn render_navigation_frame( // Navbar section let navbar = { - if let None = app.states.nav_state.message { + if app.states.nav_state.message.is_none() { match app.states.active_frame { ActiveFrame::Navigation => "Navigating: q/Esc to exit".to_string(), _ => "Editing Body: q/Esc to go back to navigating".to_string(), diff --git a/src/uis/quick_transaction.rs b/src/uis/quick_transaction.rs index 2c73482..5474b52 100644 --- a/src/uis/quick_transaction.rs +++ b/src/uis/quick_transaction.rs @@ -27,7 +27,7 @@ impl<'a> QuickTransactionState<'a> { } pub fn handle_event(event: KeyEvent, app: &mut App) { - let transact_state = &app.states.transactions; + // let transact_state = &app.states.transactions; if event.kind == KeyEventKind::Press { match event.code { @@ -38,6 +38,12 @@ impl<'a> QuickTransactionState<'a> { } } +impl<'a> Default for QuickTransactionState<'a> { + fn default() -> Self { + Self::new() + } +} + pub fn render_quick_transaction(f: &mut Frame, body_rect: Rect, app: &App) { let chunks = Layout::default() .direction(Direction::Vertical)