From 7fb1894b425a1d9df234d85a6e416df7115e6641 Mon Sep 17 00:00:00 2001 From: saying Date: Tue, 28 May 2024 16:18:25 +0800 Subject: [PATCH] feat(leetcode-api): add append last test case function. --- Cargo.lock | 25 ++++++----- crates/leetcode-api/src/dao/save_info.rs | 27 +++++++++++- .../src/leetcode/impl_lc/judge.rs | 24 +++++++++-- crates/leetcode-api/tests/lc_manual.rs | 3 +- sample/submit/SubmissionDetail.txt | 43 +++++++++++++++++++ 5 files changed, 103 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb36a63..97c7a89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -467,12 +467,11 @@ dependencies = [ [[package]] name = "blocking" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "495f7104e962b7356f0aeb34247aca1fe7d2e783b346582db7f2904cb5717e88" +checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea" dependencies = [ "async-channel", - "async-lock 3.3.0", "async-task", "futures-io", "futures-lite 2.3.0", @@ -492,9 +491,9 @@ dependencies = [ [[package]] name = "brotli-decompressor" -version = "4.0.0" +version = "4.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6221fe77a248b9117d431ad93761222e1cf8ff282d9d1d5d9f53d6299a1cf76" +checksum = "9a45bd2e4095a8b518033b128020dd4a55aab1c0a381ba4404a472630f4bc362" dependencies = [ "alloc-no-stdlib", "alloc-stdlib", @@ -2695,9 +2694,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.83" +version = "1.0.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43" +checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6" dependencies = [ "unicode-ident", ] @@ -3205,18 +3204,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", @@ -4699,9 +4698,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" [[package]] name = "zvariant" diff --git a/crates/leetcode-api/src/dao/save_info.rs b/crates/leetcode-api/src/dao/save_info.rs index 4b3fd7e..4842ce6 100644 --- a/crates/leetcode-api/src/dao/save_info.rs +++ b/crates/leetcode-api/src/dao/save_info.rs @@ -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)] @@ -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] @@ -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); diff --git a/crates/leetcode-api/src/leetcode/impl_lc/judge.rs b/crates/leetcode-api/src/leetcode/impl_lc/judge.rs index 71df8d3..225cec7 100644 --- a/crates/leetcode-api/src/leetcode/impl_lc/judge.rs +++ b/crates/leetcode-api/src/leetcode/impl_lc/judge.rs @@ -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)) @@ -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 { + pub async fn get_submit_res(&self, sub_id: &SubmitInfo) -> Result { let test_res_url = G_USER_CONFIG .urls .mod_submissions(&sub_id.submission_id().to_string()); @@ -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 { diff --git a/crates/leetcode-api/tests/lc_manual.rs b/crates/leetcode-api/tests/lc_manual.rs index e0ff54b..9cd57d2 100644 --- a/crates/leetcode-api/tests/lc_manual.rs +++ b/crates/leetcode-api/tests/lc_manual.rs @@ -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(); diff --git a/sample/submit/SubmissionDetail.txt b/sample/submit/SubmissionDetail.txt index 0efcb4d..4e60e47 100644 --- a/sample/submit/SubmissionDetail.txt +++ b/sample/submit/SubmissionDetail.txt @@ -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", +}