Skip to content

Commit 6977e37

Browse files
committed
feat(ing list): type option
1 parent b5a991d commit 6977e37

File tree

7 files changed

+54
-57
lines changed

7 files changed

+54
-57
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ cnb post create --title 'Hello' --body 'world!' --publish
3434
cnb --id 114514 post update --body 'niconiconiconi'
3535

3636
# Show ing list
37-
cnb ing --list
37+
cnb ing list
3838
# Publish ing
39-
cnb ing --pub 'Hello world!'
39+
cnb ing --publish 'Hello world!'
4040
# Comment to ing
4141
cnb --id 114514 ing --comment 'Awesome!'
4242

src/api/ing/create.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::Result;
55
use serde_json::json;
66

77
impl Ing {
8+
// TODO: impl send from
89
pub async fn create(&self, content: &str) -> Result<()> {
910
let client = reqwest::Client::new();
1011

src/api/ing/mod.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
pub mod comment;
22
pub mod create;
33

4-
use crate::infra::result::IntoResult;
5-
use anyhow::bail;
4+
use clap::{Parser, ValueEnum};
65
use lazy_static::lazy_static;
76
use regex::Regex;
87
use serde_repr::{Deserialize_repr, Serialize_repr};
@@ -20,16 +19,16 @@ impl Ing {
2019
}
2120
}
2221

23-
#[derive(Clone, Debug)]
22+
#[derive(Clone, Debug, Parser, ValueEnum)]
2423
pub enum IngType {
25-
Following = 1,
24+
Follow = 1,
2625
Myself = 4,
27-
Public = 5,
28-
RecentComment = 6,
26+
Pub = 5,
27+
//RecentComment = 6,
2928
MyComment = 7,
30-
Tag = 10,
31-
Comment = 13,
32-
Mention = 14,
29+
//Tag = 10,
30+
//Comment = 13,
31+
//Mention = 14,
3332
}
3433

3534
#[derive(Clone, Debug, Serialize_repr, Deserialize_repr)]
@@ -46,26 +45,6 @@ pub enum IngSendFrom {
4645
Cli = 13,
4746
}
4847

49-
impl TryFrom<usize> for IngSendFrom {
50-
type Error = anyhow::Error;
51-
52-
fn try_from(value: usize) -> Result<Self, Self::Error> {
53-
match value {
54-
0 => Self::None,
55-
1 => Self::Ms,
56-
2 => Self::GTalk,
57-
3 => Self::Qq,
58-
5 => Self::Sms,
59-
6 => Self::CellPhone,
60-
8 => Self::Web,
61-
9 => Self::VsCode,
62-
13 => Self::Cli,
63-
u => bail!("Unknown value of ing source: {}", u),
64-
}
65-
.into_ok()
66-
}
67-
}
68-
6948
pub fn ing_star_tag_to_text(tag: &str) -> String {
7049
lazy_static! {
7150
static ref REGEX: Regex = Regex::new(r#"<img.*alt="\[(.*?)]"(\n|.)*>"#).unwrap();

src/args/cmd/ing.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use clap::Parser;
1+
use crate::api::ing::IngType;
2+
use clap::{Parser, Subcommand};
23

34
#[derive(Parser, Debug)]
45
pub struct Opt {
5-
#[arg(verbatim_doc_comment)]
6-
/// Show ing list, order by time in DESC
7-
/// Example: cnb ing --list
8-
#[arg(long)]
9-
#[arg(short = 'l')]
10-
pub list: bool,
6+
#[command(subcommand)]
7+
pub cmd: Option<Cmd>,
118

129
#[arg(verbatim_doc_comment)]
1310
/// Publish ing with specific content
14-
/// Example: cnb ing --publish 'Hello world'
15-
/// The visibility of ing is public
11+
/// Example: cnb ing --publish 'Hello world'
12+
/// The visibility of ing is public
13+
/// *
1614
#[arg(long)]
1715
#[arg(short = 'p')]
1816
#[arg(visible_alias = "pub")]
@@ -21,10 +19,29 @@ pub struct Opt {
2119

2220
#[arg(verbatim_doc_comment)]
2321
/// Comment ing with specific content
24-
/// Example: cnb --id 114514 ing --comment 'Hello world'
25-
/// You should also specify the id of the ing via --id
22+
/// Example: cnb --id 114514 ing --comment 'Hello world'
23+
/// You should also specify the id of the ing via --id
2624
#[arg(long)]
2725
#[arg(short = 'c')]
2826
#[arg(value_name = "CONTENT")]
2927
pub comment: Option<String>,
3028
}
29+
30+
#[derive(Debug, Subcommand)]
31+
pub enum Cmd {
32+
#[clap(verbatim_doc_comment)]
33+
/// Show ing list, order by time in DESC
34+
/// Example: cnb ing list
35+
/// *
36+
#[clap(visible_alias = "l")]
37+
List {
38+
#[arg(verbatim_doc_comment)]
39+
/// Ing type to show
40+
/// Example: cnb ing list --type myself
41+
/// *
42+
#[arg(long)]
43+
#[arg(value_name = "TYPE")]
44+
#[arg(default_value = "public")]
45+
r#type: IngType,
46+
},
47+
}

src/args/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ pub struct Args {
2525

2626
#[arg(verbatim_doc_comment)]
2727
/// Reverse list output
28-
/// Example: cnb --rev ing --list
28+
/// Example: cnb --rev ing list
2929
#[arg(long)]
3030
pub rev: bool,
3131

3232
#[arg(verbatim_doc_comment)]
3333
/// Skip items while request list
34-
/// Example: cnb --skip 2 ing --list
34+
/// Example: cnb --skip 2 ing list
3535
/// Use this option to save network I/O if some items of the list output are not needed
3636
/// If this option is required but not specified, it will be set to 0
3737
#[arg(long)]
@@ -41,7 +41,7 @@ pub struct Args {
4141

4242
#[arg(verbatim_doc_comment)]
4343
/// Take items while request list
44-
/// Example: cnb --take 2 ing --list
44+
/// Example: cnb --take 2 ing list
4545
/// Use this option to save network I/O if only a subset of the list output are required
4646
/// <LENGTH> should be in the range [0,100]
4747
/// If <LENGTH> is greater than 100, it will be set to 100
@@ -62,7 +62,7 @@ pub struct Args {
6262

6363
#[arg(verbatim_doc_comment)]
6464
/// Execute in debug mode, this will print some messages for the developer
65-
/// Example: cnb --debug ing --list
65+
/// Example: cnb --debug ing list
6666
/// THIS OPTION IS UNSTABLE FOREVER and any output from it may change in the future
6767
/// You should NEVER rely on the output while you turn this option on
6868
/// *
@@ -83,7 +83,7 @@ pub struct Args {
8383

8484
#[arg(verbatim_doc_comment)]
8585
/// Fail if error occurred
86-
/// Example: cnb --fail-on-error ing --list
86+
/// Example: cnb --fail-on-error ing list
8787
/// *
8888
#[arg(long)]
8989
#[clap(visible_alias = "foe")]
@@ -92,7 +92,7 @@ pub struct Args {
9292

9393
#[arg(verbatim_doc_comment)]
9494
/// Suppress all output
95-
/// Example: cnb --quiet ing --list
95+
/// Example: cnb --quiet ing list
9696
/// *
9797
#[arg(long)]
9898
#[clap(visible_alias = "silent")]

src/args/parser.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::api::ing::IngType;
12
use crate::args::{cmd, Args, Cmd};
23
use crate::infra::option::IntoOption;
34

@@ -54,7 +55,7 @@ pub fn publish_ing(args: &Args) -> Option<&String> {
5455
Args {
5556
cmd:
5657
Some(Cmd::Ing(cmd::ing::Opt {
57-
list: false,
58+
cmd: None,
5859
publish: Some(content),
5960
comment: None,
6061
})),
@@ -119,12 +120,12 @@ pub const fn logout(args: &Args) -> bool {
119120
)
120121
}
121122

122-
pub fn list_ing(args: &Args) -> Option<(usize, usize)> {
123+
pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType)> {
123124
match args {
124125
Args {
125126
cmd:
126127
Some(Cmd::Ing(cmd::ing::Opt {
127-
list: true,
128+
cmd: Some(cmd::ing::Cmd::List { r#type }),
128129
publish: None,
129130
comment: None,
130131
})),
@@ -140,7 +141,7 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize)> {
140141
} => {
141142
let skip = get_skip(skip);
142143
let take = get_take(take);
143-
(skip, take)
144+
(skip, take, r#type.clone())
144145
}
145146
_ => return None,
146147
}
@@ -152,7 +153,7 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
152153
Args {
153154
cmd:
154155
Some(Cmd::Ing(cmd::ing::Opt {
155-
list: false,
156+
cmd: None,
156157
publish: None,
157158
comment: Some(content),
158159
})),

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![warn(clippy::all, clippy::nursery, clippy::cargo_common_metadata)]
77

88
use crate::api::auth::session;
9-
use crate::api::ing::{Ing, IngType};
9+
use crate::api::ing::Ing;
1010
use crate::api::news::News;
1111
use crate::api::post::Post;
1212
use crate::api::user::User;
@@ -68,10 +68,9 @@ async fn main() -> Result<()> {
6868
foe.then(||panic_if_err(&user_info));
6969
quiet.not().then(||display::user_info(style, &user_info));
7070
}
71-
_ if let Some((skip, take)) = parser::list_ing(&args) => {
72-
let ing_type = IngType::Public;
71+
_ if let Some((skip, take, r#type)) = parser::list_ing(&args) => {
7372
let ing_vec = try {
74-
Ing::new(pat?).get_list(skip, take, &ing_type).await?
73+
Ing::new(pat?).get_list(skip, take, &r#type).await?
7574
};
7675
foe.then(||panic_if_err(&ing_vec));
7776
quiet.not().then(||display::list_ing(style, &ing_vec, rev));

0 commit comments

Comments
 (0)