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 e3eb5ef

Browse files
Merge pull request clearloop#15 from hulufei/master
Handle new added problems properly
2 parents 70eab63 + 2dfb18c commit e3eb5ef

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

‎src/cache/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ impl Cache {
4747

4848
/// ref to download probems
4949
pub fn update(self) -> Result<(), Error> {
50-
let c = conn((&self.0.conf.storage.cache()?).to_owned());
51-
let ps = self.download_problems()?;
52-
for i in ps.into_iter() {
53-
let target = problems.filter(id.eq(i.id));
54-
diesel::update(target).set(i.to_owned()).execute(&c)?;
55-
}
56-
50+
self.download_problems()?;
5751
Ok(())
5852
}
5953

@@ -74,13 +68,9 @@ impl Cache {
7468
parser::problem(&mut ps, json)?;
7569
}
7670

77-
let count = self.get_problems()?.len();
78-
if count == 0 {
79-
ps.sort_by(|a, b| b.id.partial_cmp(&a.id).unwrap_or(std::cmp::Ordering::Equal));
80-
diesel::insert_into(problems)
81-
.values(&ps)
82-
.execute(&self.conn()?)?;
83-
}
71+
diesel::replace_into(problems)
72+
.values(&ps)
73+
.execute(&self.conn()?)?;
8474

8575
Ok(ps)
8676
}

‎src/cmds/pick.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ impl Command for PickCommand {
107107
crate::helper::filter(&mut problems, query.to_string());
108108
}
109109

110-
if let Some(id) = m.value_of("id") {
111-
problems.retain(|x| x.fid.to_string() == id);
112-
}
113-
114-
let problem = &problems[rand::thread_rng().gen_range(0, problems.len())];
110+
let fid = m
111+
.value_of("id")
112+
.and_then(|id| id.parse::<i32>().ok())
113+
.unwrap_or_else(|| {
114+
// Pick random without specify id
115+
let problem = &problems[rand::thread_rng().gen_range(0, problems.len())];
116+
problem.fid
117+
});
115118

116-
let r = cache.get_question(problem.fid);
119+
let r = cache.get_question(fid);
117120
if r.is_err() {
118121
let e = r.err()?;
119122
eprintln!("{:?}", &e);

‎src/err.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Errors in leetcode-cli
2+
use crate::cmds::{Command, DataCommand};
23
use colored::Colorize;
34
use std::fmt;
45

@@ -14,6 +15,7 @@ pub enum Error {
1415
ScriptError(String),
1516
CookieError,
1617
DecryptError,
18+
SilentError,
1719
}
1820

1921
impl std::fmt::Debug for Error {
@@ -40,6 +42,7 @@ impl std::fmt::Debug for Error {
4042
Error::MatchError => write!(f, "{} Nothing matches", e),
4143
Error::DecryptError => write!(f, "{} openssl decrypt failed", e),
4244
Error::ScriptError(s) => write!(f, "{} {}", e, s),
45+
Error::SilentError => write!(f, ""),
4346
}
4447
}
4548
}
@@ -61,7 +64,14 @@ impl std::convert::From<std::num::ParseIntError> for Error {
6164
// sql
6265
impl std::convert::From<diesel::result::Error> for Error {
6366
fn from(err: diesel::result::Error) -> Self {
64-
Error::CacheError(err.to_string())
67+
match err {
68+
diesel::result::Error::NotFound => {
69+
println!("NotFound, you may update cache, and try it again\r\n");
70+
DataCommand::usage().print_help().unwrap_or(());
71+
Error::SilentError
72+
}
73+
_ => Error::CacheError(err.to_string()),
74+
}
6575
}
6676
}
6777

0 commit comments

Comments
(0)

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