Skip to content

Commit

Permalink
feat(leetcode-api): add append last test case function.
Browse files Browse the repository at this point in the history
  • Loading branch information
saying121 committed May 28, 2024
1 parent 8574729 commit 7fb1894
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 19 deletions.
25 changes: 12 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion crates/leetcode-api/src/dao/save_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
render::Render,
};

/// write info to file
/// Contains file's info,
/// Useful for write some content to question's files.
#[derive(Clone)]
#[derive(Debug)]
#[derive(Default)]
Expand All @@ -25,6 +26,29 @@ pub struct FileInfo {
pub content_path: PathBuf,
}

impl FileInfo {
/// When submit have testcase failed, can call it.
pub async fn append_test_case(&self, case: &str) -> Result<()> {
if case.is_empty() {
return Ok(());
}

let mut f = OpenOptions::new()
.append(true)
.open(&self.test_case_path)
.await
.into_diagnostic()?;

f.write_all(b"\n")
.await
.into_diagnostic()?;
f.write_all(case.as_bytes())
.await
.into_diagnostic()?;
Ok(())
}
}

impl FileInfo {
/// Get code, test, content dir
#[instrument]
Expand Down Expand Up @@ -63,6 +87,7 @@ impl FileInfo {
content_path,
})
}

/// Write a question's `content`, `code` and `test_case` to file
pub async fn write_to_file(&self, detail: &Question) -> Result<()> {
let content = detail.to_md_str(true);
Expand Down
24 changes: 20 additions & 4 deletions crates/leetcode-api/src/leetcode/impl_lc/judge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl LeetCode {
},
};

let last_sub_result = self
.get_one_submit_res(&sub_info)
.await?;
let last_sub_result = self.get_submit_res(&sub_info).await?;
debug!("last submit result: {:#?}", last_sub_result);

Ok((sub_info, last_sub_result))
Expand All @@ -70,7 +68,7 @@ impl LeetCode {
/// Get one submit info
///
/// * `sub_id`: be fetch `submission_id`
pub async fn get_one_submit_res(&self, sub_id: &SubmitInfo) -> Result<RunResult> {
pub async fn get_submit_res(&self, sub_id: &SubmitInfo) -> Result<RunResult> {
let test_res_url = G_USER_CONFIG
.urls
.mod_submissions(&sub_id.submission_id().to_string());
Expand All @@ -93,6 +91,24 @@ impl LeetCode {
)
.build())
}
pub async fn add_last_test_case(&self, submit_res: &RunResult) -> Result<()> {
let case = &submit_res.last_testcase;
if case.is_empty() {
return Ok(());
}
let pb = Query::get_question_index(&IdSlug::Id(
submit_res
.question_id
.parse()
.expect("submit res question id parse error"),
))
.await?;

let info = FileInfo::build(&pb).await?;
info.append_test_case(case).await?;

Ok(())
}

/// Get all submission results for a question
pub async fn all_submit_res(&self, idslug: IdSlug) -> Result<SubmissionList> {
Expand Down
3 changes: 2 additions & 1 deletion crates/leetcode-api/tests/lc_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ async fn submit_work() {

let (_, res) = glob_leetcode()
.await
.submit_code(IdSlug::Id(27))
.submit_code(IdSlug::Id(100345))
.await
.unwrap();
dbg!(&res);
dbg!(res.to_para_vec());
println!(r##"(| res |) -> {} "##, res.to_md_str(false));
res.render_with_mdcat();
Expand Down
43 changes: 43 additions & 0 deletions sample/submit/SubmissionDetail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,46 @@ correct
},
)


----


[crates/leetcode-api/tests/lc_manual.rs:42:5] &res = RunResult {
elapsed_time: 0,
finished: true,
task_name: "judger.judgetask.Judge",
status_code: 20,
status_msg: "Compile Error",
question_id: "100345",
std_output: "",
expected_output: "",
last_testcase: "",
code_answer: [],
compare_result: "",
correct_answer: false,
expected_code_answer: [],
expected_code_output: [],
pretty_lang: "Rust",
lang: "rust",
memory: 0,
status_memory: "N/A",
memory_percentile: None,
status_runtime: "N/A",
runtime_percentile: None,
run_success: false,
state: "SUCCESS",
std_output_list: [],
submission_id: "535418998",
task_finish_time: 1716882420090,
total_correct: None,
total_testcases: None,
full_runtime_error: "",
runtime_error: "",
compile_error: "Line 46: Char 29: error: no function or associated item named `mechanical_accumulator` found for struct `Solution` in the current scop
e (solution.rs)",
full_compile_error: "Line 46: Char 29: error: no function or associated item named `mechanical_accumulator` found for struct `Solution` in the current
scope (solution.rs)\n |\n31 | struct Solution;\n | --------------- function or associated item `mechanical_accumulator` not found for this struct\n..
.\n46 | let ret = Solution::mechanical_accumulator(param_1);\n | ^^^^^^^^^^^^^^^^^^^^^^ function or associated item
not found in `Solution`\nFor more information about this error, try `rustc --explain E0599`.\nerror: could not compile `prog` (bin \"prog\") due to previo
us error",
}

0 comments on commit 7fb1894

Please sign in to comment.