Skip to content

Commit 674e0ee

Browse files
committed
Support picking question via name
1 parent 930064f commit 674e0ee

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/cache/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ impl Cache {
119119
Ok(p)
120120
}
121121

122+
/// Get problem from name
123+
pub fn get_problem_from_name(&self, problem_name: &String) -> Result<Problem, Error> {
124+
let p: Problem = problems
125+
.filter(name.eq(problem_name))
126+
.first(&self.conn()?)?;
127+
if p.category != "algorithms" {
128+
return Err(Error::FeatureError(
129+
"Not support database and shell questions for now".to_string(),
130+
));
131+
}
132+
Ok(p)
133+
}
134+
122135
/// Get daily problem
123136
pub async fn get_daily_problem_id(&self) -> Result<i32, Error> {
124137
parser::daily(

src/cmds/pick.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ impl Command for PickCommand {
4747
ClapCommand::new("pick")
4848
.about("Pick a problem")
4949
.visible_alias("p")
50+
.arg(
51+
Arg::new("name")
52+
.short('n')
53+
.long("name")
54+
.value_parser(clap::value_parser!(String))
55+
.help("Problem name")
56+
.num_args(1),
57+
)
5058
.arg(
5159
Arg::new("id")
5260
.value_parser(clap::value_parser!(i32))
@@ -127,15 +135,32 @@ impl Command for PickCommand {
127135
None
128136
};
129137

130-
let fid = m
131-
.get_one::<i32>("id")
132-
.map(|id| *id)
133-
.or(daily_id)
134-
.unwrap_or_else(|| {
135-
// Pick random without specify id
136-
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
137-
problem.fid
138-
});
138+
let fid = match m.contains_id("name") {
139+
//check for name specified
140+
true => {
141+
match m.get_one::<String>("name").map(|name| name) {
142+
Some(quesname) => match cache.get_problem_from_name(quesname) {
143+
Ok(p) => p.fid,
144+
Err(_) => 1,
145+
},
146+
None => {
147+
// Pick random without specify id
148+
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
149+
problem.fid
150+
}
151+
}
152+
}
153+
false => {
154+
m.get_one::<i32>("id")
155+
.map(|id| *id)
156+
.or(daily_id)
157+
.unwrap_or_else(|| {
158+
// Pick random without specify id
159+
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
160+
problem.fid
161+
})
162+
}
163+
};
139164

140165
let r = cache.get_question(fid).await;
141166

0 commit comments

Comments
 (0)