Skip to content

Commit 9b8f093

Browse files
committed
feat!: better err handle
1 parent 6f18164 commit 9b8f093

File tree

8 files changed

+332
-218
lines changed

8 files changed

+332
-218
lines changed

src/api/user/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::openapi;
66
use anyhow::Result;
77
use serde::{Deserialize, Serialize};
88

9-
#[derive(Serialize, Deserialize, Debug)]
9+
#[derive(Serialize, Deserialize, Debug, Clone)]
1010
pub struct UserInfo {
1111
#[serde(rename = "UserId")]
1212
pub user_id: String,

src/args/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ pub struct Args {
2323
#[arg(long)]
2424
pub id: Option<usize>,
2525

26-
#[arg(verbatim_doc_comment)]
27-
/// Execute with specific PAT
28-
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
29-
/// Your PAT in ~/.cnbrc will be ignored in this execution if it exists
30-
/// Please login if you don't want to input PAT everytime, try 'cnb user --help' for more details
31-
#[arg(long)]
32-
#[arg(value_name = "PAT")]
33-
pub with_pat: Option<String>,
34-
3526
#[arg(verbatim_doc_comment)]
3627
/// Reverse list output
3728
/// Example: cnb --rev ing --list
@@ -60,6 +51,15 @@ pub struct Args {
6051
#[arg(value_name = "LENGTH")]
6152
pub take: Option<usize>,
6253

54+
#[arg(verbatim_doc_comment)]
55+
/// Execute with specific PAT
56+
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
57+
/// Your PAT in ~/.cnbrc will be ignored in this execution if it exists
58+
/// Please login if you don't want to input PAT everytime, try 'cnb user --help' for more details
59+
#[arg(long)]
60+
#[arg(value_name = "PAT")]
61+
pub with_pat: Option<String>,
62+
6363
#[arg(verbatim_doc_comment)]
6464
/// Execute in debug mode, this will print some messages for the developer
6565
/// Example: cnb --debug ing --list
@@ -74,7 +74,15 @@ pub struct Args {
7474
#[arg(long)]
7575
#[arg(value_enum)]
7676
#[arg(hide_possible_values = true)]
77-
#[arg(default_value = "colorful")]
77+
#[arg(default_value_t = Style::Colorful)]
7878
#[arg(value_name = "NAME")]
7979
pub style: Style,
80+
81+
#[arg(verbatim_doc_comment)]
82+
/// Fail if error occurred
83+
/// Example: cnb --fail-on-error ing --list
84+
#[arg(long)]
85+
#[clap(visible_alias = "foe")]
86+
#[arg(default_value_t = false)]
87+
pub fail_on_error: bool,
8088
}

src/args/parser.rs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use crate::api::auth::session;
21
use crate::args::{cmd, Args, Cmd};
3-
use crate::infra::option::{IntoOption, OptionExt};
4-
use anyhow::Result;
2+
use crate::infra::option::IntoOption;
53

64
fn get_skip(skip: &Option<usize>) -> usize {
75
skip.unwrap_or(0)
@@ -11,10 +9,6 @@ fn get_take(take: &Option<usize>) -> usize {
119
take.unwrap_or(8).min(100)
1210
}
1311

14-
fn get_pat(pat: &Option<String>) -> Result<String> {
15-
pat.clone().or_eval_result(session::get_pat)
16-
}
17-
1812
pub const fn no_operation(args: &Args) -> bool {
1913
matches!(
2014
args,
@@ -27,33 +21,33 @@ pub const fn no_operation(args: &Args) -> bool {
2721
take: None,
2822
debug: _,
2923
style: _,
24+
fail_on_error: _,
3025
}
3126
)
3227
}
3328

34-
pub fn user_info(args: &Args) -> Option<Result<String>> {
35-
match args {
29+
pub const fn user_info(args: &Args) -> bool {
30+
matches!(
31+
args,
3632
Args {
37-
cmd:
38-
Some(Cmd::User(cmd::user::Opt {
39-
login: None,
40-
logout: false,
41-
info: true,
42-
})),
33+
cmd: Some(Cmd::User(cmd::user::Opt {
34+
login: None,
35+
logout: false,
36+
info: true,
37+
})),
4338
id: None,
44-
with_pat,
39+
with_pat: _,
4540
rev: false,
4641
skip: None,
4742
take: None,
4843
debug: _,
4944
style: _,
50-
} => get_pat(with_pat),
51-
_ => return None,
52-
}
53-
.into_some()
45+
fail_on_error: _,
46+
}
47+
)
5448
}
5549

56-
pub fn publish_ing(args: &Args) -> Option<Result<(String, &String)>> {
50+
pub fn publish_ing(args: &Args) -> Option<&String> {
5751
match args {
5852
Args {
5953
cmd:
@@ -63,13 +57,14 @@ pub fn publish_ing(args: &Args) -> Option<Result<(String, &String)>> {
6357
comment: None,
6458
})),
6559
id: None,
66-
with_pat,
60+
with_pat: _,
6761
rev: false,
6862
skip: None,
6963
take: None,
7064
debug: _,
7165
style: _,
72-
} => get_pat(with_pat).map(|pat| (pat, content)),
66+
fail_on_error: _,
67+
} => content,
7368
_ => return None,
7469
}
7570
.into_some()
@@ -91,6 +86,7 @@ pub fn login(args: &Args) -> Option<&String> {
9186
take: None,
9287
debug: _,
9388
style: _,
89+
fail_on_error: _,
9490
} => pat,
9591
_ => return None,
9692
}
@@ -113,11 +109,12 @@ pub const fn logout(args: &Args) -> bool {
113109
take: None,
114110
debug: _,
115111
style: _,
112+
fail_on_error: _,
116113
}
117114
)
118115
}
119116

