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::{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> {
pub fn handle_task_edit(state: &mut State<'_>, key: event::KeyEvent, mut task: TaskState<'_>) {
let project = &mut state.project;
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 {
// TODO: Handle wrapping around the enum rather than doing it manually
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);
}
} else {
let task = db::insert_new_task(
&state.db_conn,
title,
description,
column,
);
let task = db::insert_new_task(&state.db_conn, title, description, column);
column.add_task(task);
}
state.task_edit_state = None;
@ -68,8 +52,12 @@ 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('h') | KeyCode::Left => {
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(())