Skip to content

Commit

Permalink
feat(quiz): added backend codes for quiz based envs
Browse files Browse the repository at this point in the history
  • Loading branch information
rootCircle committed Dec 2, 2023
1 parent 90fcf77 commit 0666af9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
8 changes: 2 additions & 6 deletions .idea/workspace.xml

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

5 changes: 3 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lettre = { version = "0.10.4", features = ["tokio1", "tokio1-native-tls"] }
serde = "1.0.189"
tower-http = { version = "0.4.4", features = ["full"] }
http = "0.2.9"
zen = {git = "https://github.com/zenlang-rs/zen-lang.git", branch = "main", version = "^0.2.0"}
zen = {git = "https://github.com/zenlang-rs/zen-lang.git", branch = "main", version = "^0.2.1"}
jsonwebtoken = "9.1.0"
once_cell = "1.18.0"
shuttle-persist = "0.34.1"
Expand Down
83 changes: 47 additions & 36 deletions src/controllers/compile_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,21 @@ pub struct CodeOutputResponse {
pub output: Result<String, String>,
}

// #[derive(Deserialize)]
// struct CodeQuizRequest {
// pub code: String,
// pub testcases: Vec<Testcase>
// }
//
// struct Testcase {
// pub input: String,
// pub expected_output: String
// }
//
// #[derive(Serialize)]
// struct QuizResponse {
// pub output_match: Vec<Result<bool, String>>
// }
#[derive(Deserialize)]
pub struct CodeQuizRequest {
pub code: String,
pub testcases: Vec<Testcase>
}

#[derive(Deserialize)]
pub struct Testcase {
pub input: String,
pub expected_output: String
}

fn runnable_code(code: String, input: &str) -> Result<String, String> {
let runnable = run_program(code, input);
match runnable {
Ok(output) => Ok(output),
Err(err) => Err(format!("[ERROR]\n{}\n{}", err.msg, err.error_type)),
}
#[derive(Serialize)]
pub struct QuizResponse {
pub output_match: Vec<Result<bool, String>>
}

pub async fn compile_code(
Expand All @@ -44,18 +36,37 @@ pub async fn compile_code(
})
}

// async fn take_quiz(
// extract::Json(user): extract::Json<CodeQuizRequest>,
// ) -> Json<QuizResponse> {
// let output_match =
// Json(QuizResponse {
// output_match: ,
// })
// }
//
// fn match_outputs(code: String, testcases: Vec<Testcase>) -> Vec<Result<bool, String>> {
// for testcase in testcases.iter() {
// runnable_code(code, testcase.input)
// }
// vec![]
// }
pub async fn take_quiz(
extract::Json(user): extract::Json<CodeQuizRequest>,
) -> Json<QuizResponse> {
Json(QuizResponse {
output_match: match_outputs(user.code, user.testcases),
})
}


fn runnable_code(code: String, input: &str) -> Result<String, String> {
let runnable = run_program(code, input, false);
match runnable {
Ok(output) => Ok(output),
Err(err) => Err(format!("[ERROR]\n{}\n{}", err.msg, err.error_type)),
}
}

fn match_outputs(code: String, testcases: Vec<Testcase>) -> Vec<Result<bool, String>> {
let mut output_vec: Vec<Result<bool, String>> = vec![];

for testcase in testcases.iter() {
let code_instance = runnable_code(code.clone(), &testcase.input);

match code_instance {
Ok(actual_output) => {
output_vec.push(Ok(actual_output == testcase.expected_output))
}
Err(err) => {
output_vec.push(Err(err))
}
}
}
output_vec
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async fn axum(#[shuttle_persist::Persist] persist: PersistInstance) -> shuttle_a
let api_router = Router::new()
.route("/health", get(api_health))
.route("/compile", post(controllers::compile_code::compile_code))
.route("/quiz", post(controllers::compile_code::take_quiz))
.merge(auth_routes(persist));

let router = Router::new().nest("/api", api_router).layer(cors);
Expand Down

0 comments on commit 0666af9

Please sign in to comment.