diff --git a/Cargo.lock b/Cargo.lock index e9e1ca3c..f399ad9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -432,6 +432,7 @@ name = "leetcode-rust" version = "0.1.0" dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.18 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c414242f..ccb27ffe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ serde = "1.0" serde_json = "1.0" serde_derive = "1.0" rand = "0.6.5" +regex = "1" [lib] doctest = false diff --git a/src/main.rs b/src/main.rs index 5460227b..8bac6d7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,15 @@ extern crate serde_derive; #[macro_use] extern crate serde_json; - +extern crate regex; mod problem; +use regex::Regex; use std::env; use std::fs; -use std::path::{Path}; -use std::io::Write; use std::io; +use std::io::Write; +use std::path::Path; /// main() helps to generate the submission template .rs fn main() { @@ -18,9 +19,10 @@ fn main() { loop { println!("Please enter a problem id, or enter \"random\" to generate a random problem."); let mut is_random = false; - let mut id :u32 = 0; + let mut id: u32 = 0; let mut id_arg = String::new(); - io::stdin().read_line(&mut id_arg) + io::stdin() + .read_line(&mut id_arg) .expect("Failed to read line"); let id_arg = id_arg.trim(); match id_arg { @@ -29,23 +31,30 @@ fn main() { id = generate_random_id(&solved_ids); is_random = true; println!("Generate random problem: {}", &id); - }, + } _ => { - id = id_arg.parse::().expect(&format!("not a number: {}", id_arg)); + id = id_arg + .parse::() + .expect(&format!("not a number: {}", id_arg)); if solved_ids.contains(&id) { - println!("The problem you chose is invalid (the problem may have been solved \ - or may have no rust version)."); + println!( + "The problem you chose is invalid (the problem may have been solved \ + or may have no rust version)." + ); continue; } } } - let problem = problem::get_problem(id) - .expect(&format!("Error: failed to get problem #{} \ - (The problem may be paid-only or may not be exist).", - id)); - let code = problem.code_definition.iter() - .filter(|&d| { d.value == "rust" }) + let problem = problem::get_problem(id).expect(&format!( + "Error: failed to get problem #{} \ + (The problem may be paid-only or may not be exist).", + id + )); + let code = problem + .code_definition + .iter() + .filter(|&d| d.value == "rust") .next(); if code.is_none() { println!("Problem {} has no rust version.", &id); @@ -88,17 +97,20 @@ fn main() { } } -fn generate_random_id(except_ids : &Vec) -> u32 { - use std::fs; +fn generate_random_id(except_ids: &Vec) -> u32 { use rand::Rng; + use std::fs; let mut rng = rand::thread_rng(); loop { - let res :u32 = rng.gen_range(1, 1106); + let res: u32 = rng.gen_range(1, 1106); if !except_ids.contains(&res) { return res; } - println!("Generate a random num ({}), but it is invalid (the problem may have been solved \ - or may have no rust version). Regenerate..", res); + println!( + "Generate a random num ({}), but it is invalid (the problem may have been solved \ + or may have no rust version). Regenerate..", + res + ); } } @@ -136,7 +148,7 @@ fn parse_extra_use(code: &str) -> String { fn build_desc(content: &str) -> String { // TODO: fix this shit - content + let content = content .replace("", "") .replace("", "") .replace("", "") @@ -147,6 +159,7 @@ fn build_desc(content: &str) -> String { .replace("", "") .replace("
", "")
         .replace("
", "") + .replace("", "") .replace("
    ", "") .replace("
", "") .replace("
  • ", "") @@ -166,5 +179,10 @@ fn build_desc(content: &str) -> String { .replace("−", "-") .replace("'", "'") .replace("\n\n", "\n") - .replace("\n", "\n * ") + .replace("\n", "\n * "); + + Regex::new("") + .unwrap() + .replace_all(&content, "") + .to_string() } diff --git a/src/problem.rs b/src/problem.rs index ba51f04f..04f219eb 100644 --- a/src/problem.rs +++ b/src/problem.rs @@ -1,7 +1,7 @@ -extern crate serde_json; extern crate reqwest; +extern crate serde_json; -use std::fmt::{Display, Formatter, Error}; +use std::fmt::{Display, Error, Formatter}; const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/"; const GRAPHQL_URL: &str = "https://leetcode.com/graphql"; @@ -21,24 +21,28 @@ pub fn get_problem(id: u32) -> Option { let problems = get_problems().unwrap(); for problem in problems.stat_status_pairs.iter() { if problem.stat.question_id == id { - if problem.paid_only { - return None + return None; } let client = reqwest::Client::new(); - let resp: RawProblem = client.post(GRAPHQL_URL) - .json(&Query::question_query(problem.stat.question_title_slug.as_ref().unwrap())) - .send().unwrap() - .json().unwrap(); + let resp: RawProblem = client + .post(GRAPHQL_URL) + .json(&Query::question_query( + problem.stat.question_title_slug.as_ref().unwrap(), + )) + .send() + .unwrap() + .json() + .unwrap(); return Some(Problem { title: problem.stat.question_title.clone().unwrap(), title_slug: problem.stat.question_title_slug.clone().unwrap(), - code_definition: serde_json::from_str( & resp.data.question.code_definition).unwrap(), + code_definition: serde_json::from_str(&resp.data.question.code_definition).unwrap(), content: resp.data.question.content, sample_test_case: resp.data.question.sample_test_case, difficulty: problem.difficulty.to_string(), - }) + }); } } None @@ -68,7 +72,6 @@ pub struct CodeDefinition { pub default_code: String, } - #[derive(Debug, Serialize, Deserialize)] struct Query { #[serde(rename = "operationName")] @@ -81,7 +84,7 @@ impl Query { fn question_query(title_slug: &str) -> Query { Query { operation_name: QUESTION_QUERY_OPERATION.to_owned(), - variables: json!({"titleSlug": title_slug}), + variables: json!({ "titleSlug": title_slug }), query: QUESTION_QUERY_STRING.to_owned(), } }