Extract funcs from input-check to reduce nesting and make pedantic clippy happy

This commit is contained in:
Joseph Ferano 2023-06-13 16:32:46 +07:00
parent c3255c97f5
commit c030984d51

View File

@ -3,20 +3,9 @@ use crate::db;
use crossterm::event; use crossterm::event;
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
/// # Errors pub fn handle_task_edit(state: &mut State<'_>, key: event::KeyEvent, mut task: TaskState<'_>) {
///
/// 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 project = &mut state.project;
let column = project.get_selected_column_mut(); let column = project.get_selected_column_mut();
if let Event::Key(key) = event::read()? {
match &mut state.task_edit_state {
Some(task) => {
// TODO: Extract this code to a separate function to avoid nesting
match task.focus { match task.focus {
// TODO: Handle wrapping around the enum rather than doing it manually // TODO: Handle wrapping around the enum rather than doing it manually
TaskEditFocus::Title => match key.code { TaskEditFocus::Title => match key.code {
@ -47,12 +36,7 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> {
db::update_task_text(&state.db_conn, selected_task); db::update_task_text(&state.db_conn, selected_task);
} }
} else { } else {
let task = db::insert_new_task( let task = db::insert_new_task(&state.db_conn, title, description, column);
&state.db_conn,
title,
description,
column,
);
column.add_task(task); column.add_task(task);
} }
state.task_edit_state = None; state.task_edit_state = None;
@ -69,7 +53,11 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> {
}, },
}; };
} }
None => match key.code {
pub fn handle_main(state: &mut State<'_>, key: event::KeyEvent) {
let project = &mut state.project;
let column = project.get_selected_column_mut();
match key.code {
KeyCode::Char('q') => state.quit = true, KeyCode::Char('q') => state.quit = true,
KeyCode::Char('h') | KeyCode::Left => { KeyCode::Char('h') | KeyCode::Left => {
project.select_previous_column(); project.select_previous_column();
@ -122,7 +110,22 @@ pub fn handle(state: &mut State<'_>) -> Result<(), std::io::Error> {
} }
} }
_ => {} _ => {}
}, }
}
/// # 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> {
if let Event::Key(key) = event::read()? {
if let Some(task) = state.task_edit_state.take() {
handle_task_edit(state, key, task);
} else {
handle_main(state, key);
} }
} }
Ok(()) Ok(())