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 2dfb18c

Browse files
committed
Handle new added problems properly
1 parent f43e67d commit 2dfb18c

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
@@ -41,13 +41,7 @@ impl Cache {
4141

4242
/// ref to download probems
4343
pub fn update(self) -> Result<(), Error> {
44-
let c = conn((&self.0.conf.storage.cache()?).to_owned());
45-
let ps = self.download_problems()?;
46-
for i in ps.into_iter() {
47-
let target = problems.filter(id.eq(i.id));
48-
diesel::update(target).set(i.to_owned()).execute(&c)?;
49-
}
50-
44+
self.download_problems()?;
5145
Ok(())
5246
}
5347

@@ -68,13 +62,9 @@ impl Cache {
6862
parser::problem(&mut ps, json)?;
6963
}
7064

71-
let count = self.get_problems()?.len();
72-
if count == 0 {
73-
ps.sort_by(|a, b| b.id.partial_cmp(&a.id).unwrap_or(std::cmp::Ordering::Equal));
74-
diesel::insert_into(problems)
75-
.values(&ps)
76-
.execute(&self.conn()?)?;
77-
}
65+
diesel::replace_into(problems)
66+
.values(&ps)
67+
.execute(&self.conn()?)?;
7868

7969
Ok(ps)
8070
}

‎src/cmds/pick.rs

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

103-
if let Some(id) = m.value_of("id") {
104-
problems.retain(|x| x.fid.to_string() == id);
105-
}
106-
107-
let problem = &problems[rand::thread_rng().gen_range(0, problems.len())];
103+
let fid = m
104+
.value_of("id")
105+
.and_then(|id| id.parse::<i32>().ok())
106+
.unwrap_or_else(|| {
107+
// Pick random without specify id
108+
let problem = &problems[rand::thread_rng().gen_range(0, problems.len())];
109+
problem.fid
110+
});
108111

109-
let r = cache.get_question(problem.fid);
112+
let r = cache.get_question(fid);
110113
if r.is_err() {
111114
let e = r.err()?;
112115
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 によって変換されたページ (->オリジナル) /