From c3255c97f5948d0852adaca47fd81b4a1dcdd07d Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 13 Jun 2023 15:59:24 +0700 Subject: [PATCH] Pedantic clippy pass --- src/app.rs | 13 ++++++++----- src/db.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- src/input.rs | 18 +++++++++++------- src/main.rs | 4 ++-- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index eb38e89..1c9b884 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,7 @@ // use int_enum::IntEnum; use rusqlite::Connection; use serde::{Deserialize, Serialize}; -use std::cmp::min; +use std::{cmp::min, error::Error}; use tui_textarea::TextArea; use crate::db; @@ -29,7 +29,6 @@ pub struct Task { #[derive(Deserialize, Serialize, Debug)] pub struct Project { pub name: String, - pub filepath: String, pub selected_column_idx: usize, pub columns: Vec, } @@ -182,12 +181,16 @@ impl<'a> Column { } impl Project { - pub async fn load(pool: &Connection) -> Result { - let columns = db::get_all_columns(&pool).unwrap(); + /// . + /// + /// # Errors + /// + /// This function will return an error if it has issues reading from the database + pub fn load(conn: &Connection) -> Result> { + let columns = db::get_all_columns(conn)?; Ok(Project { name: String::from("Kanban Board"), - filepath: String::from("path"), columns, selected_column_idx: 0, }) diff --git a/src/db.rs b/src/db.rs index 6014a05..410abbc 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,11 @@ use crate::{Column, Task}; use rusqlite::{params, Connection, Result}; +/// . +/// +/// # Errors +/// +/// This function will return an error if something is wrong with the SQL pub fn get_tasks_by_column(conn: &Connection, column_name: &String) -> Result> { let mut stmt = conn.prepare( r#" @@ -11,7 +16,7 @@ pub fn get_tasks_by_column(conn: &Connection, column_name: &String) -> Result Result Result> { let mut stmt = conn.prepare("select id, name from kb_column")?; let query_rows = stmt.query_map((), |row| { @@ -33,11 +43,11 @@ pub fn get_all_columns(conn: &Connection) -> Result> { for row in query_rows { let r = &row?; let name = &r.1; - let tasks = get_tasks_by_column(conn, &name).unwrap(); + let tasks = get_tasks_by_column(conn, name)?; let col = Column { id: r.0, name: name.clone(), - tasks: tasks, + tasks, selected_task_idx: 0, }; columns.push(col); @@ -45,6 +55,11 @@ pub fn get_all_columns(conn: &Connection) -> Result> { Ok(columns) } +/// . +/// +/// # Panics +/// +/// Panics if something goes wrong with the SQL pub fn insert_new_task( conn: &Connection, title: String, @@ -64,11 +79,21 @@ pub fn insert_new_task( } } +/// . +/// +/// # Panics +/// +/// Panics if something goes wrong with the SQL pub fn delete_task(conn: &Connection, task: &Task) { let mut stmt = conn.prepare("delete from task where id = ?1").unwrap(); stmt.execute([task.id]).unwrap(); } +/// . +/// +/// # Panics +/// +/// Panics if something goes wrong with the SQL pub fn update_task_text(conn: &Connection, task: &Task) { let mut stmt = conn .prepare("update task set title = ?2, description = ?3 where id = ?1") @@ -77,6 +102,11 @@ pub fn update_task_text(conn: &Connection, task: &Task) { .unwrap(); } +/// . +/// +/// # Panics +/// +/// Panics if something goes wrong with the SQL pub fn move_task_to_column(conn: &Connection, task: &Task, target_column: &Column) { let mut stmt = conn .prepare( @@ -93,12 +123,17 @@ pub fn move_task_to_column(conn: &Connection, task: &Task, target_column: &Colum stmt.execute((&task.id, &target_column.id)).unwrap(); } +/// . +/// +/// # Panics +/// +/// Panics if something goes wrong with the SQL pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) { let tx = conn.transaction().unwrap(); tx.execute( "create temp table temp_order as select sort_order from task where id = ?1", - &[&task1.id], + [&task1.id], ) .unwrap(); @@ -111,7 +146,7 @@ pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) { tx.execute( "update task set sort_order = (select sort_order from temp_order) where id = ?1", - &[&task2.id], + [&task2.id], ) .unwrap(); tx.execute("drop table temp_order", ()).unwrap(); diff --git a/src/input.rs b/src/input.rs index 5982773..e34f9b4 100644 --- a/src/input.rs +++ b/src/input.rs @@ -6,6 +6,10 @@ use crossterm::event::{Event, KeyCode}; /// # Errors /// /// Crossterm `event::read()` might return an error +/// +/// # Panics +/// +/// Shouldn't really panic because there are checks to ensure we can unwrap safely pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> { let project = &mut state.project; let column = project.get_selected_column_mut(); @@ -40,14 +44,14 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> { if let Some(selected_task) = column.get_selected_task_mut() { selected_task.title = title; selected_task.description = description; - db::update_task_text(&state.db_conn, &selected_task); + db::update_task_text(&state.db_conn, selected_task); } } else { let task = db::insert_new_task( &state.db_conn, title, description, - &column, + column, ); column.add_task(task); } @@ -82,7 +86,7 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> { project.move_task_previous_column(); let col = project.get_selected_column(); let t = col.get_selected_task().unwrap(); - db::move_task_to_column(&state.db_conn, &t, &col); + db::move_task_to_column(&state.db_conn, t, col); } } KeyCode::Char('L') => { @@ -90,26 +94,26 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> { project.move_task_next_column(); let col = project.get_selected_column(); let t = col.get_selected_task().unwrap(); - db::move_task_to_column(&state.db_conn, &t, &col); + db::move_task_to_column(&state.db_conn, t, col); } } KeyCode::Char('J') => { if column.move_task_down() { let task1 = column.get_selected_task().unwrap(); let task2 = column.get_previous_task().unwrap(); - db::swap_task_order(&mut state.db_conn, &task1, &task2); + db::swap_task_order(&mut state.db_conn, task1, task2); } } KeyCode::Char('K') => { if column.move_task_up() { let task1 = column.get_selected_task().unwrap(); let task2 = column.get_next_task().unwrap(); - db::swap_task_order(&mut state.db_conn, &task1, &task2); + db::swap_task_order(&mut state.db_conn, task1, task2); } } KeyCode::Char('n') => state.task_edit_state = Some(TaskState::default()), KeyCode::Char('e') => { - state.task_edit_state = column.get_task_state_from_curr_selected_task() + state.task_edit_state = column.get_task_state_from_curr_selected_task(); } KeyCode::Char('D') => { if !column.tasks.is_empty() { diff --git a/src/main.rs b/src/main.rs index 1736a1e..35585a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ async fn main() -> anyhow::Result<(), Box> { if migrate { let migrations = include_str!("../sql/migrations.sql"); - let migrations: Vec<&str> = migrations.split(";").collect(); + let migrations: Vec<&str> = migrations.split(';').collect(); let tx = conn.transaction()?; for m in migrations { if !m.trim().is_empty() { @@ -42,7 +42,7 @@ async fn main() -> anyhow::Result<(), Box> { tx.commit()?; } - let project = Project::load(&conn).await?; + let project = Project::load(&conn)?; let mut state = State::new(conn, project); enable_raw_mode()?;