Skip to content
Prev Previous commit
fix: fmt fix
  • Loading branch information
wendajiang committed Jul 8, 2022
commit 8fed4a6fb971b0fa788f5ec4ca91d9b8b5c430cf
37 changes: 22 additions & 15 deletions src/cache/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ pub fn desc(q: &mut Question, v: Value) -> Option<bool> {
stats: serde_json::from_str(o.get("stats")?.as_str()?).ok()?,
defs: serde_json::from_str(o.get("codeDefinition")?.as_str()?).ok()?,
case: o.get("sampleTestCase")?.as_str()?.to_string(),
all_cases: o.get("exampleTestcases")
.unwrap_or(o.get("sampleTestCase")?) // soft fail to the sampleTestCase
.as_str()?
.to_string(),
all_cases: o
.get("exampleTestcases")
.unwrap_or(o.get("sampleTestCase")?) // soft fail to the sampleTestCase
.as_str()?
.to_string(),
metadata: serde_json::from_str(o.get("metaData")?.as_str()?).ok()?,
test: o.get("enableRunCode")?.as_bool()?,
t_content: o
Expand Down Expand Up @@ -85,29 +86,35 @@ pub fn tags(v: Value) -> Option<Vec<String>> {
Some(res)
}

/// daily parser
/// daily parser
pub fn daily(v: Value) -> Option<i32> {
trace!("Parse daily...");
v.as_object()?
.get("data")?.as_object()?
.get("activeDailyCodingChallengeQuestion")?.as_object()?
.get("question")?.as_object()?
.get("questionFrontendId")?.as_str()?
.parse().ok()
.get("data")?
.as_object()?
.get("activeDailyCodingChallengeQuestion")?
.as_object()?
.get("question")?
.as_object()?
.get("questionFrontendId")?
.as_str()?
.parse()
.ok()
}

/// user parser
pub fn user(v: Value) -> Option<Option<(String,bool)>> {
pub fn user(v: Value) -> Option<Option<(String, bool)>> {
// None => error while parsing
// Some(None) => User not found
// Some("...") => username
let user = v.as_object()?.get("data")?
.as_object()?.get("user")?;
if *user == Value::Null { return Some(None) }
let user = v.as_object()?.get("data")?.as_object()?.get("user")?;
if *user == Value::Null {
return Some(None);
}
let user = user.as_object()?;
Some(Some((
user.get("username")?.as_str()?.to_owned(),
user.get("isCurrentUserPremium")?.as_bool()?
user.get("isCurrentUserPremium")?.as_bool()?,
)))
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmds/exec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Exec command
use crate::Error;
use super::Command;
use crate::Error;
use async_trait::async_trait;
use clap::{Command as ClapCommand, ArgMatches, Arg};
use clap::{Arg, ArgMatches, Command as ClapCommand};

/// Abstract Exec Command
///
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/stat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! status command
use super::Command;
use async_trait::async_trait;
use clap::{Command as ClapCommand, ArgMatches};
use clap::{ArgMatches, Command as ClapCommand};
use colored::Colorize;

/// Abstract statues command
Expand Down
26 changes: 21 additions & 5 deletions src/plugins/leetcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl LeetCode {
.conf
.sys
.urls
.get("problems").ok_or(Error::NoneError)?
.get("problems")
.ok_or(Error::NoneError)?
.replace("$category", category);

Req {
Expand Down Expand Up @@ -110,7 +111,9 @@ impl LeetCode {

Req {
default_headers: self.default_headers,
refer: Some((self.conf.sys.urls.get("tag").ok_or(Error::NoneError)?).replace("$slug", slug)),
refer: Some(
(self.conf.sys.urls.get("tag").ok_or(Error::NoneError)?).replace("$slug", slug),
),
info: false,
json: Some(json),
mode: Mode::Post,
Expand All @@ -133,7 +136,8 @@ impl LeetCode {
username
isCurrentUserPremium
}
}".to_owned()
}"
.to_owned(),
);

Req {
Expand Down Expand Up @@ -185,7 +189,13 @@ impl LeetCode {
/// Get specific problem detail
pub async fn get_question_detail(self, slug: &str) -> Result<Response, Error> {
trace!("Requesting {} detail...", &slug);
let refer = self.conf.sys.urls.get("problems").ok_or(Error::NoneError)?.replace("$slug", slug);
let refer = self
.conf
.sys
.urls
.get("problems")
.ok_or(Error::NoneError)?
.replace("$slug", slug);
let mut json: Json = HashMap::new();
json.insert(
"query",
Expand Down Expand Up @@ -245,7 +255,13 @@ impl LeetCode {
/// Get the result of submission / testing
pub async fn verify_result(self, id: String) -> Result<Response, Error> {
trace!("Verifying result...");
let url = self.conf.sys.urls.get("verify").ok_or(Error::NoneError)?.replace("$id", &id);
let url = self
.conf
.sys
.urls
.get("verify")
.ok_or(Error::NoneError)?
.replace("$id", &id);
Req {
default_headers: self.default_headers,
refer: None,
Expand Down