Skip to content

Commit

Permalink
refactor: reduce type complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 27, 2023
1 parent b02ffe9 commit 6b34b02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 72 deletions.
100 changes: 53 additions & 47 deletions src/args/cmd/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,64 +60,70 @@ pub struct Opt {
pub cmd: Option<Cmd>,
}

#[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<String>,

#[arg(verbatim_doc_comment)]
/// Set post body
/// Example: cnb --id 114514 post update --body 'Body'
#[arg(long)]
#[arg(value_name = "BODY")]
pub body: Option<String>,

#[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<bool>,
}

#[derive(Debug, Subcommand)]
pub enum Cmd {
#[clap(verbatim_doc_comment)]
/// Create post
/// 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<String>,

#[arg(verbatim_doc_comment)]
/// Set post body
/// Example: cnb --id 114514 post update --body 'Body'
#[arg(long)]
#[arg(value_name = "BODY")]
body: Option<String>,

#[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<bool>,
},
Update(UpdateCmd),
}
4 changes: 2 additions & 2 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
27 changes: 7 additions & 20 deletions src/args/parser/post.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -154,7 +155,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
.into_some()
}

pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
pub fn create_post(args: &Args) -> Option<&CreateCmd> {
match args {
Args {
cmd:
Expand All @@ -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<String>, &Option<String>, &Option<bool>)> {
pub fn update_post(args: &Args) -> Option<(usize, &UpdateCmd)> {
match args {
Args {
cmd:
Expand All @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6b34b02

Please sign in to comment.