Skip to content

Commit 056c9c3

Browse files
committed
refactor: proj structure
1 parent 1be5224 commit 056c9c3

File tree

7 files changed

+126
-118
lines changed

7 files changed

+126
-118
lines changed

src/args/cmd/ing.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser, Debug)]
4+
pub struct Opt {
5+
#[arg(verbatim_doc_comment)]
6+
/// Show ing list, order by time in DESC
7+
#[arg(long)]
8+
#[arg(short = 'l')]
9+
pub list: bool,
10+
11+
#[arg(verbatim_doc_comment)]
12+
/// Publish ing with specific content
13+
/// The visibility of ing is public
14+
#[arg(long)]
15+
#[arg(short = 'p')]
16+
#[arg(visible_alias = "pub")]
17+
#[arg(value_name = "CONTENT")]
18+
pub publish: Option<String>,
19+
20+
#[arg(verbatim_doc_comment)]
21+
/// Comment ing with specific content
22+
/// You should also specify the id of ing via option --id
23+
#[arg(long)]
24+
#[arg(short = 'c')]
25+
#[arg(value_name = "CONTENT")]
26+
pub comment: Option<String>,
27+
}

src/args/cmd/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod ing;
2+
pub mod post;
3+
pub mod user;
4+
5+
use clap::Subcommand;
6+
7+
#[derive(Debug, Subcommand)]
8+
pub enum Cmd {
9+
/// User operations
10+
#[clap(visible_alias = "u")]
11+
User(user::Opt),
12+
/// Ing operations
13+
#[clap(visible_alias = "i")]
14+
Ing(ing::Opt),
15+
/// Post operations
16+
#[clap(visible_alias = "p")]
17+
Post(post::Opt),
18+
}

src/args/cmd/post.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser, Debug)]
4+
pub struct Opt {
5+
#[arg(verbatim_doc_comment)]
6+
/// Show title and content of a specific post
7+
/// You should also specify the id of post via option --id
8+
#[arg(long)]
9+
#[arg(short = 's')]
10+
pub show: bool,
11+
12+
#[arg(verbatim_doc_comment)]
13+
/// Show metadata of a specific post
14+
/// You should also specify the id of post via option --id
15+
#[arg(long)]
16+
#[arg(short = 'm')]
17+
pub show_meta: bool,
18+
19+
#[arg(verbatim_doc_comment)]
20+
/// Show post list, order by time in DESC
21+
/// <LENGTH> should in range [0,100]
22+
/// If <LENGTH> greater than 100, it will be set to 100
23+
#[arg(long)]
24+
#[arg(short = 'l')]
25+
#[arg(default_missing_value = "8")]
26+
#[arg(value_name = "LENGTH")]
27+
pub list: bool,
28+
29+
#[arg(verbatim_doc_comment)]
30+
/// Delete post
31+
/// You should also specify the id of post via option --id
32+
#[arg(long)]
33+
#[arg(visible_alias = "del")]
34+
pub delete: bool,
35+
36+
#[arg(verbatim_doc_comment)]
37+
/// Search post by keyword and output the post id list that matches
38+
/// Example: cnb post --search FOO
39+
#[arg(long)]
40+
#[arg(value_name = "KEYWORD")]
41+
pub search: Option<String>,
42+
}

src/args/cmd/user.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser, Debug)]
4+
pub struct Opt {
5+
#[arg(verbatim_doc_comment)]
6+
/// Login with your personal access token (PAT)
7+
/// PAT will be saved in ~/.cnbrc
8+
/// You can create PAT in https://account.cnblogs.com/tokens
9+
#[arg(long)]
10+
#[arg(value_name = "PAT")]
11+
pub login: Option<String>,
12+
13+
#[arg(verbatim_doc_comment)]
14+
/// Logout and remove ~/.cnbrc
15+
#[arg(long)]
16+
pub logout: bool,
17+
18+
#[arg(verbatim_doc_comment)]
19+
/// Show user info
20+
#[arg(long)]
21+
#[arg(short = 'i')]
22+
pub info: bool,
23+
}

src/args/mod.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1+
pub mod cmd;
12
pub mod parser;
2-
pub mod sub_cmd;
33

4-
use clap::{Parser, Subcommand, ValueEnum};
5-
6-
#[derive(Debug, Subcommand)]
7-
pub enum SubCmds {
8-
/// User operations
9-
#[clap(visible_alias = "u")]
10-
User(sub_cmd::User),
11-
/// Ing operations
12-
#[clap(visible_alias = "i")]
13-
Ing(sub_cmd::Ing),
14-
/// Post operations
15-
#[clap(visible_alias = "p")]
16-
Post(sub_cmd::Post),
17-
}
4+
use crate::args::cmd::Cmd;
5+
use clap::{Parser, ValueEnum};
186

