diff --git a/src/args/cmd/post.rs b/src/args/cmd/post.rs index c9dd8bc..a4e9a7e 100644 --- a/src/args/cmd/post.rs +++ b/src/args/cmd/post.rs @@ -60,6 +60,57 @@ pub struct Opt { pub cmd: Option, } +#[derive(Parser, Debug)] +pub struct CreateCmd { + #[arg(verbatim_doc_comment)] + /// Set post title + /// Example: cnb post create --title 'Title' --body 'Body' + #[arg(long)] + #[arg(value_name = "TITLE")] + pub title: String, + + #[arg(verbatim_doc_comment)] + /// Set post body + /// Example: cnb post create --title 'Title' --body 'Body' + #[arg(long)] + #[arg(value_name = "BODY")] + pub body: String, + + #[arg(verbatim_doc_comment)] + /// Set post status to publish + /// Example: cnb post create --title 'Title' --body 'Body' --publish + /// * + #[arg(long)] + #[arg(visible_alias = "pub")] + pub publish: bool, +} + +#[derive(Parser, Debug)] +pub struct UpdateCmd { + #[arg(verbatim_doc_comment)] + /// Set post title + /// Example: cnb --id 114514 post update --title 'Title' + #[arg(long)] + #[arg(value_name = "TITLE")] + pub title: Option, + + #[arg(verbatim_doc_comment)] + /// Set post body + /// Example: cnb --id 114514 post update --body 'Body' + #[arg(long)] + #[arg(value_name = "BODY")] + pub body: Option, + + #[arg(verbatim_doc_comment)] + /// Set post publish state + /// Example: cnb --id 114514 post update --publish true + /// * + #[arg(long)] + #[arg(value_name = "BOOL")] + #[arg(visible_alias = "pub")] + pub publish: Option, +} + #[derive(Debug, Subcommand)] pub enum Cmd { #[clap(verbatim_doc_comment)] @@ -67,57 +118,12 @@ pub enum Cmd { /// Example: cnb post create --title 'Title' --body 'Body' /// * #[clap(visible_alias = "c")] - Create { - #[arg(verbatim_doc_comment)] - /// Set post title - /// Example: cnb post create --title 'Title' --body 'Body' - #[arg(long)] - #[arg(value_name = "TITLE")] - title: String, - - #[arg(verbatim_doc_comment)] - /// Set post body - /// Example: cnb post create --title 'Title' --body 'Body' - #[arg(long)] - #[arg(value_name = "BODY")] - body: String, - - #[arg(verbatim_doc_comment)] - /// Set post status to publish - /// Example: cnb post create --title 'Title' --body 'Body' --publish - /// * - #[arg(long)] - #[arg(visible_alias = "pub")] - publish: bool, - }, + Create(CreateCmd), #[clap(verbatim_doc_comment)] /// Update post /// Example: cnb --id 114514 post update --title 'Title' /// You should also specify the id of the post via --id /// * #[clap(visible_alias = "u")] - Update { - #[arg(verbatim_doc_comment)] - /// Set post title - /// Example: cnb --id 114514 post update --title 'Title' - #[arg(long)] - #[arg(value_name = "TITLE")] - title: Option, - - #[arg(verbatim_doc_comment)] - /// Set post body - /// Example: cnb --id 114514 post update --body 'Body' - #[arg(long)] - #[arg(value_name = "BODY")] - body: Option, - - #[arg(verbatim_doc_comment)] - /// Set post publish state - /// Example: cnb --id 114514 post update --publish true - /// * - #[arg(long)] - #[arg(value_name = "BOOL")] - #[arg(visible_alias = "pub")] - publish: Option, - }, + Update(UpdateCmd), } diff --git a/src/args/mod.rs b/src/args/mod.rs index 2a69d14..006c8ea 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -17,7 +17,7 @@ pub enum TimeStyle { Normal, } -#[derive(Debug, Parser)] +#[derive(Parser, Debug)] pub struct GlobalOpt { #[arg(verbatim_doc_comment)] /// Execute with specific PAT @@ -80,7 +80,7 @@ pub struct GlobalOpt { pub quiet: bool, } -#[derive(Debug, Parser)] +#[derive(Parser, Debug)] #[command(author, about, long_about = None, version)] pub struct Args { #[command(subcommand)] diff --git a/src/args/parser/post.rs b/src/args/parser/post.rs index 77d98ab..68fd38f 100644 --- a/src/args/parser/post.rs +++ b/src/args/parser/post.rs @@ -1,3 +1,4 @@ +use crate::args::cmd::post::{CreateCmd, UpdateCmd}; use crate::args::parser::{get_skip, get_take}; use crate::args::{cmd, Args, Cmd}; use crate::infra::option::IntoOption; @@ -154,7 +155,7 @@ pub fn delete_post(args: &Args) -> Option { .into_some() } -pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> { +pub fn create_post(args: &Args) -> Option<&CreateCmd> { match args { Args { cmd: @@ -165,29 +166,20 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> { list: false, delete: false, search: None, - cmd: - Some(cmd::post::Cmd::Create { - title, - body, - publish, - }), + cmd: Some(cmd::post::Cmd::Create(cmd)), })), id: None, rev: _, skip: None, take: None, global_opt: _, - } => (title, body, *publish), + } => cmd, _ => return None, } .into_some() } -// TODO: fix warn -#[allow(clippy::type_complexity)] -pub fn update_post( - args: &Args, -) -> Option<(usize, &Option, &Option, &Option)> { +pub fn update_post(args: &Args) -> Option<(usize, &UpdateCmd)> { match args { Args { cmd: @@ -198,19 +190,14 @@ pub fn update_post( list: false, delete: false, search: None, - cmd: - Some(cmd::post::Cmd::Update { - title, - body, - publish, - }), + cmd: Some(cmd::post::Cmd::Update(cmd)), })), id: Some(id), rev: _, skip: None, take: None, global_opt: _, - } => (*id, title, body, publish), + } => (*id, cmd), _ => return None, } .into_some() diff --git a/src/main.rs b/src/main.rs index 4d6d5f6..568fccc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use crate::api::ing::Ing; use crate::api::news::News; use crate::api::post::Post; use crate::api::user::User; +use crate::args::cmd::post::{CreateCmd, UpdateCmd}; use crate::args::parser::no_operation; use crate::args::{parser, Args}; use crate::infra::fp::currying::eq; @@ -159,12 +160,14 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&result)); display::search_post(style, result)? } - _ if let Some((title, body, publish)) = parser::post::create_post(&args) => { - let id = Post::new(pat?).create(title, body, publish).await; + _ if let Some(create_cmd) = parser::post::create_post(&args) => { + let CreateCmd { title, body, publish } = create_cmd; + let id = Post::new(pat?).create(title, body, *publish).await; foe.then(|| panic_if_err(&id)); display::create_post(style, &id) } - _ if let Some((id, title, body, publish)) = parser::post::update_post(&args) => { + _ if let Some((id, update_cmd)) = parser::post::update_post(&args) => { + let UpdateCmd { title, body, publish } = update_cmd; let id = Post::new(pat?).update(id, title, body, publish).await; foe.then(|| panic_if_err(&id)); display::update_post(style, &id)