From 6977e374e6fdeaa9fd4e40cb7c6380ea021fbb28 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Wed, 13 Sep 2023 16:13:07 +0800 Subject: [PATCH] feat(ing list): type option --- README.md | 4 ++-- src/api/ing/create.rs | 1 + src/api/ing/mod.rs | 37 ++++++++----------------------------- src/args/cmd/ing.rs | 39 ++++++++++++++++++++++++++++----------- src/args/mod.rs | 12 ++++++------ src/args/parser.rs | 11 ++++++----- src/main.rs | 7 +++---- 7 files changed, 54 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 1a96026..4427ce1 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ cnb post create --title 'Hello' --body 'world!' --publish cnb --id 114514 post update --body 'niconiconiconi' # Show ing list -cnb ing --list +cnb ing list # Publish ing -cnb ing --pub 'Hello world!' +cnb ing --publish 'Hello world!' # Comment to ing cnb --id 114514 ing --comment 'Awesome!' diff --git a/src/api/ing/create.rs b/src/api/ing/create.rs index ad79f00..3c5c897 100644 --- a/src/api/ing/create.rs +++ b/src/api/ing/create.rs @@ -5,6 +5,7 @@ use anyhow::Result; use serde_json::json; impl Ing { + // TODO: impl send from pub async fn create(&self, content: &str) -> Result<()> { let client = reqwest::Client::new(); diff --git a/src/api/ing/mod.rs b/src/api/ing/mod.rs index 4da6682..e51bb95 100644 --- a/src/api/ing/mod.rs +++ b/src/api/ing/mod.rs @@ -1,8 +1,7 @@ pub mod comment; pub mod create; -use crate::infra::result::IntoResult; -use anyhow::bail; +use clap::{Parser, ValueEnum}; use lazy_static::lazy_static; use regex::Regex; use serde_repr::{Deserialize_repr, Serialize_repr}; @@ -20,16 +19,16 @@ impl Ing { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Parser, ValueEnum)] pub enum IngType { - Following = 1, + Follow = 1, Myself = 4, - Public = 5, - RecentComment = 6, + Pub = 5, + //RecentComment = 6, MyComment = 7, - Tag = 10, - Comment = 13, - Mention = 14, + //Tag = 10, + //Comment = 13, + //Mention = 14, } #[derive(Clone, Debug, Serialize_repr, Deserialize_repr)] @@ -46,26 +45,6 @@ pub enum IngSendFrom { Cli = 13, } -impl TryFrom for IngSendFrom { - type Error = anyhow::Error; - - fn try_from(value: usize) -> Result { - match value { - 0 => Self::None, - 1 => Self::Ms, - 2 => Self::GTalk, - 3 => Self::Qq, - 5 => Self::Sms, - 6 => Self::CellPhone, - 8 => Self::Web, - 9 => Self::VsCode, - 13 => Self::Cli, - u => bail!("Unknown value of ing source: {}", u), - } - .into_ok() - } -} - pub fn ing_star_tag_to_text(tag: &str) -> String { lazy_static! { static ref REGEX: Regex = Regex::new(r#""#).unwrap(); diff --git a/src/args/cmd/ing.rs b/src/args/cmd/ing.rs index 157c4c4..f09abc0 100644 --- a/src/args/cmd/ing.rs +++ b/src/args/cmd/ing.rs @@ -1,18 +1,16 @@ -use clap::Parser; +use crate::api::ing::IngType; +use clap::{Parser, Subcommand}; #[derive(Parser, Debug)] pub struct Opt { - #[arg(verbatim_doc_comment)] - /// Show ing list, order by time in DESC - /// Example: cnb ing --list - #[arg(long)] - #[arg(short = 'l')] - pub list: bool, + #[command(subcommand)] + pub cmd: Option, #[arg(verbatim_doc_comment)] /// Publish ing with specific content - /// Example: cnb ing --publish 'Hello world' - /// The visibility of ing is public + /// Example: cnb ing --publish 'Hello world' + /// The visibility of ing is public + /// * #[arg(long)] #[arg(short = 'p')] #[arg(visible_alias = "pub")] @@ -21,10 +19,29 @@ pub struct Opt { #[arg(verbatim_doc_comment)] /// Comment ing with specific content - /// Example: cnb --id 114514 ing --comment 'Hello world' - /// You should also specify the id of the ing via --id + /// Example: cnb --id 114514 ing --comment 'Hello world' + /// You should also specify the id of the ing via --id #[arg(long)] #[arg(short = 'c')] #[arg(value_name = "CONTENT")] pub comment: Option, } + +#[derive(Debug, Subcommand)] +pub enum Cmd { + #[clap(verbatim_doc_comment)] + /// Show ing list, order by time in DESC + /// Example: cnb ing list + /// * + #[clap(visible_alias = "l")] + List { + #[arg(verbatim_doc_comment)] + /// Ing type to show + /// Example: cnb ing list --type myself + /// * + #[arg(long)] + #[arg(value_name = "TYPE")] + #[arg(default_value = "public")] + r#type: IngType, + }, +} diff --git a/src/args/mod.rs b/src/args/mod.rs index 9ca96a8..fe202e4 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -25,13 +25,13 @@ pub struct Args { #[arg(verbatim_doc_comment)] /// Reverse list output - /// Example: cnb --rev ing --list + /// Example: cnb --rev ing list #[arg(long)] pub rev: bool, #[arg(verbatim_doc_comment)] /// Skip items while request list - /// Example: cnb --skip 2 ing --list + /// Example: cnb --skip 2 ing list /// Use this option to save network I/O if some items of the list output are not needed /// If this option is required but not specified, it will be set to 0 #[arg(long)] @@ -41,7 +41,7 @@ pub struct Args { #[arg(verbatim_doc_comment)] /// Take items while request list - /// Example: cnb --take 2 ing --list + /// Example: cnb --take 2 ing list /// Use this option to save network I/O if only a subset of the list output are required /// should be in the range [0,100] /// If is greater than 100, it will be set to 100 @@ -62,7 +62,7 @@ pub struct Args { #[arg(verbatim_doc_comment)] /// Execute in debug mode, this will print some messages for the developer - /// Example: cnb --debug ing --list + /// Example: cnb --debug ing list /// THIS OPTION IS UNSTABLE FOREVER and any output from it may change in the future /// You should NEVER rely on the output while you turn this option on /// * @@ -83,7 +83,7 @@ pub struct Args { #[arg(verbatim_doc_comment)] /// Fail if error occurred - /// Example: cnb --fail-on-error ing --list + /// Example: cnb --fail-on-error ing list /// * #[arg(long)] #[clap(visible_alias = "foe")] @@ -92,7 +92,7 @@ pub struct Args { #[arg(verbatim_doc_comment)] /// Suppress all output - /// Example: cnb --quiet ing --list + /// Example: cnb --quiet ing list /// * #[arg(long)] #[clap(visible_alias = "silent")] diff --git a/src/args/parser.rs b/src/args/parser.rs index c1bbb12..ba417ba 100644 --- a/src/args/parser.rs +++ b/src/args/parser.rs @@ -1,3 +1,4 @@ +use crate::api::ing::IngType; use crate::args::{cmd, Args, Cmd}; use crate::infra::option::IntoOption; @@ -54,7 +55,7 @@ pub fn publish_ing(args: &Args) -> Option<&String> { Args { cmd: Some(Cmd::Ing(cmd::ing::Opt { - list: false, + cmd: None, publish: Some(content), comment: None, })), @@ -119,12 +120,12 @@ pub const fn logout(args: &Args) -> bool { ) } -pub fn list_ing(args: &Args) -> Option<(usize, usize)> { +pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> { match args { Args { cmd: Some(Cmd::Ing(cmd::ing::Opt { - list: true, + cmd: Some(cmd::ing::Cmd::List { r#type }), publish: None, comment: None, })), @@ -140,7 +141,7 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize)> { } => { let skip = get_skip(skip); let take = get_take(take); - (skip, take) + (skip, take, r#type.clone()) } _ => return None, } @@ -152,7 +153,7 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> { Args { cmd: Some(Cmd::Ing(cmd::ing::Opt { - list: false, + cmd: None, publish: None, comment: Some(content), })), diff --git a/src/main.rs b/src/main.rs index 8ac51ed..ea0be88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ #![warn(clippy::all, clippy::nursery, clippy::cargo_common_metadata)] use crate::api::auth::session; -use crate::api::ing::{Ing, IngType}; +use crate::api::ing::Ing; use crate::api::news::News; use crate::api::post::Post; use crate::api::user::User; @@ -68,10 +68,9 @@ async fn main() -> Result<()> { foe.then(||panic_if_err(&user_info)); quiet.not().then(||display::user_info(style, &user_info)); } - _ if let Some((skip, take)) = parser::list_ing(&args) => { - let ing_type = IngType::Public; + _ if let Some((skip, take, r#type)) = parser::list_ing(&args) => { let ing_vec = try { - Ing::new(pat?).get_list(skip, take, &ing_type).await? + Ing::new(pat?).get_list(skip, take, &r#type).await? }; foe.then(||panic_if_err(&ing_vec)); quiet.not().then(||display::list_ing(style, &ing_vec, rev));