197
#[derive(Clone, Debug, Parser, ValueEnum)]
208
pub enum Style {
@@ -27,7 +15,7 @@ pub enum Style {
2715
#[command(author, about, long_about = None, version)]
2816
pub struct Args {
2917
#[command(subcommand)]
30-
command: Option<SubCmds>,
18+
command: Option<Cmd>,
3119

3220
#[arg(verbatim_doc_comment)]
3321
/// Provide ID required by other options

src/args/parser.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::api::auth::session;
2-
use crate::args::{sub_cmd, Args, SubCmds};
2+
use crate::args::{cmd, Args, Cmd};
33
use crate::infra::option::{IntoOption, OptionExt};
44
use anyhow::Result;
55

@@ -35,7 +35,7 @@ pub fn user_info(args: &Args) -> Option<Result<String>> {
3535
match args {
3636
Args {
3737
command:
38-
Some(SubCmds::User(sub_cmd::User {
38+
Some(Cmd::User(cmd::user::Opt {
3939
login: None,
4040
logout: false,
4141
info: true,
@@ -57,7 +57,7 @@ pub fn publish_ing(args: &Args) -> Option<Result<(String, &String)>> {
5757
match args {
5858
Args {
5959
command:
60-
Some(SubCmds::Ing(sub_cmd::Ing {
60+
Some(Cmd::Ing(cmd::ing::Opt {
6161
list: false,
6262
publish: Some(content),
6363
comment: None,
@@ -79,7 +79,7 @@ pub fn login(args: &Args) -> Option<&String> {
7979
match args {
8080
Args {
8181
command:
82-
Some(SubCmds::User(sub_cmd::User {
82+
Some(Cmd::User(cmd::user::Opt {
8383
login: Some(pat),
8484
logout: false,
8585
info: false,
@@ -101,7 +101,7 @@ pub fn logout(args: &Args) -> bool {
101101
matches!(
102102
args,
103103
Args {
104-
command: Some(SubCmds::User(sub_cmd::User {
104+
command: Some(Cmd::User(cmd::user::Opt {
105105
login: None,
106106
logout: true,
107107
info: false,
@@ -121,7 +121,7 @@ pub fn list_ing(args: &Args) -> Option<Result<(String, usize, usize)>> {
121121
match args {
122122
Args {
123123
command:
124-
Some(SubCmds::Ing(sub_cmd::Ing {
124+
Some(Cmd::Ing(cmd::ing::Opt {
125125
list: true,
126126
publish: None,
127127
comment: None,
@@ -147,7 +147,7 @@ pub fn comment_ing(args: &Args) -> Option<Result<(String, &String, usize)>> {
147147
match args {
148148
Args {
149149
command:
150-
Some(SubCmds::Ing(sub_cmd::Ing {
150+
Some(Cmd::Ing(cmd::ing::Opt {
151151
list: false,
152152
publish: None,
153153
comment: Some(content),
@@ -169,7 +169,7 @@ pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
169169
match args {
170170
Args {
171171
command:
172-
Some(SubCmds::Post(sub_cmd::Post {
172+
Some(Cmd::Post(cmd::post::Opt {
173173
show: true,
174174
show_meta: false,
175175
list: false,
@@ -193,7 +193,7 @@ pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
193193
match args {
194194
Args {
195195
command:
196-
Some(SubCmds::Post(sub_cmd::Post {
196+
Some(Cmd::Post(cmd::post::Opt {
197197
show: false,
198198
show_meta: true,
199199
list: false,
@@ -217,7 +217,7 @@ pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
217217
match args {
218218
Args {
219219
command:
220-
Some(SubCmds::Post(sub_cmd::Post {
220+
Some(Cmd::Post(cmd::post::Opt {
221221
show: false,
222222
show_meta: false,
223223
list: true,
@@ -245,7 +245,7 @@ pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
245245
match args {
246246
Args {
247247
command:
248-
Some(SubCmds::Post(sub_cmd::Post {
248+
Some(Cmd::Post(cmd::post::Opt {
249249
show: false,
250250
show_meta: false,
251251
list: false,
@@ -269,7 +269,7 @@ pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)
269269
match args {
270270
Args {
271271
command:
272-
Some(SubCmds::Post(sub_cmd::Post {
272+
Some(Cmd::Post(cmd::post::Opt {
273273
show: false,
274274
show_meta: false,
275275
list: false,

src/args/sub_cmd.rs

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)