From f9d276170030f8dda85e36c2211756261c1c68b0 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 22:05:22 +0300 Subject: [PATCH] cli: extract add subcommand --- src/cli.rs | 8 ++------ src/cli/add.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 14 ++++++-------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 363f2a8..5ee785e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,7 @@ //! along with tas. If not, see . //! +pub mod add; pub mod edit; pub mod finish; pub mod list; @@ -32,12 +33,7 @@ pub struct Args { #[derive(clap::Subcommand)] pub enum SubCommand { - Add { - #[clap(short, long)] - link: Option, - - name: String, - }, + Add(self::add::Args), Edit(self::edit::Args), Remove(self::remove::Args), Priority(self::priority::Args), diff --git a/src/cli/add.rs b/src/cli/add.rs index e69de29..879d63e 100644 --- a/src/cli/add.rs +++ b/src/cli/add.rs @@ -0,0 +1,27 @@ +use std::path::PathBuf; + +use crate::Task; + +#[derive(clap::Args)] +pub struct Args { + #[clap(short, long)] + link: Option, + + name: String, +} + +pub struct Request { + pub args: Args, + pub tasks: Vec, + pub tasks_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + tasks.push(Task { name, link }); + + let mut file = std::fs::File::create(&tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&tasks).unwrap()) + .unwrap(); + + println!("added"); +} diff --git a/src/main.rs b/src/main.rs index 83f671d..168c45b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,14 +52,12 @@ fn main() { .unwrap_or_default(); match args.command { - cli::SubCommand::Add { link, name } => { - tasks.push(Task { name, link }); - - let mut file = std::fs::File::create(&tasks_file_path).unwrap(); - file.write_all(&serde_json::to_vec(&tasks).unwrap()) - .unwrap(); - - println!("added"); + cli::SubCommand::Add(args) => { + cli::add::execute(cli::add::Request { + args, + tasks, + tasks_file_path, + }); } cli::SubCommand::Edit(args) => { cli::edit::execute(cli::edit::Request {