From 5e466d5b01bd7ab67ec1b9e0d6c8ec5c8fa9f69b Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Sun, 25 Jun 2023 20:50:19 +0530 Subject: [PATCH 1/6] Fix rebasing conflicts --- src/cache/mod.rs | 13 +++++++++++++ src/cmds/pick.rs | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index b15f677..09fdc6e 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -122,6 +122,19 @@ impl Cache { Ok(p) } + /// Get problem from name + pub fn get_problem_from_name(&self, problem_name: &String) -> Result { + let p: Problem = problems + .filter(name.eq(problem_name)) + .first(&self.conn()?)?; + if p.category != "algorithms" { + return Err(Error::FeatureError( + "Not support database and shell questions for now".to_string(), + )); + } + Ok(p) + } + /// Get daily problem pub async fn get_daily_problem_id(&self) -> Result { parser::daily( diff --git a/src/cmds/pick.rs b/src/cmds/pick.rs index a814143..1903825 100644 --- a/src/cmds/pick.rs +++ b/src/cmds/pick.rs @@ -47,6 +47,14 @@ impl Command for PickCommand { ClapCommand::new("pick") .about("Pick a problem") .visible_alias("p") + .arg( + Arg::new("name") + .short('n') + .long("name") + .value_parser(clap::value_parser!(String)) + .help("Problem name") + .num_args(1), + ) .arg( Arg::new("id") .value_parser(clap::value_parser!(i32)) @@ -127,15 +135,32 @@ impl Command for PickCommand { None }; - let fid = m - .get_one::("id") - .copied() - .or(daily_id) - .unwrap_or_else(|| { - // Pick random without specify id - let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; - problem.fid - }); + let fid = match m.contains_id("name") { + //check for name specified + true => { + match m.get_one::("name").map(|name| name) { + Some(quesname) => match cache.get_problem_from_name(quesname) { + Ok(p) => p.fid, + Err(_) => 1, + }, + None => { + // Pick random without specify id + let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; + problem.fid + } + } + } + false => { + m.get_one::("id") + .copied() + .or(daily_id) + .unwrap_or_else(|| { + // Pick random without specify id + let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; + problem.fid + }); + } + }; let r = cache.get_question(fid).await; From 609931c2015b013314a3a89cdc08126e2075c1d0 Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Thu, 16 Feb 2023 08:04:05 +0530 Subject: [PATCH 2/6] Include README example to pick by name --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index fe8e0dd..4e68afc 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,10 @@ scripts = 'scripts' leetcode pick 1 ``` +```sh +leetcode pick --name "Two Sum" +``` + ```sh [1] Two Sum is on the run... From 06df7ead2075fba1c5330bf69deb3cb3d75f335a Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Thu, 16 Feb 2023 08:15:11 +0530 Subject: [PATCH 3/6] Only return question ID when searching by name --- src/cache/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 09fdc6e..f5787ef 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -123,7 +123,7 @@ impl Cache { } /// Get problem from name - pub fn get_problem_from_name(&self, problem_name: &String) -> Result { + pub fn get_problem_id_from_name(&self, problem_name: &String) -> Result { let p: Problem = problems .filter(name.eq(problem_name)) .first(&self.conn()?)?; @@ -132,7 +132,7 @@ impl Cache { "Not support database and shell questions for now".to_string(), )); } - Ok(p) + Ok(p.fid) } /// Get daily problem From 319055b6595a15d358e8a772efe99d8ea0f37fad Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Thu, 16 Feb 2023 08:18:56 +0530 Subject: [PATCH 4/6] Clean up ID selecting code --- src/cmds/pick.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cmds/pick.rs b/src/cmds/pick.rs index 1903825..1ade824 100644 --- a/src/cmds/pick.rs +++ b/src/cmds/pick.rs @@ -159,10 +159,17 @@ impl Command for PickCommand { let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; problem.fid }); + + let id = match fid { + Ok(id) => id, + Err(_) => { + // Pick random without specify id + let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; + problem.fid } }; - let r = cache.get_question(fid).await; + let r = cache.get_question(id).await; match r { Ok(q) => println!("{}", q.desc()), From 17fcf9578a043067b70be8a49ad97692fd882f1f Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Sun, 25 Jun 2023 20:57:18 +0530 Subject: [PATCH 5/6] fix: use get_problem_id_from_name() --- src/cmds/pick.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/cmds/pick.rs b/src/cmds/pick.rs index 1ade824..967e205 100644 --- a/src/cmds/pick.rs +++ b/src/cmds/pick.rs @@ -139,8 +139,8 @@ impl Command for PickCommand { //check for name specified true => { match m.get_one::("name").map(|name| name) { - Some(quesname) => match cache.get_problem_from_name(quesname) { - Ok(p) => p.fid, + Some(quesname) => match cache.get_problem_id_from_name(quesname) { + Ok(p) => p, Err(_) => 1, }, None => { @@ -158,18 +158,11 @@ impl Command for PickCommand { // Pick random without specify id let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; problem.fid - }); - - let id = match fid { - Ok(id) => id, - Err(_) => { - // Pick random without specify id - let problem = &problems[rand::thread_rng().gen_range(0..problems.len())]; - problem.fid + }) } }; - let r = cache.get_question(id).await; + let r = cache.get_question(fid).await; match r { Ok(q) => println!("{}", q.desc()), From 259edce932d84be1cbdee1127c547b6ab03403e5 Mon Sep 17 00:00:00 2001 From: Saksham Mittal Date: Sun, 25 Jun 2023 21:06:26 +0530 Subject: [PATCH 6/6] fix: use mutable reference instead when filtering --- src/cache/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index f5787ef..03e138e 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -126,7 +126,7 @@ impl Cache { pub fn get_problem_id_from_name(&self, problem_name: &String) -> Result { let p: Problem = problems .filter(name.eq(problem_name)) - .first(&self.conn()?)?; + .first(&mut self.conn()?)?; if p.category != "algorithms" { return Err(Error::FeatureError( "Not support database and shell questions for now".to_string(),