From 3a88debd82f854b4907ff57e2de6b7dabd34846c Mon Sep 17 00:00:00 2001 From: Thaumy Date: Wed, 27 Sep 2023 12:06:01 +0800 Subject: [PATCH] refactor: parser mod layout --- src/args/parser/fav.rs | 27 +++ src/args/parser/ing.rs | 84 ++++++++ src/args/parser/mod.rs | 34 ++++ src/args/parser/news.rs | 27 +++ src/args/{parser.rs => parser/post.rs} | 254 ++----------------------- src/args/parser/user.rs | 73 +++++++ src/main.rs | 32 ++-- 7 files changed, 275 insertions(+), 256 deletions(-) create mode 100644 src/args/parser/fav.rs create mode 100644 src/args/parser/ing.rs create mode 100644 src/args/parser/mod.rs create mode 100644 src/args/parser/news.rs rename src/args/{parser.rs => parser/post.rs} (55%) create mode 100644 src/args/parser/user.rs diff --git a/src/args/parser/fav.rs b/src/args/parser/fav.rs new file mode 100644 index 0000000..493503d --- /dev/null +++ b/src/args/parser/fav.rs @@ -0,0 +1,27 @@ +use crate::args::parser::{get_skip, get_take}; +use crate::args::{cmd, Args, Cmd}; +use crate::infra::option::IntoOption; + +pub fn list_fav(args: &Args) -> Option<(usize, usize)> { + match args { + Args { + cmd: Some(Cmd::Fav(cmd::fav::Opt { list: true })), + id: None, + with_pat: _, + rev: _, + skip, + take, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => { + let skip = get_skip(skip); + let take = get_take(take); + (skip, take) + } + _ => return None, + } + .into_some() +} diff --git a/src/args/parser/ing.rs b/src/args/parser/ing.rs new file mode 100644 index 0000000..0747a5b --- /dev/null +++ b/src/args/parser/ing.rs @@ -0,0 +1,84 @@ +use crate::api::ing::IngType; +use crate::args::parser::{get_skip, get_take}; +use crate::args::{cmd, Args, Cmd}; +use crate::infra::option::IntoOption; + +pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> { + match args { + Args { + cmd: + Some(Cmd::Ing(cmd::ing::Opt { + cmd: Some(cmd::ing::Cmd::List { r#type, align }), + publish: None, + comment: None, + })), + id: None, + with_pat: _, + rev: _, + skip, + take, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => { + let skip = get_skip(skip); + let take = get_take(take); + let r#type = r#type.clone().unwrap_or(IngType::Public); + (skip, take, r#type, *align) + } + _ => return None, + } + .into_some() +} + +pub fn publish_ing(args: &Args) -> Option<&String> { + match args { + Args { + cmd: + Some(Cmd::Ing(cmd::ing::Opt { + cmd: None, + publish: Some(content), + comment: None, + })), + id: None, + with_pat: _, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => content, + _ => return None, + } + .into_some() +} + +pub fn comment_ing(args: &Args) -> Option<(&String, usize)> { + match args { + Args { + cmd: + Some(Cmd::Ing(cmd::ing::Opt { + cmd: None, + publish: None, + comment: Some(content), + })), + id: Some(id), + with_pat: _, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => (content, *id), + _ => return None, + } + .into_some() +} diff --git a/src/args/parser/mod.rs b/src/args/parser/mod.rs new file mode 100644 index 0000000..72965dc --- /dev/null +++ b/src/args/parser/mod.rs @@ -0,0 +1,34 @@ +pub mod fav; +pub mod ing; +pub mod news; +pub mod post; +pub mod user; + +use crate::args::Args; + +fn get_skip(skip: &Option) -> usize { + skip.unwrap_or(0) +} + +fn get_take(take: &Option) -> usize { + take.unwrap_or(8).min(100) +} + +pub const fn no_operation(args: &Args) -> bool { + matches!( + args, + Args { + cmd: None, + id: None, + with_pat: None, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } + ) +} diff --git a/src/args/parser/news.rs b/src/args/parser/news.rs new file mode 100644 index 0000000..230e97a --- /dev/null +++ b/src/args/parser/news.rs @@ -0,0 +1,27 @@ +use crate::args::parser::{get_skip, get_take}; +use crate::args::{cmd, Args, Cmd}; +use crate::infra::option::IntoOption; + +pub fn list_news(args: &Args) -> Option<(usize, usize)> { + match args { + Args { + cmd: Some(Cmd::News(cmd::news::Opt { list: true })), + id: None, + with_pat: _, + rev: _, + skip, + take, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => { + let skip = get_skip(skip); + let take = get_take(take); + (skip, take) + } + _ => return None, + } + .into_some() +} diff --git a/src/args/parser.rs b/src/args/parser/post.rs similarity index 55% rename from src/args/parser.rs rename to src/args/parser/post.rs index f0e10fe..6a541e1 100644 --- a/src/args/parser.rs +++ b/src/args/parser/post.rs @@ -1,138 +1,19 @@ -use crate::api::ing::IngType; +use crate::args::parser::{get_skip, get_take}; use crate::args::{cmd, Args, Cmd}; use crate::infra::option::IntoOption; -fn get_skip(skip: &Option) -> usize { - skip.unwrap_or(0) -} - -fn get_take(take: &Option) -> usize { - take.unwrap_or(8).min(100) -} - -pub const fn no_operation(args: &Args) -> bool { - matches!( - args, - Args { - cmd: None, - id: None, - with_pat: None, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } - ) -} - -pub const fn user_info(args: &Args) -> bool { - matches!( - args, - Args { - cmd: Some(Cmd::User(cmd::user::Opt { - login: None, - logout: false, - info: true, - })), - id: None, - with_pat: _, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } - ) -} - -pub fn publish_ing(args: &Args) -> Option<&String> { +pub fn list_post(args: &Args) -> Option<(usize, usize)> { match args { Args { cmd: - Some(Cmd::Ing(cmd::ing::Opt { + Some(Cmd::Post(cmd::post::Opt { + show: false, + show_meta: false, + show_comment: false, + list: true, + delete: false, + search: None, cmd: None, - publish: Some(content), - comment: None, - })), - id: None, - with_pat: _, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => content, - _ => return None, - } - .into_some() -} - -pub fn login(args: &Args) -> Option<&String> { - match args { - Args { - cmd: - Some(Cmd::User(cmd::user::Opt { - login: Some(pat), - logout: false, - info: false, - })), - id: None, - with_pat: None, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => pat, - _ => return None, - } - .into_some() -} - -pub const fn logout(args: &Args) -> bool { - matches!( - args, - Args { - cmd: Some(Cmd::User(cmd::user::Opt { - login: None, - logout: true, - info: false, - })), - id: None, - with_pat: None, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } - ) -} - -pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> { - match args { - Args { - cmd: - Some(Cmd::Ing(cmd::ing::Opt { - cmd: Some(cmd::ing::Cmd::List { r#type, align }), - publish: None, - comment: None, })), id: None, with_pat: _, @@ -147,39 +28,13 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> { } => { let skip = get_skip(skip); let take = get_take(take); - let r#type = r#type.clone().unwrap_or(IngType::Public); - (skip, take, r#type, *align) + (skip, take) } _ => return None, } .into_some() } -pub fn comment_ing(args: &Args) -> Option<(&String, usize)> { - match args { - Args { - cmd: - Some(Cmd::Ing(cmd::ing::Opt { - cmd: None, - publish: None, - comment: Some(content), - })), - id: Some(id), - with_pat: _, - rev: false, - skip: None, - take: None, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => (content, *id), - _ => return None, - } - .into_some() -} - pub fn show_post(args: &Args) -> Option { match args { Args { @@ -267,7 +122,7 @@ pub fn show_post_comment(args: &Args) -> Option { .into_some() } -pub fn list_post(args: &Args) -> Option<(usize, usize)> { +pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> { match args { Args { cmd: @@ -275,9 +130,9 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> { show: false, show_meta: false, show_comment: false, - list: true, + list: false, delete: false, - search: None, + search: Some(keyword), cmd: None, })), id: None, @@ -293,7 +148,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> { } => { let skip = get_skip(skip); let take = get_take(take); - (skip, take) + (keyword, skip, take) } _ => return None, } @@ -329,39 +184,6 @@ pub fn delete_post(args: &Args) -> Option { .into_some() } -pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> { - match args { - Args { - cmd: - Some(Cmd::Post(cmd::post::Opt { - show: false, - show_meta: false, - show_comment: false, - list: false, - delete: false, - search: Some(keyword), - cmd: None, - })), - id: None, - with_pat: _, - rev: _, - skip, - take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => { - let skip = get_skip(skip); - let take = get_take(take); - (keyword, skip, take) - } - _ => return None, - } - .into_some() -} - pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> { match args { Args { @@ -433,51 +255,3 @@ pub fn update_post( } .into_some() } - -pub fn list_news(args: &Args) -> Option<(usize, usize)> { - match args { - Args { - cmd: Some(Cmd::News(cmd::news::Opt { list: true })), - id: None, - with_pat: _, - rev: _, - skip, - take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => { - let skip = get_skip(skip); - let take = get_take(take); - (skip, take) - } - _ => return None, - } - .into_some() -} - -pub fn list_fav(args: &Args) -> Option<(usize, usize)> { - match args { - Args { - cmd: Some(Cmd::Fav(cmd::fav::Opt { list: true })), - id: None, - with_pat: _, - rev: _, - skip, - take, - debug: _, - style: _, - time_style: _, - fail_on_error: _, - quiet: _, - } => { - let skip = get_skip(skip); - let take = get_take(take); - (skip, take) - } - _ => return None, - } - .into_some() -} diff --git a/src/args/parser/user.rs b/src/args/parser/user.rs new file mode 100644 index 0000000..e9ee7cb --- /dev/null +++ b/src/args/parser/user.rs @@ -0,0 +1,73 @@ +use crate::args::{cmd, Args, Cmd}; +use crate::infra::option::IntoOption; + +pub fn login(args: &Args) -> Option<&String> { + match args { + Args { + cmd: + Some(Cmd::User(cmd::user::Opt { + login: Some(pat), + logout: false, + info: false, + })), + id: None, + with_pat: None, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } => pat, + _ => return None, + } + .into_some() +} + +pub const fn logout(args: &Args) -> bool { + matches!( + args, + Args { + cmd: Some(Cmd::User(cmd::user::Opt { + login: None, + logout: true, + info: false, + })), + id: None, + with_pat: None, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } + ) +} + +pub const fn user_info(args: &Args) -> bool { + matches!( + args, + Args { + cmd: Some(Cmd::User(cmd::user::Opt { + login: None, + logout: false, + info: true, + })), + id: None, + with_pat: _, + rev: false, + skip: None, + take: None, + debug: _, + style: _, + time_style: _, + fail_on_error: _, + quiet: _, + } + ) +} diff --git a/src/main.rs b/src/main.rs index e55ae0b..dc60213 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,22 +69,22 @@ async fn main() -> Result<()> { let foe = args.fail_on_error; let output = match args { - _ if let Some(pat) = parser::login(&args) => { + _ if let Some(pat) = parser::user::login(&args) => { let cfg_path = session::login(pat); foe.then(|| panic_if_err(&cfg_path)); display::login(style, &cfg_path) } - _ if parser::logout(&args) => { + _ if parser::user::logout(&args) => { let cfg_path = session::logout(); foe.then(|| panic_if_err(&cfg_path)); display::logout(style, &cfg_path) } - _ if parser::user_info(&args) => { + _ if parser::user::user_info(&args) => { let user_info = User::new(pat?).get_info().await; foe.then(|| panic_if_err(&user_info)); display::user_info(style, &user_info)? } - _ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => { + _ if let Some((skip, take, r#type, align)) = parser::ing::list_ing(&args) => { let ing_with_comment_iter = infer::>(try { let ing_api = Ing::new(pat?); let ing_vec = ing_api.get_list(skip, take, &r#type).await?; @@ -101,7 +101,7 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&ing_with_comment_iter)); display::list_ing(style, time_style, ing_with_comment_iter, align)? } - _ if let Some(content) = parser::publish_ing(&args) => { + _ if let Some(content) = parser::ing::publish_ing(&args) => { let content = try { Ing::new(pat?).publish(content).await?; content @@ -109,7 +109,7 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&content)); display::publish_ing(style, &content) } - _ if let Some((content, id)) = parser::comment_ing(&args) => { + _ if let Some((content, id)) = parser::ing::comment_ing(&args) => { let content = try { Ing::new(pat?).comment(id, content.clone(), None, None).await?; content @@ -117,24 +117,24 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&content)); display::comment_ing(style, &content) } - _ if let Some(id) = parser::show_post(&args) => { + _ if let Some(id) = parser::post::show_post(&args) => { let entry = Post::new(pat?).get_one(id).await; foe.then(|| panic_if_err(&entry)); display::show_post(style, &entry)? } - _ if let Some(id) = parser::show_post_meta(&args) => { + _ if let Some(id) = parser::post::show_post_meta(&args) => { let entry = Post::new(pat?).get_one(id).await; foe.then(|| panic_if_err(&entry)); display::show_post_meta(style, time_style, &entry)? } - _ if let Some(id) = parser::show_post_comment(&args) => { + _ if let Some(id) = parser::post::show_post_comment(&args) => { let comment_iter = Post::new(pat?) .get_comment_list(id).await .map(|vec| vec.into_iter().dyn_rev(rev)); foe.then(|| panic_if_err(&comment_iter)); display::show_post_comment(style, time_style, comment_iter)? } - _ if let Some((skip, take)) = parser::list_post(&args) => { + _ if let Some((skip, take)) = parser::post::list_post(&args) => { let meta_iter = Post::new(pat?) .get_meta_list(skip, take) .await @@ -142,7 +142,7 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&meta_iter)); display::list_post(style, meta_iter)? } - _ if let Some(id) = parser::delete_post(&args) => { + _ if let Some(id) = parser::post::delete_post(&args) => { let id = try { Post::new(pat?).del_one(id).await?; id @@ -150,7 +150,7 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&id)); display::delete_post(style, &id) } - _ if let Some((kw, skip, take)) = parser::search_post(&args) => { + _ if let Some((kw, skip, take)) = parser::post::search_post(&args) => { let result = Post::new(pat?) .search(skip, take, kw) .await @@ -158,17 +158,17 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&result)); display::search_post(style, result)? } - _ if let Some((title, body, publish)) = parser::create_post(&args) => { + _ if let Some((title, body, publish)) = parser::post::create_post(&args) => { 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::update_post(&args) => { + _ if let Some((id, title, body, publish)) = parser::post::update_post(&args) => { let id = Post::new(pat?).update(id, title, body, publish).await; foe.then(|| panic_if_err(&id)); display::update_post(style, &id) } - _ if let Some((skip, take)) = parser::list_news(&args) => { + _ if let Some((skip, take)) = parser::news::list_news(&args) => { let news_iter = News::new(pat?) .get_list(skip, take) .await @@ -176,7 +176,7 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&news_iter)); display::list_news(style, time_style, news_iter)? } - _ if let Some((skip, take)) = parser::list_fav(&args) => { + _ if let Some((skip, take)) = parser::fav::list_fav(&args) => { let fav_iter = Fav::new(pat?) .get_list(skip, take) .await