Skip to content

Commit

Permalink
feat!: better err handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 8, 2023
1 parent 6f18164 commit 9b8f093
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 218 deletions.
2 changes: 1 addition & 1 deletion src/api/user/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::openapi;
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserInfo {
#[serde(rename = "UserId")]
pub user_id: String,
Expand Down
28 changes: 18 additions & 10 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ pub struct Args {
#[arg(long)]
pub id: Option<usize>,

#[arg(verbatim_doc_comment)]
/// Execute with specific PAT
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
/// Your PAT in ~/.cnbrc will be ignored in this execution if it exists
/// Please login if you don't want to input PAT everytime, try 'cnb user --help' for more details
#[arg(long)]
#[arg(value_name = "PAT")]
pub with_pat: Option<String>,

#[arg(verbatim_doc_comment)]
/// Reverse list output
/// Example: cnb --rev ing --list
Expand Down Expand Up @@ -60,6 +51,15 @@ pub struct Args {
#[arg(value_name = "LENGTH")]
pub take: Option<usize>,

#[arg(verbatim_doc_comment)]
/// Execute with specific PAT
/// Example: cnb --with-pat 'FOOBARBAZ' post --list
/// Your PAT in ~/.cnbrc will be ignored in this execution if it exists
/// Please login if you don't want to input PAT everytime, try 'cnb user --help' for more details
#[arg(long)]
#[arg(value_name = "PAT")]
pub with_pat: Option<String>,

#[arg(verbatim_doc_comment)]
/// Execute in debug mode, this will print some messages for the developer
/// Example: cnb --debug ing --list
Expand All @@ -74,7 +74,15 @@ pub struct Args {
#[arg(long)]
#[arg(value_enum)]
#[arg(hide_possible_values = true)]
#[arg(default_value = "colorful")]
#[arg(default_value_t = Style::Colorful)]
#[arg(value_name = "NAME")]
pub style: Style,

#[arg(verbatim_doc_comment)]
/// Fail if error occurred
/// Example: cnb --fail-on-error ing --list
#[arg(long)]
#[clap(visible_alias = "foe")]
#[arg(default_value_t = false)]
pub fail_on_error: bool,
}
114 changes: 56 additions & 58 deletions src/args/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::api::auth::session;
use crate::args::{cmd, Args, Cmd};
use crate::infra::option::{IntoOption, OptionExt};
use anyhow::Result;
use crate::infra::option::IntoOption;

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

fn get_pat(pat: &Option<String>) -> Result<String> {
pat.clone().or_eval_result(session::get_pat)
}

pub const fn no_operation(args: &Args) -> bool {
matches!(
args,
Expand All @@ -27,33 +21,33 @@ pub const fn no_operation(args: &Args) -> bool {
take: None,
debug: _,
style: _,
fail_on_error: _,
}
)
}

pub fn user_info(args: &Args) -> Option<Result<String>> {
match args {
pub const fn user_info(args: &Args) -> bool {
matches!(
args,
Args {
cmd:
Some(Cmd::User(cmd::user::Opt {
login: None,
logout: false,
info: true,
})),
cmd: Some(Cmd::User(cmd::user::Opt {
login: None,
logout: false,
info: true,
})),
id: None,
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat),
_ => return None,
}
.into_some()
fail_on_error: _,
}
)
}

pub fn publish_ing(args: &Args) -> Option<Result<(String, &String)>> {
pub fn publish_ing(args: &Args) -> Option<&String> {
match args {
Args {
cmd:
Expand All @@ -63,13 +57,14 @@ pub fn publish_ing(args: &Args) -> Option<Result<(String, &String)>> {
comment: None,
})),
id: None,
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, content)),
fail_on_error: _,
} => content,
_ => return None,
}
.into_some()
Expand All @@ -91,6 +86,7 @@ pub fn login(args: &Args) -> Option<&String> {
take: None,
debug: _,
style: _,
fail_on_error: _,
} => pat,
_ => return None,
}
Expand All @@ -113,11 +109,12 @@ pub const fn logout(args: &Args) -> bool {
take: None,
debug: _,
style: _,
fail_on_error: _,
}
)
}