120-
pub fn list_ing(args: &Args) -> Option<Result<(String, usize, usize)>> {
117+
pub fn list_ing(args: &Args) -> Option<(usize, usize)> {
121118
match args {
122119
Args {
123120
cmd:
@@ -127,23 +124,24 @@ pub fn list_ing(args: &Args) -> Option<Result<(String, usize, usize)>> {
127124
comment: None,
128125
})),
129126
id: None,
130-
with_pat,
127+
with_pat: _,
131128
rev: _,
132129
skip,
133130
take,
134131
debug: _,
135132
style: _,
133+
fail_on_error: _,
136134
} => {
137135
let skip = get_skip(skip);
138136
let take = get_take(take);
139-
get_pat(with_pat).map(|pat| (pat, skip, take))
137+
(skip, take)
140138
}
141139
_ => return None,
142140
}
143141
.into_some()
144142
}
145143

146-
pub fn comment_ing(args: &Args) -> Option<Result<(String, &String, usize)>> {
144+
pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
147145
match args {
148146
Args {
149147
cmd:
@@ -153,19 +151,20 @@ pub fn comment_ing(args: &Args) -> Option<Result<(String, &String, usize)>> {
153151
comment: Some(content),
154152
})),
155153
id: Some(id),
156-
with_pat,
154+
with_pat: _,
157155
rev: false,
158156
skip: None,
159157
take: None,
160158
debug: _,
161159
style: _,
162-
} => get_pat(with_pat).map(|pat| (pat, content, *id)),
160+
fail_on_error: _,
161+
} => (content, *id),
163162
_ => return None,
164163
}
165164
.into_some()
166165
}
167166

168-
pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
167+
pub fn show_post(args: &Args) -> Option<usize> {
169168
match args {
170169
Args {
171170
cmd:
@@ -178,19 +177,20 @@ pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
178177
cmd: None,
179178
})),
180179
id: Some(id),
181-
with_pat,
180+
with_pat: _,
182181
rev: false,
183182
skip: None,
184183
take: None,
185184
debug: _,
186185
style: _,
187-
} => get_pat(with_pat).map(|pat| (pat, *id)),
186+
fail_on_error: _,
187+
} => *id,
188188
_ => return None,
189189
}
190190
.into_some()
191191
}
192192

