Skip to content

fix: list plan crash #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
flag::{Debug, Flag},
};
use clap::{crate_name, crate_version};
use log::LevelFilter;

/// This should be called before calling any cli method or printing any output.
pub fn reset_signal_pipe_handler() {
Expand Down Expand Up @@ -41,10 +42,11 @@ pub async fn main() -> Result<(), Error> {
.arg_required_else_help(true)
.get_matches();

if m.contains_id("debug") {
if m.get_flag("debug") {
Debug::handler()?;
} else {
env_logger::Builder::from_env(env_logger::Env::new().default_filter_or("info"))
env_logger::Builder::new()
.filter_level(LevelFilter::Info)
.format_timestamp(None)
.init();
}
Expand Down
8 changes: 2 additions & 6 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Command for EditCommand {
Arg::new("id")
.num_args(1)
.required(true)
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
}
Expand All @@ -51,11 +52,7 @@ impl Command for EditCommand {
use std::io::Write;
use std::path::Path;

let id: i32 = m
.get_one::<String>("id")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.parse()?;
let id = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
let cache = Cache::new()?;
let problem = cache.get_problem(id)?;
let mut conf = cache.to_owned().0.conf;
Expand All @@ -67,7 +64,6 @@ impl Command for EditCommand {
if m.contains_id("lang") {
conf.code.lang = m
.get_one::<String>("lang")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.to_string();
conf.sync()?;
Expand Down
7 changes: 2 additions & 5 deletions src/cmds/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Command for ExecCommand {
Arg::new("id")
.num_args(1)
.required(true)
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
}
Expand All @@ -41,11 +42,7 @@ impl Command for ExecCommand {
async fn handler(m: &ArgMatches) -> Result<(), crate::Error> {
use crate::cache::{Cache, Run};

let id: i32 = m
.get_one::<String>("id")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.parse()?;
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
let cache = Cache::new()?;
let res = cache.exec_problem(id, Run::Submit, None).await?;

Expand Down
22 changes: 10 additions & 12 deletions src/cmds/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl Command for ListCommand {
.short('r')
.long("range")
.num_args(2..)
.value_parser(clap::value_parser!(i32))
.help("Filter questions by id range"),
)
.after_help(LIST_AFTER_HELP)
Expand Down Expand Up @@ -148,15 +149,15 @@ impl Command for ListCommand {
#[cfg(feature = "pym")]
{
if m.contains_id("plan") {
let ids = crate::pym::exec(m.value_of("plan").unwrap_or(""))?;
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
crate::helper::squash(&mut ps, ids)?;
}
}

// filter tag
if m.contains_id("tag") {
let ids = cache
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
.await?;
crate::helper::squash(&mut ps, ids)?;
}
Expand All @@ -165,34 +166,31 @@ impl Command for ListCommand {
if m.contains_id("category") {
ps.retain(|x| {
x.category
== m.get_one::<String>("category")
.map(|s| s.as_str())
.unwrap_or("algorithms")
== *m
.get_one::<String>("category")
.unwrap_or(&"algorithms".to_string())
});
}

// filter query
if m.contains_id("query") {
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?;
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
crate::helper::filter(&mut ps, query.to_string());
}

// filter range
if m.contains_id("range") {
let num_range: Vec<i32> = m
.get_many::<String>("range")
.get_many::<i32>("range")
.ok_or(Error::NoneError)?
.map(|id| *id)
.into_iter()
.map(|x| x.parse::<i32>().unwrap_or(0))
.collect();
ps.retain(|x| num_range[0] <= x.fid && x.fid <= num_range[1]);
}

// retain if keyword exists
if let Some(keyword) = m.get_one::<String>("keyword").map(|s| s.as_str()) {
if let Some(keyword) = m.get_one::<String>("keyword") {
let lowercase_kw = keyword.to_lowercase();
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
}
Expand Down
25 changes: 11 additions & 14 deletions src/cmds/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ impl Command for PickCommand {
ClapCommand::new("pick")
.about("Pick a problem")
.visible_alias("p")
.arg(Arg::new("id").help("Problem id").num_args(1))
.arg(
Arg::new("id")
.value_parser(clap::value_parser!(i32))
.help("Problem id")
.num_args(1),
)
.arg(
Arg::new("plan")
.short('p')
Expand Down Expand Up @@ -96,11 +101,7 @@ impl Command for PickCommand {
#[cfg(feature = "pym")]
{
if m.contains_id("plan") {
let ids = crate::pym::exec(
m.get_one::<String>("plan")
.map(|s| s.as_str())
.unwrap_or(""),
)?;
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
crate::helper::squash(&mut problems, ids)?;
}
}
Expand All @@ -109,17 +110,14 @@ impl Command for PickCommand {
if m.contains_id("tag") {
let ids = cache
.clone()
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
.await?;
crate::helper::squash(&mut problems, ids)?;
}

// query filter
if m.contains_id("query") {
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?;
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
crate::helper::filter(&mut problems, query.to_string());
}

Expand All @@ -130,9 +128,8 @@ impl Command for PickCommand {
};

let fid = m
.get_one::<String>("id")
.map(|s| s.as_str())
.and_then(|id| id.parse::<i32>().ok())
.get_one::<i32>("id")
.map(|id| *id)
.or(daily_id)
.unwrap_or_else(|| {
// Pick random without specify id
Expand Down
9 changes: 3 additions & 6 deletions src/cmds/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl Command for TestCommand {
Arg::new("id")
.num_args(1)
.required(true)
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
.arg(
Expand All @@ -46,12 +47,8 @@ impl Command for TestCommand {
/// `test` handler
async fn handler(m: &ArgMatches) -> Result<(), Error> {
use crate::cache::{Cache, Run};
let id: i32 = m
.get_one::<String>("id")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.parse()?;
let testcase = m.get_one::<String>("testcase").map(|s| s.as_str());
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
let testcase = m.get_one::<String>("testcase");
let case_str: Option<String> = match testcase {
Some(case) => Option::from(case.replace("\\n", "\n")),
_ => None,
Expand Down
5 changes: 3 additions & 2 deletions src/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! -V, --version Prints version information
//! ```
use crate::err::Error;
use clap::Arg;
use clap::{Arg, ArgAction};
use env_logger::Env;

/// Abstract flag trait
Expand All @@ -25,10 +25,11 @@ impl Flag for Debug {
.short('d')
.long("debug")
.help("debug mode")
.action(ArgAction::SetTrue)
}

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

Ok(())
}
Expand Down