Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 930064f

Browse files
Merge pull request clearloop#103 from wendajiang/master
fix 1. list plan crash 2. clearloop#93 3. some typo fix
2 parents d5228d9 + 0f16f5a commit 930064f

File tree

8 files changed

+43
-52
lines changed

8 files changed

+43
-52
lines changed

‎src/cache/models.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl std::fmt::Display for VerifyResult {
290290

291291
match &self.status.status_code {
292292
10 => {
293-
if self.correct_answer {
293+
if matches!(self.result_type,Run::Test) && self.correct_answer {
294294
// Pass Tests
295295
write!(
296296
f,
@@ -305,17 +305,20 @@ impl std::fmt::Display for VerifyResult {
305305
&"\nExpected:".after_spaces(6),
306306
eca,
307307
)?
308-
} else if !self.submit.compare_result.is_empty() {
308+
} else if matches!(self.result_type, Run::Submit)
309+
&& !self.submit.compare_result.is_empty()
310+
{
311+
// only Submit execute this branch
309312
// Submit Successfully
310-
// TODO: result shoule be all 1;
313+
// TODO: result should be all 1;
311314
// Lines below are sucks...
312315
let cache = super::Cache::new().expect("cache gen failed");
313316
cache
314317
.update_after_ac(
315318
self.submit
316319
.question_id
317320
.parse()
318-
.expect("submit succcessfully, parse question_id to i32 failed"),
321+
.expect("submit successfully, parse question_id to i32 failed"),
319322
)
320323
.expect("update ac to cache failed");
321324

‎src/cli.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
flag::{Debug, Flag},
99
};
1010
use clap::{crate_name, crate_version};
11+
use log::LevelFilter;
1112

1213
/// This should be called before calling any cli method or printing any output.
1314
pub fn reset_signal_pipe_handler() {
@@ -22,7 +23,7 @@ pub fn reset_signal_pipe_handler() {
2223
}
2324
}
2425

25-
/// Get maches
26+
/// Get matches
2627
pub async fn main() -> Result<(), Error> {
2728
reset_signal_pipe_handler();
2829
let m = clap::Command::new(crate_name!())
@@ -41,10 +42,11 @@ pub async fn main() -> Result<(), Error> {
4142
.arg_required_else_help(true)
4243
.get_matches();
4344

44-
if m.contains_id("debug") {
45+
if m.get_flag("debug") {
4546
Debug::handler()?;
4647
} else {
47-
env_logger::Builder::from_env(env_logger::Env::new().default_filter_or("info"))
48+
env_logger::Builder::new()
49+
.filter_level(LevelFilter::Info)
4850
.format_timestamp(None)
4951
.init();
5052
}

‎src/cmds/edit.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl Command for EditCommand {
4040
Arg::new("id")
4141
.num_args(1)
4242
.required(true)
43+
.value_parser(clap::value_parser!(i32))
4344
.help("question id"),
4445
)
4546
}
@@ -51,11 +52,7 @@ impl Command for EditCommand {
5152
use std::io::Write;
5253
use std::path::Path;
5354

54-
let id: i32 = m
55-
.get_one::<String>("id")
56-
.map(|s| s.as_str())
57-
.ok_or(Error::NoneError)?
58-
.parse()?;
55+
let id = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
5956
let cache = Cache::new()?;
6057
let problem = cache.get_problem(id)?;
6158
let mut conf = cache.to_owned().0.conf;
@@ -67,7 +64,6 @@ impl Command for EditCommand {
6764
if m.contains_id("lang") {
6865
conf.code.lang = m
6966
.get_one::<String>("lang")
70-
.map(|s| s.as_str())
7167
.ok_or(Error::NoneError)?
7268
.to_string();
7369
conf.sync()?;

‎src/cmds/exec.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Command for ExecCommand {
3333
Arg::new("id")
3434
.num_args(1)
3535
.required(true)
36+
.value_parser(clap::value_parser!(i32))
3637
.help("question id"),
3738
)
3839
}
@@ -41,11 +42,7 @@ impl Command for ExecCommand {
4142
async fn handler(m: &ArgMatches) -> Result<(), crate::Error> {
4243
use crate::cache::{Cache, Run};
4344

44-
let id: i32 = m
45-
.get_one::<String>("id")
46-
.map(|s| s.as_str())
47-
.ok_or(Error::NoneError)?
48-
.parse()?;
45+
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
4946
let cache = Cache::new()?;
5047
let res = cache.exec_problem(id, Run::Submit, None).await?;
5148

‎src/cmds/list.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl Command for ListCommand {
102102
.short('r')
103103
.long("range")
104104
.num_args(2..)
105+
.value_parser(clap::value_parser!(i32))
105106
.help("Filter questions by id range"),
106107
)
107108
.after_help(LIST_AFTER_HELP)
@@ -148,15 +149,15 @@ impl Command for ListCommand {
148149
#[cfg(feature = "pym")]
149150
{
150151
if m.contains_id("plan") {
151-
let ids = crate::pym::exec(m.value_of("plan").unwrap_or(""))?;
152+
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
152153
crate::helper::squash(&mut ps, ids)?;
153154
}
154155
}
155156

156157
// filter tag
157158
if m.contains_id("tag") {
158159
let ids = cache
159-
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
160+
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
160161
.await?;
161162
crate::helper::squash(&mut ps, ids)?;
162163
}
@@ -165,34 +166,31 @@ impl Command for ListCommand {
165166
if m.contains_id("category") {
166167
ps.retain(|x| {
167168
x.category
168-
== m.get_one::<String>("category")
169-
.map(|s| s.as_str())
170-
.unwrap_or("algorithms")
169+
== *m
170+
.get_one::<String>("category")
171+
.unwrap_or(&"algorithms".to_string())
171172
});
172173
}
173174

174175
// filter query
175176
if m.contains_id("query") {
176-
let query = m
177-
.get_one::<String>("query")
178-
.map(|s| s.as_str())
179-
.ok_or(Error::NoneError)?;
177+
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
180178
crate::helper::filter(&mut ps, query.to_string());
181179
}
182180

183181
// filter range
184182
if m.contains_id("range") {
185183
let num_range: Vec<i32> = m
186-
.get_many::<String>("range")
184+
.get_many::<i32>("range")
187185
.ok_or(Error::NoneError)?
186+
.map(|id| *id)
188187
.into_iter()
189-
.map(|x| x.parse::<i32>().unwrap_or(0))
190188
.collect();
191189
ps.retain(|x| num_range[0] <= x.fid && x.fid <= num_range[1]);
192190
}
193191

194192
// retain if keyword exists
195-
if let Some(keyword) = m.get_one::<String>("keyword").map(|s| s.as_str()) {
193+
if let Some(keyword) = m.get_one::<String>("keyword") {
196194
let lowercase_kw = keyword.to_lowercase();
197195
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
198196
}

‎src/cmds/pick.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ impl Command for PickCommand {
4747
ClapCommand::new("pick")
4848
.about("Pick a problem")
4949
.visible_alias("p")
50-
.arg(Arg::new("id").help("Problem id").num_args(1))
50+
.arg(
51+
Arg::new("id")
52+
.value_parser(clap::value_parser!(i32))
53+
.help("Problem id")
54+
.num_args(1),
55+
)
5156
.arg(
5257
Arg::new("plan")
5358
.short('p')
@@ -96,11 +101,7 @@ impl Command for PickCommand {
96101
#[cfg(feature = "pym")]
97102
{
98103
if m.contains_id("plan") {
99-
let ids = crate::pym::exec(
100-
m.get_one::<String>("plan")
101-
.map(|s| s.as_str())
102-
.unwrap_or(""),
103-
)?;
104+
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
104105
crate::helper::squash(&mut problems, ids)?;
105106
}
106107
}
@@ -109,17 +110,14 @@ impl Command for PickCommand {
109110
if m.contains_id("tag") {
110111
let ids = cache
111112
.clone()
112-
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
113+
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
113114
.await?;
114115
crate::helper::squash(&mut problems, ids)?;
115116
}
116117

117118
// query filter
118119
if m.contains_id("query") {
119-
let query = m
120-
.get_one::<String>("query")
121-
.map(|s| s.as_str())
122-
.ok_or(Error::NoneError)?;
120+
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
123121
crate::helper::filter(&mut problems, query.to_string());
124122
}
125123

@@ -130,9 +128,8 @@ impl Command for PickCommand {
130128
};
131129

132130
let fid = m
133-
.get_one::<String>("id")
134-
.map(|s| s.as_str())
135-
.and_then(|id| id.parse::<i32>().ok())
131+
.get_one::<i32>("id")
132+
.map(|id| *id)
136133
.or(daily_id)
137134
.unwrap_or_else(|| {
138135
// Pick random without specify id

‎src/cmds/test.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Command for TestCommand {
3333
Arg::new("id")
3434
.num_args(1)
3535
.required(true)
36+
.value_parser(clap::value_parser!(i32))
3637
.help("question id"),
3738
)
3839
.arg(
@@ -46,12 +47,8 @@ impl Command for TestCommand {
4647
/// `test` handler
4748
async fn handler(m: &ArgMatches) -> Result<(), Error> {
4849
use crate::cache::{Cache, Run};
49-
let id: i32 = m
50-
.get_one::<String>("id")
51-
.map(|s| s.as_str())
52-
.ok_or(Error::NoneError)?
53-
.parse()?;
54-
let testcase = m.get_one::<String>("testcase").map(|s| s.as_str());
50+
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
51+
let testcase = m.get_one::<String>("testcase");
5552
let case_str: Option<String> = match testcase {
5653
Some(case) => Option::from(case.replace("\\n", "\n")),
5754
_ => None,

‎src/flag.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! -V, --version Prints version information
88
//! ```
99
use crate::err::Error;
10-
use clap::Arg;
10+
use clap::{Arg,ArgAction};
1111
use env_logger::Env;
1212

1313
/// Abstract flag trait
@@ -25,10 +25,11 @@ impl Flag for Debug {
2525
.short('d')
2626
.long("debug")
2727
.help("debug mode")
28+
.action(ArgAction::SetTrue)
2829
}
2930

3031
fn handler() -> Result<(), Error> {
31-
env_logger::Builder::from_env(Env::default().default_filter_or("leetcode")).init();
32+
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
3233

3334
Ok(())
3435
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /