From ec880dad6e883d5c20567de9f42523db3e60d2bd Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 21:57:34 +0300 Subject: [PATCH] cli: extract edit subcommand --- src/cli.rs | 13 ++----------- src/cli/edit.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 36 +++++++--------------------------- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 7a23b26..363f2a8 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,7 @@ //! along with tas. If not, see . //! +pub mod edit; pub mod finish; pub mod list; pub mod pause; @@ -37,17 +38,7 @@ pub enum SubCommand { name: String, }, - Edit { - #[clap(short, long)] - link: Option, - #[clap(long)] - no_link: bool, - - #[clap(short, long)] - name: Option, - - idx: usize, - }, + Edit(self::edit::Args), Remove(self::remove::Args), Priority(self::priority::Args), List, diff --git a/src/cli/edit.rs b/src/cli/edit.rs index e69de29..43566b2 100644 --- a/src/cli/edit.rs +++ b/src/cli/edit.rs @@ -0,0 +1,51 @@ +use std::io::Write; +use std::path::PathBuf; + +use crate::{CurrentTaskInfo, Task}; + +#[derive(clap::Args)] +pub struct Args { + #[clap(short, long)] + link: Option, + #[clap(long)] + no_link: bool, + + #[clap(short, long)] + name: Option, + + idx: usize, +} + +pub struct Request { + pub args: Args, + pub current_task_info: Option, + pub tasks: Vec, + pub tasks_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + if req.current_task_info.is_some() { + panic!("You can edit task only when you don't have an active task, yet"); + } + + let idx = req.args.idx; + if idx == 0 || idx > req.tasks.len() { + panic!("invalid index"); + } + + let mut task = &mut req.tasks[idx - 1]; + if let Some(name) = req.args.name { + task.name = name; + } + if let Some(link) = req.args.link { + task.link = Some(link); + } else if req.args.no_link { + task.link = None; + } + + let mut file = std::fs::File::create(&req.tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.tasks).unwrap()) + .unwrap(); + + println!("changed"); +} diff --git a/src/main.rs b/src/main.rs index 1d140be..83f671d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,35 +61,13 @@ fn main() { println!("added"); } - cli::SubCommand::Edit { - idx, - name, - link, - no_link, - } => { - if current_task_info.is_some() { - panic!("You can edit task only when you don't have an active task, yet"); - } - - if idx == 0 || idx > tasks.len() { - panic!("invalid index"); - } - - let mut task = &mut tasks[idx - 1]; - if let Some(name) = name { - task.name = name; - } - if let Some(link) = link { - task.link = Some(link); - } else if no_link { - task.link = None; - } - - let mut file = std::fs::File::create(&tasks_file_path).unwrap(); - file.write_all(&serde_json::to_vec(&tasks).unwrap()) - .unwrap(); - - println!("changed"); + cli::SubCommand::Edit(args) => { + cli::edit::execute(cli::edit::Request { + args, + current_task_info, + tasks, + tasks_file_path, + }); } cli::SubCommand::Remove(args) => { cli::remove::execute(cli::remove::Request {