use serde::{Deserialize, Serialize};
use super::{GQLLeetcodeQuery, Language};
use crate::deserializers::run_submit::RunResponse;
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct SubmitCode {
pub lang: Language,
pub question_id: String,
pub typed_code: String,
#[serde(skip_serializing, skip_deserializing)]
pub slug: String,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct SubmitCodeResponse {
submission_id: u32,
}
impl GQLLeetcodeQuery for SubmitCode {
type T = SubmitCodeResponse;
fn get_endpoint(&self) -> String {
let slug = self.slug.as_str();
format!("https://leetcode.com/problems/{slug}/submit/")
}
}
impl GQLLeetcodeQuery for SubmitCodeResponse {
type T = RunResponse;
fn is_post(&self) -> bool {
false
}
fn get_endpoint(&self) -> String {
let submission_id = self.submission_id;
format!("https://leetcode.com/submissions/detail/{submission_id}/check/")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let request_json_data = r#"
{
"lang": "python3",
"question_id": "1",
"typed_code": "class Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]: return [1]"
}
"#;
let request_body: SubmitCode = serde_json::from_str(request_json_data).unwrap();
println!("Request Body: {:?}", request_body);
let expected_request_body = SubmitCode {
lang: super::super::Language::Python3,
question_id: "1".to_string(),
typed_code: "class Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]: return [1]".to_string(),
slug: "".to_string()
};
assert_eq!(request_body, expected_request_body);
let response_json_data = r#"
{
"submission_id": 1001727658
}
"#;
let response_body: SubmitCodeResponse = serde_json::from_str(response_json_data).unwrap();
println!("Response Body: {:?}", response_body);
let expected_response_body = SubmitCodeResponse {
submission_id: 1001727658,
};
assert_eq!(response_body, expected_response_body);
}
}