pub fn list_ing(args: &Args) -> Option<Result<(String, usize, usize)>> {
pub fn list_ing(args: &Args) -> Option<(usize, usize)> {
match args {
Args {
cmd:
Expand All @@ -127,23 +124,24 @@ pub fn list_ing(args: &Args) -> Option<Result<(String, usize, usize)>> {
comment: None,
})),
id: None,
with_pat,
with_pat: _,
rev: _,
skip,
take,
debug: _,
style: _,
fail_on_error: _,
} => {
let skip = get_skip(skip);
let take = get_take(take);
get_pat(with_pat).map(|pat| (pat, skip, take))
(skip, take)
}
_ => return None,
}
.into_some()
}

pub fn comment_ing(args: &Args) -> Option<Result<(String, &String, usize)>> {
pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
match args {
Args {
cmd:
Expand All @@ -153,19 +151,20 @@ pub fn comment_ing(args: &Args) -> Option<Result<(String, &String, usize)>> {
comment: Some(content),
})),
id: Some(id),
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, content, *id)),
fail_on_error: _,
} => (content, *id),
_ => return None,
}
.into_some()
}

pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
pub fn show_post(args: &Args) -> Option<usize> {
match args {
Args {
cmd:
Expand All @@ -178,19 +177,20 @@ pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
cmd: None,
})),
id: Some(id),
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, *id)),
fail_on_error: _,
} => *id,
_ => return None,
}
.into_some()
}

pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
pub fn show_post_meta(args: &Args) -> Option<usize> {
match args {
Args {
cmd:
Expand All @@ -203,19 +203,20 @@ pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
cmd: None,
})),
id: Some(id),
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, *id)),
fail_on_error: _,
} => *id,
_ => return None,
}
.into_some()
}

pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
pub fn list_post(args: &Args) -> Option<(usize, usize)> {
match args {
Args {
cmd:
Expand All @@ -228,23 +229,24 @@ pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
cmd: None,
})),
id: None,
with_pat,
with_pat: _,
rev: _,
skip,
take,
debug: _,
style: _,
fail_on_error: _,
} => {
let skip = get_skip(skip);
let take = get_take(take);
get_pat(with_pat).map(|pat| (pat, skip, take))
(skip, take)
}
_ => return None,
}
.into_some()
}

pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
pub fn delete_post(args: &Args) -> Option<usize> {
match args {
Args {
cmd:
Expand All @@ -257,19 +259,20 @@ pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
cmd: None,
})),
id: Some(id),
with_pat,
with_pat: _,
rev: false,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, *id)),
fail_on_error: _,
} => *id,
_ => return None,
}
.into_some()
}

pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)>> {
pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
match args {
Args {
cmd:
Expand All @@ -282,23 +285,24 @@ pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)
cmd: None,
})),
id: None,
with_pat,
with_pat: _,
rev: _,
skip,
take,
debug: _,
style: _,
fail_on_error: _,
} => {
let skip = get_skip(skip);
let take = get_take(take);
get_pat(with_pat).map(|pat| (pat, keyword, skip, take))
(keyword, skip, take)
}
_ => return None,
}
.into_some()
}

pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool)>> {
pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
match args {
Args {
cmd:
Expand All @@ -316,13 +320,14 @@ pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool
}),
})),
id: None,
with_pat,
with_pat: _,
rev: _,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, title, body, *publish)),
fail_on_error: _,
} => (title, body, *publish),
_ => return None,
}
.into_some()
Expand All @@ -332,15 +337,7 @@ pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String, bool
#[allow(clippy::type_complexity)]
pub fn update_post(
args: &Args,
) -> Option<
Result<(
String,
usize,
&Option<String>,
&Option<String>,
&Option<bool>,
)>,
> {
) -> Option<(usize, &Option<String>, &Option<String>, &Option<bool>)> {
match args {
Args {
cmd:
Expand All @@ -358,13 +355,14 @@ pub fn update_post(
}),
})),
id: Some(id),
with_pat,
with_pat: _,
rev: _,
skip: None,
take: None,
debug: _,
style: _,
} => get_pat(with_pat).map(|pat| (pat, *id, title, body, publish)),
fail_on_error: _,
} => (*id, title, body, publish),
_ => return None,
}
.into_some()
Expand Down
Loading

0 comments on commit 9b8f093

Please sign in to comment.