Skip to content

Commit 6b34b02

Browse files
committed
refactor: reduce type complexity
1 parent b02ffe9 commit 6b34b02

File tree

4 files changed

+68
-72
lines changed

4 files changed

+68
-72
lines changed

src/args/cmd/post.rs

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,64 +60,70 @@ pub struct Opt {
6060
pub cmd: Option<Cmd>,
6161
}
6262

63+
#[derive(Parser, Debug)]
64+
pub struct CreateCmd {
65+
#[arg(verbatim_doc_comment)]
66+
/// Set post title
67+
/// Example: cnb post create --title 'Title' --body 'Body'
68+
#[arg(long)]
69+
#[arg(value_name = "TITLE")]
70+
pub title: String,
71+
72+
#[arg(verbatim_doc_comment)]
73+
/// Set post body
74+
/// Example: cnb post create --title 'Title' --body 'Body'
75+
#[arg(long)]
76+
#[arg(value_name = "BODY")]
77+
pub body: String,
78+
79+
#[arg(verbatim_doc_comment)]
80+
/// Set post status to publish
81+
/// Example: cnb post create --title 'Title' --body 'Body' --publish
82+
/// *
83+
#[arg(long)]
84+
#[arg(visible_alias = "pub")]
85+
pub publish: bool,
86+
}
87+
88+
#[derive(Parser, Debug)]
89+
pub struct UpdateCmd {
90+
#[arg(verbatim_doc_comment)]
91+
/// Set post title
92+
/// Example: cnb --id 114514 post update --title 'Title'
93+
#[arg(long)]
94+
#[arg(value_name = "TITLE")]
95+
pub title: Option<String>,
96+
97+
#[arg(verbatim_doc_comment)]
98+
/// Set post body
99+
/// Example: cnb --id 114514 post update --body 'Body'
100+
#[arg(long)]
101+
#[arg(value_name = "BODY")]
102+
pub body: Option<String>,
103+
104+
#[arg(verbatim_doc_comment)]
105+
/// Set post publish state
106+
/// Example: cnb --id 114514 post update --publish true
107+
/// *
108+
#[arg(long)]
109+
#[arg(value_name = "BOOL")]
110+
#[arg(visible_alias = "pub")]
111+
pub publish: Option<bool>,
112+
}
113+
63114
#[derive(Debug, Subcommand)]
64115
pub enum Cmd {
65116
#[clap(verbatim_doc_comment)]
66117
/// Create post
67118
/// Example: cnb post create --title 'Title' --body 'Body'
68119
/// *
69120
#[clap(visible_alias = "c")]
70-
Create {
71-
#[arg(verbatim_doc_comment)]
72-
/// Set post title
73-
/// Example: cnb post create --title 'Title' --body 'Body'
74-
#[arg(long)]
75-
#[arg(value_name = "TITLE")]
76-
title: String,
77-
78-
#[arg(verbatim_doc_comment)]
79-
/// Set post body
80-
/// Example: cnb post create --title 'Title' --body 'Body'
81-
#[arg(long)]
82-
#[arg(value_name = "BODY")]
83-
body: String,
84-
85-
#[arg(verbatim_doc_comment)]
86-
/// Set post status to publish
87-
/// Example: cnb post create --title 'Title' --body 'Body' --publish
88-
/// *
89-
#[arg(long)]
90-
#[arg(visible_alias = "pub")]
91-
publish: bool,
92-
},
121+
Create(CreateCmd),
93122
#[clap(verbatim_doc_comment)]
94123
/// Update post
95124
/// Example: cnb --id 114514 post update --title 'Title'
96125
/// You should also specify the id of the post via --id
97126
/// *
98127
#[clap(visible_alias = "u")]
99-
Update {
100-
#[arg(verbatim_doc_comment)]
101-
/// Set post title
102-
/// Example: cnb --id 114514 post update --title 'Title'
103-
#[arg(long)]
104-
#[arg(value_name = "TITLE")]
105-
title: Option<String>,
106-
107-
#[arg(verbatim_doc_comment)]
108-
/// Set post body
109-
/// Example: cnb --id 114514 post update --body 'Body'
110-
#[arg(long)]
111-
#[arg(value_name = "BODY")]
112-
body: Option<String>,
113-
114-
#[arg(verbatim_doc_comment)]
115-
/// Set post publish state
116-
/// Example: cnb --id 114514 post update --publish true
117-
/// *
118-
#[arg(long)]
119-
#[arg(value_name = "BOOL")]
120-
#[arg(visible_alias = "pub")]
121-
publish: Option<bool>,
122-
},
128+
Update(UpdateCmd),
123129
}

