diff --git a/src/cli.rs b/src/cli.rs index 8f55086..94161e6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -16,6 +16,7 @@ pub mod finish; pub mod pause; +pub mod priority; pub mod start; pub mod status; @@ -48,20 +49,10 @@ pub enum SubCommand { Remove { idx: usize, }, - Priority { - idx: usize, - #[clap(subcommand)] - priority: Priority, - }, + Priority(self::priority::Args), List, Start(self::start::Args), Pause, Finish, Status, } - -#[derive(clap::Subcommand)] -pub enum Priority { - Before { idx: usize }, - After { idx: usize }, -} diff --git a/src/cli/priority.rs b/src/cli/priority.rs index e69de29..2382702 100644 --- a/src/cli/priority.rs +++ b/src/cli/priority.rs @@ -0,0 +1,62 @@ +use crate::{CurrentTaskInfo, Task}; + +use std::cmp::Ordering; +use std::io::Write; +use std::path::PathBuf; + +#[derive(clap::Args)] +pub struct Args { + idx: usize, + #[clap(subcommand)] + priority: Priority, +} + +#[derive(clap::Subcommand)] +pub enum Priority { + Before { idx: usize }, + After { idx: usize }, +} + +pub struct Request { + pub args: Args, + pub tasks: Vec, + pub current_task_info: Option, + pub tasks_file_path: PathBuf, +} + +pub fn execute(req: Request) { + if req.current_task_info.is_some() { + panic!("You can change priority only when you don't have an active task, yet"); + } + + let target_idx = req.args.idx; + if target_idx == 0 || target_idx > req.tasks.len() { + panic!("invalid index"); + } + + let idx = match req.args.priority { + Priority::Before { idx } | Priority::After { idx } => match target_idx.cmp(&idx) { + Ordering::Equal => return, + Ordering::Less => idx - 1, + Ordering::Greater => idx, + }, + }; + if idx == 0 || idx > req.tasks.len() { + panic!("invalid index"); + } + + let target = req.tasks.remove(target_idx - 1); + + match req.args.priority { + Priority::Before { .. } => { + req.tasks.insert(idx - 1, target); + } + Priority::After { .. } => { + req.tasks.insert(idx, target); + } + } + + let mut file = std::fs::File::create(&req.tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.tasks).unwrap()) + .unwrap(); +} diff --git a/src/main.rs b/src/main.rs index b3eb303..200a5f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,64 +148,40 @@ fn main() { println!("{}", task.name); } } - cli::SubCommand::Priority { - idx: target_idx, - priority, - } => { - if current_task_info.is_some() { - panic!("You can change priority only when you don't have an active task, yet"); - } - - if target_idx == 0 || target_idx > tasks.len() { - panic!("invalid index"); - } - - let idx = match priority { - cli::Priority::Before { idx } | cli::Priority::After { idx } => { - match target_idx.cmp(&idx) { - Ordering::Equal => return, - Ordering::Less => idx - 1, - Ordering::Greater => idx, - } - } - }; - if idx == 0 || idx > tasks.len() { - panic!("invalid index"); - } - - let target = tasks.remove(target_idx - 1); - - match priority { - cli::Priority::Before { .. } => { - tasks.insert(idx - 1, target); - } - cli::Priority::After { .. } => { - tasks.insert(idx, target); - } - } - - let mut file = std::fs::File::create(&tasks_file_path).unwrap(); - file.write_all(&serde_json::to_vec(&tasks).unwrap()) - .unwrap(); + cli::SubCommand::Priority(args) => { + cli::priority::execute(cli::priority::Request { + args, + tasks, + tasks_file_path, + current_task_info, + }); + } + cli::SubCommand::Start(args) => { + cli::start::execute(cli::start::Request { + args, + tasks, + current_task_info, + current_task_info_file_path, + }); + } + cli::SubCommand::Pause => { + cli::pause::execute(cli::pause::Request { + current_task_info, + current_task_info_file_path, + }); + } + cli::SubCommand::Finish => { + cli::finish::execute(cli::finish::Request { + tasks, + current_task_info, + current_task_info_file_path, + tasks_file_path, + finished_tasks_file_path, + }); + } + cli::SubCommand::Status => { + cli::status::execute(current_task_info); } - cli::SubCommand::Start(args) => cli::start::execute(cli::start::Request { - args, - tasks, - current_task_info, - current_task_info_file_path, - }), - cli::SubCommand::Pause => cli::pause::execute(cli::pause::Request { - current_task_info, - current_task_info_file_path, - }), - cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request { - tasks, - current_task_info, - current_task_info_file_path, - tasks_file_path, - finished_tasks_file_path, - }), - cli::SubCommand::Status => cli::status::execute(current_task_info), } }