leetcode_core/graphql/query/
run_code.rs

1use super::{GQLLeetcodeRequest, RunOrSubmitCodeCheckResult};
2pub use crate::types::{
3    run::{RunCodeIntermediateResponse, RunCodeRequest},
4    run_submit_response::RunSubmitResult,
5};
6use crate::{errors::LcAppError, graphql::query::console_panel_config};
7
8impl GQLLeetcodeRequest for RunCodeRequest {
9    type T = RunCodeIntermediateResponse;
10
11    fn get_endpoint(&self) -> String {
12        let slug = self.slug.as_str();
13        format!("https://leetcode.com/problems/{slug}/interpret_solution/")
14    }
15}
16
17impl GQLLeetcodeRequest for RunCodeIntermediateResponse {
18    type T = RunSubmitResult;
19    fn is_post(&self) -> bool {
20        false
21    }
22
23    fn get_endpoint(&self) -> String {
24        let interpret_id = self.interpret_id.as_str();
25        format!("https://leetcode.com/submissions/detail/{interpret_id}/check/")
26    }
27}
28
29impl RunCodeRequest {
30    pub async fn set_sample_test_cases_if_none(&mut self) -> Result<(), LcAppError> {
31        if self.test_cases_stdin.is_none() {
32            let fetched_test_cases = console_panel_config::Query::new(self.slug.clone())
33                .send()
34                .await?
35                .data
36                .question
37                .example_testcase_list
38                .join("\n");
39            self.test_cases_stdin = Some(fetched_test_cases);
40        }
41        Ok(())
42    }
43}
44
45impl RunOrSubmitCodeCheckResult<RunCodeIntermediateResponse> for RunCodeRequest {}
46
47#[cfg(test)]
48mod tests {
49    use serde_json::json;
50
51    use super::RunCodeRequest;
52
53    #[test]
54    fn test() {
55        let s = RunCodeRequest {
56            lang: crate::types::language::Language::Python3,
57            question_id: "1".to_string(),
58            typed_code: "class Solution:\n    def twoSum(self, nums: List[int], target: int) -> List[int]:    return [4]".to_string(),
59            test_cases_stdin: Some("[2,7,11,15]\n9\n[3,2,4]\n6\n[3,3]\n6".to_string()),
60            slug: "".to_string(),
61        };
62
63        let from_struct = json!(s);
64        let from_raw_json = json!(
65            {
66             "lang": "python3",
67             "question_id": "1",
68             "typed_code": "class Solution:\n    def twoSum(self, nums: List[int], target: int) -> List[int]:    return [4]",
69             "data_input": "[2,7,11,15]\n9\n[3,2,4]\n6\n[3,3]\n6"
70            }
71        );
72        assert_eq!(from_struct, from_raw_json);
73    }
74}