20241024 finished.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::error::Error;
|
||||
use super::{Fetcher, Problem, Problems, Query};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use super::{Problem, Problems, Query, Fetcher};
|
||||
use std::error::Error;
|
||||
|
||||
const PROBLEMS_URL: &str = "https://leetcode.cn/api/problems/algorithms/";
|
||||
const GRAPHQL_URL: &str = "https://leetcode.cn/graphql";
|
||||
@@ -8,16 +8,12 @@ const GRAPHQL_URL: &str = "https://leetcode.cn/graphql";
|
||||
impl Fetcher {
|
||||
pub fn new() -> Fetcher {
|
||||
Fetcher {
|
||||
client: reqwest::Client::new()
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_problems(&self) -> Result<Problems, reqwest::Error> {
|
||||
Ok(reqwest::get(PROBLEMS_URL)
|
||||
.await?
|
||||
.json()
|
||||
.await?
|
||||
)
|
||||
Ok(reqwest::get(PROBLEMS_URL).await?.json().await?)
|
||||
}
|
||||
|
||||
pub async fn get_problem(self, question_id: u32) -> Result<Problem, Box<dyn Error>> {
|
||||
@@ -28,19 +24,18 @@ impl Fetcher {
|
||||
Ok(id) => {
|
||||
if id == question_id {
|
||||
if problem.paid_only {
|
||||
return Err("failed to get paid only problem".into())
|
||||
return Err("failed to get paid only problem".into());
|
||||
}
|
||||
|
||||
let query = match &problem.stat.question_title_slug {
|
||||
None => {
|
||||
Err::<Query, Box<dyn Error>>("failed to get problem title slug".into())
|
||||
}
|
||||
Some(value) => {
|
||||
Ok(Query::new(value.as_str()))
|
||||
}
|
||||
None => Err::<Query, Box<dyn Error>>(
|
||||
"failed to get problem title slug".into(),
|
||||
),
|
||||
Some(value) => Ok(Query::new(value.as_str())),
|
||||
}?;
|
||||
|
||||
let response = self.client
|
||||
let response = self
|
||||
.client
|
||||
.post(GRAPHQL_URL)
|
||||
.json(&query)
|
||||
.send()
|
||||
@@ -48,20 +43,24 @@ impl Fetcher {
|
||||
.json::<RawProblem>()
|
||||
.await?;
|
||||
|
||||
let title = problem.stat.question_title.clone()
|
||||
let title = problem
|
||||
.stat
|
||||
.question_title
|
||||
.clone()
|
||||
.ok_or::<Box<dyn Error>>("failed to get problem title".into())?;
|
||||
let title_slug = problem.stat.question_title_slug.clone()
|
||||
let title_slug = problem
|
||||
.stat
|
||||
.question_title_slug
|
||||
.clone()
|
||||
.ok_or::<Box<dyn Error>>("failed to get problem title slug".into())?;
|
||||
let return_type = {
|
||||
let v = serde_json::from_str::<serde_json::Value>(
|
||||
&response.data.question.meta_data);
|
||||
v.and_then(|x| {
|
||||
return Ok(x.to_string().replace("\"", ""))
|
||||
})
|
||||
&response.data.question.meta_data,
|
||||
);
|
||||
v.and_then(|x| return Ok(x.to_string().replace("\"", "")))
|
||||
}?;
|
||||
let code_definition = serde_json::from_str(
|
||||
&response.data.question.code_definition
|
||||
)?;
|
||||
let code_definition =
|
||||
serde_json::from_str(&response.data.question.code_definition)?;
|
||||
|
||||
return Ok(Problem {
|
||||
title,
|
||||
@@ -69,8 +68,8 @@ impl Fetcher {
|
||||
code_definition,
|
||||
content: response.data.question.content,
|
||||
question_id: id,
|
||||
return_type
|
||||
})
|
||||
return_type,
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(_) => {}
|
||||
@@ -101,4 +100,4 @@ struct Question {
|
||||
sample_test_case: String,
|
||||
#[serde(rename = "metaData")]
|
||||
meta_data: String,
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use super::{Problem, ProblemManager};
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use regex::{Regex};
|
||||
|
||||
impl ProblemManager {
|
||||
pub fn scan() -> Result<ProblemManager, Box<dyn std::error::Error>> {
|
||||
@@ -11,45 +11,43 @@ impl ProblemManager {
|
||||
for i in pattern.captures_iter(&mod_content) {
|
||||
match i.get(1) {
|
||||
None => {}
|
||||
Some(value) => {
|
||||
match value.as_str().parse::<u32>() {
|
||||
Ok(id) => {
|
||||
problems.push(id);
|
||||
}
|
||||
Err(_) => {}
|
||||
Some(value) => match value.as_str().parse::<u32>() {
|
||||
Ok(id) => {
|
||||
problems.push(id);
|
||||
}
|
||||
}
|
||||
Err(_) => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ProblemManager {
|
||||
problem_list: problems
|
||||
problem_list: problems,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Problem {
|
||||
pub fn get_filename(&self) -> String {
|
||||
format!("p{}_{}", self.question_id, self.title_slug.replace('-', "_"))
|
||||
format!(
|
||||
"p{}_{}",
|
||||
self.question_id,
|
||||
self.title_slug.replace('-', "_")
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_file_content(&self) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let template = fs::read_to_string("./template.rs")?;
|
||||
|
||||
let code = self.code_definition
|
||||
.iter()
|
||||
.find(|x| x.value == "rust");
|
||||
let code = self.code_definition.iter().find(|x| x.value == "rust");
|
||||
|
||||
let code = code.ok_or::<Box<dyn std::error::Error>>(
|
||||
format!("problem {} doesn't have rust version", self.question_id).into()
|
||||
format!("problem {} doesn't have rust version", self.question_id).into(),
|
||||
)?;
|
||||
|
||||
let source = template
|
||||
.replace("__PROBLEM_TITLE__", &self.title)
|
||||
.replace("__PROBLEM_ID__", self.question_id.to_string().as_str())
|
||||
.replace(
|
||||
"__PROBLEM_DEFAULT_CODE__",
|
||||
&code.default_code)
|
||||
.replace("__PROBLEM_DEFAULT_CODE__", &code.default_code)
|
||||
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
|
||||
|
||||
Ok(source)
|
||||
@@ -69,4 +67,4 @@ fn parse_extra_use(code: &str) -> String {
|
||||
extra_use_line.push_str("\nuse crate::util::point::Point;")
|
||||
}
|
||||
extra_use_line
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user