src/args/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum TimeStyle {
1717
Normal,
1818
}
1919

20-
#[derive(Debug, Parser)]
20+
#[derive(Parser, Debug)]
2121
pub struct GlobalOpt {
2222
#[arg(verbatim_doc_comment)]
2323
/// Execute with specific PAT
@@ -80,7 +80,7 @@ pub struct GlobalOpt {
8080
pub quiet: bool,
8181
}
8282

83-
#[derive(Debug, Parser)]
83+
#[derive(Parser, Debug)]
8484
#[command(author, about, long_about = None, version)]
8585
pub struct Args {
8686
#[command(subcommand)]

src/args/parser/post.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::args::cmd::post::{CreateCmd, UpdateCmd};
12
use crate::args::parser::{get_skip, get_take};
23
use crate::args::{cmd, Args, Cmd};
34
use crate::infra::option::IntoOption;
@@ -154,7 +155,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
154155
.into_some()
155156
}
156157

157-
pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
158+
pub fn create_post(args: &Args) -> Option<&CreateCmd> {
158159
match args {
159160
Args {
160161
cmd:
@@ -165,29 +166,20 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
165166
list: false,
166167
delete: false,
167168
search: None,
168-
cmd:
169-
Some(cmd::post::Cmd::Create {
170-
title,
171-
body,
172-
publish,
173-
}),
169+
cmd: Some(cmd::post::Cmd::Create(cmd)),
174170
})),
175171
id: None,
176172
rev: _,
177173
skip: None,
178174
take: None,
179175
global_opt: _,
180-
} => (title, body, *publish),
176+
} => cmd,
181177
_ => return None,
182178
}
183179
.into_some()
184180
}
185181

186-
// TODO: fix warn
187-
#[allow(clippy::type_complexity)]
188-
pub fn update_post(
189-
args: &Args,
190-
) -> Option<(usize, &Option<String>, &Option<String>, &Option<bool>)> {
182+
pub fn update_post(args: &Args) -> Option<(usize, &UpdateCmd)> {
191183
match args {
192184
Args {
193185
cmd:
@@ -198,19 +190,14 @@ pub fn update_post(
198190
list: false,
199191
delete: false,
200192
search: None,
201-
cmd:
202-
Some(cmd::post::Cmd::Update {
203-
title,
204-
body,
205-
publish,
206-
}),
193+
cmd: Some(cmd::post::Cmd::Update(cmd)),
207194
})),
208195
id: Some(id),
209196
rev: _,
210197
skip: None,
211198
take: None,
212199
global_opt: _,
213-
} => (*id, title, body, publish),
200+
} => (*id, cmd),
214201
_ => return None,
215202
}
216203
.into_some()

src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::api::ing::Ing;
1212
use crate::api::news::News;
1313
use crate::api::post::Post;
1414
use crate::api::user::User;
15+
use crate::args::cmd::post::{CreateCmd, UpdateCmd};
1516
use crate::args::parser::no_operation;
1617
use crate::args::{parser, Args};
1718
use crate::infra::fp::currying::eq;
@@ -159,12 +160,14 @@ async fn main() -> Result<()> {
159160
foe.then(|| panic_if_err(&result));
160161
display::search_post(style, result)?
161162
}
162-
_ if let Some((title, body, publish)) = parser::post::create_post(&args) => {
163-
let id = Post::new(pat?).create(title, body, publish).await;
163+
_ if let Some(create_cmd) = parser::post::create_post(&args) => {
164+
let CreateCmd { title, body, publish } = create_cmd;
165+
let id = Post::new(pat?).create(title, body, *publish).await;
164166
foe.then(|| panic_if_err(&id));
165167
display::create_post(style, &id)
166168
}
167-
_ if let Some((id, title, body, publish)) = parser::post::update_post(&args) => {
169+
_ if let Some((id, update_cmd)) = parser::post::update_post(&args) => {
170+
let UpdateCmd { title, body, publish } = update_cmd;
168171
let id = Post::new(pat?).update(id, title, body, publish).await;
169172
foe.then(|| panic_if_err(&id));
170173
display::update_post(style, &id)

0 commit comments

Comments
 (0)