diff --git a/src/cache/models.rs b/src/cache/models.rs index 0a9a6ab..f74cb28 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -290,7 +290,7 @@ impl std::fmt::Display for VerifyResult { match &self.status.status_code { 10 => { - if self.correct_answer { + if matches!(self.result_type, Run::Test) && self.correct_answer { // Pass Tests write!( f, @@ -305,9 +305,12 @@ impl std::fmt::Display for VerifyResult { &"\nExpected:".after_spaces(6), eca, )? - } else if !self.submit.compare_result.is_empty() { + } else if matches!(self.result_type, Run::Submit) + && !self.submit.compare_result.is_empty() + { + // only Submit execute this branch // Submit Successfully - // TODO: result shoule be all 1; + // TODO: result should be all 1; // Lines below are sucks... let cache = super::Cache::new().expect("cache gen failed"); cache @@ -315,7 +318,7 @@ impl std::fmt::Display for VerifyResult { self.submit .question_id .parse() - .expect("submit succcessfully, parse question_id to i32 failed"), + .expect("submit successfully, parse question_id to i32 failed"), ) .expect("update ac to cache failed"); diff --git a/src/cli.rs b/src/cli.rs index 3ec98bf..90d1756 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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() { @@ -22,7 +23,7 @@ pub fn reset_signal_pipe_handler() { } } -/// Get maches +/// Get matches pub async fn main() -> Result<(), Error> { reset_signal_pipe_handler(); let m = clap::Command::new(crate_name!()) @@ -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(); } diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 4e0e4e0..f7e490b 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -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"), ) } @@ -51,11 +52,7 @@ impl Command for EditCommand { use std::io::Write; use std::path::Path; - let id: i32 = m - .get_one::("id") - .map(|s| s.as_str()) - .ok_or(Error::NoneError)? - .parse()?; + let id = *m.get_one::("id").ok_or(Error::NoneError)?; let cache = Cache::new()?; let problem = cache.get_problem(id)?; let mut conf = cache.to_owned().0.conf; @@ -67,7 +64,6 @@ impl Command for EditCommand { if m.contains_id("lang") { conf.code.lang = m .get_one::("lang") - .map(|s| s.as_str()) .ok_or(Error::NoneError)? .to_string(); conf.sync()?; diff --git a/src/cmds/exec.rs b/src/cmds/exec.rs index 733a458..f25f9b4 100644 --- a/src/cmds/exec.rs +++ b/src/cmds/exec.rs @@ -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"), ) } @@ -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::("id") - .map(|s| s.as_str()) - .ok_or(Error::NoneError)? - .parse()?; + let id: i32 = *m.get_one::("id").ok_or(Error::NoneError)?; let cache = Cache::new()?; let res = cache.exec_problem(id, Run::Submit, None).await?; diff --git a/src/cmds/list.rs b/src/cmds/list.rs index ca9b686..f18a953 100644 --- a/src/cmds/list.rs +++ b/src/cmds/list.rs @@ -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) @@ -148,7 +149,7 @@ 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::("plan").unwrap_or(&"".to_string()))?; crate::helper::squash(&mut ps, ids)?; } } @@ -156,7 +157,7 @@ impl Command for ListCommand { // filter tag if m.contains_id("tag") { let ids = cache - .get_tagged_questions(m.get_one::("tag").map(|s| s.as_str()).unwrap_or("")) + .get_tagged_questions(m.get_one::("tag").unwrap_or(&"".to_string())) .await?; crate::helper::squash(&mut ps, ids)?; } @@ -165,34 +166,31 @@ impl Command for ListCommand { if m.contains_id("category") { ps.retain(|x| { x.category - == m.get_one::("category") - .map(|s| s.as_str()) - .unwrap_or("algorithms") + == *m + .get_one::("category") + .unwrap_or(&"algorithms".to_string()) }); } // filter query if m.contains_id("query") { - let query = m - .get_one::("query") - .map(|s| s.as_str()) - .ok_or(Error::NoneError)?; + let query = m.get_one::("query").ok_or(Error::NoneError)?; crate::helper::filter(&mut ps, query.to_string()); } // filter range if m.contains_id("range") { let num_range: Vec = m - .get_many::("range") + .get_many::("range") .ok_or(Error::NoneError)? + .map(|id| *id) .into_iter() - .map(|x| x.parse::().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::("keyword").map(|s| s.as_str()) { + if let Some(keyword) = m.get_one::("keyword") { let lowercase_kw = keyword.to_lowercase(); ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw)); } diff --git a/src/cmds/pick.rs b/src/cmds/pick.rs index e5102e8..9fc136f 100644 --- a/src/cmds/pick.rs +++ b/src/cmds/pick.rs @@ -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') @@ -96,11 +101,7 @@ impl Command for PickCommand { #[cfg(feature = "pym")] { if m.contains_id("plan") { - let ids = crate::pym::exec( - m.get_one::("plan") - .map(|s| s.as_str()) - .unwrap_or(""), - )?; + let ids = crate::pym::exec(m.get_one::("plan").unwrap_or(&"".to_string()))?; crate::helper::squash(&mut problems, ids)?; } } @@ -109,17 +110,14 @@ impl Command for PickCommand { if m.contains_id("tag") { let ids = cache .clone() - .get_tagged_questions(m.get_one::("tag").map(|s| s.as_str()).unwrap_or("")) + .get_tagged_questions(m.get_one::("tag").unwrap_or(&"".to_string())) .await?; crate::helper::squash(&mut problems, ids)?; } // query filter if m.contains_id("query") { - let query = m - .get_one::("query") - .map(|s| s.as_str()) - .ok_or(Error::NoneError)?; + let query = m.get_one::("query").ok_or(Error::NoneError)?; crate::helper::filter(&mut problems, query.to_string()); } @@ -130,9 +128,8 @@ impl Command for PickCommand { }; let fid = m - .get_one::("id") - .map(|s| s.as_str()) - .and_then(|id| id.parse::().ok()) + .get_one::("id") + .map(|id| *id) .or(daily_id) .unwrap_or_else(|| { // Pick random without specify id diff --git a/src/cmds/test.rs b/src/cmds/test.rs index d0a1578..af72014 100644 --- a/src/cmds/test.rs +++ b/src/cmds/test.rs @@ -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( @@ -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::("id") - .map(|s| s.as_str()) - .ok_or(Error::NoneError)? - .parse()?; - let testcase = m.get_one::("testcase").map(|s| s.as_str()); + let id: i32 = *m.get_one::("id").ok_or(Error::NoneError)?; + let testcase = m.get_one::("testcase"); let case_str: Option = match testcase { Some(case) => Option::from(case.replace("\\n", "\n")), _ => None, diff --git a/src/flag.rs b/src/flag.rs index cb509f9..5517153 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -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 @@ -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(()) }