193-
pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
193+
pub fn show_post_meta(args: &Args) -> Option<usize> {
194194
match args {
195195
Args {
196196
cmd:
@@ -203,19 +203,20 @@ pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
203203
cmd: None,
204204
})),
205205
id: Some(id),
206-
with_pat,
206+
with_pat: _,
207207
rev: false,
208208
skip: None,
209209
take: None,
210210
debug: _,
211211
style: _,
212-
} => get_pat(with_pat).map(|pat| (pat, *id)),
212+
fail_on_error: _,
213+
} => *id,
213214
_ => return None,
214215
}
215216
.into_some()
216217
}
217218

218-
pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
219+
pub fn list_post(args: &Args) -> Option<(usize, usize)> {
219220
match args {
220221
Args {
221222
cmd:
@@ -228,23 +229,24 @@ pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
228229
cmd: None,
229230
})),
230231
id: None,
231-
with_pat,
232+
with_pat: _,
232233
rev: _,
233234
skip,
234235
take,
235236
debug: _,
236237
style: _,
238+
fail_on_error: _,
237239
} => {
238240
let skip = get_skip(skip);
239241
let take = get_take(take);
240-
get_pat(with_pat).map(|pat| (pat, skip, take))
242+
(skip, take)
241243
}
242244
_ => return None,
243245
}
244246
.into_some()
245247
}
246248

247-
pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
249+
pub fn delete_post(args: &Args) -> Option<usize> {
248250
match args {
249251
Args {
250252
cmd:
@@ -257,19 +259,20 @@ pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
257259
cmd: None,
258260
})),
259261
id: Some(id),
260-
with_pat,
262+
with_pat: _,
261263
rev: false,
262264
skip: None,
263265
take: None,
264266
debug: _,
265267
style: _,
266-
} => get_pat(with_pat).map(|pat| (pat, *id)),
268+
fail_on_error: _,
269+
} => *id,
267270
_ => return None,
268271
}
269272
.into_some()
270273
}
271274

272-
pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)>> {
275+
pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
273276
match args {
274277
Args {
275278
cmd:
@@ -282,23 +285,24 @@ pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)
282285
cmd: None,
283286
})),
284287
id: None,
285-
with_pat,
288+
with_pat: _,
286289
rev: _,
287290
skip,
288291
take,
289292
debug: _,
290293
style: _,
294+
fail_on_error: _,
291295
} => {
292296
let skip = get_skip(skip);
293297
let take = get_take(take);
294-
get_pat(with_pat).map(|pat| (pat, keyword, skip, take))
298+
(keyword, skip, take)
295299
}
296300
_ => return None,
297301
}
298302
.into_some()
299303
}
300304

301-
pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool)>> {
305+
pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
302306
match args {
303307
Args {
304308
cmd:
@@ -316,13 +320,14 @@ pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool
316320
}),
317321
})),
318322
id: None,
319-
with_pat,
323+
with_pat: _,
320324
rev: _,
321325
skip: None,
322326
take: None,
323327
debug: _,
324328
style: _,
325-
} => get_pat(with_pat).map(|pat| (pat, title, body, *publish)),
329+
fail_on_error: _,
330+
} => (title, body, *publish),
326331
_ => return None,
327332
}
328333
.into_some()
@@ -332,15 +337,7 @@ pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool
332337
#[allow(clippy::type_complexity)]
333338
pub fn update_post(
334339
args: &Args,
335-
) -> Option<
336-
Result<(
337-
String,
338-
usize,
339-
&Option<String>,
340-
&Option<String>,
341-
&Option<bool>,
342-
)>,
343-
> {
340+
) -> Option<(usize, &Option<String>, &Option<String>, &Option<bool>)> {
344341
match args {
345342
Args {
346343
cmd:
@@ -358,13 +355,14 @@ pub fn update_post(
358355
}),
359356
})),
360357
id: Some(id),
361-
with_pat,
358+
with_pat: _,
362359
rev: _,
363360
skip: None,
364361
take: None,
365362
debug: _,
366363
style: _,
367-
} => get_pat(with_pat).map(|pat| (pat, *id, title, body, publish)),
364+
fail_on_error: _,
365+
} => (*id, title, body, publish),
368366
_ => return None,
369367
}
370368
.into_some()

0 commit comments

Comments
 (0)