Skip to content

Commit

Permalink
Merge pull request #128 from traP-jp/feat/#127-stdout-interpreter
Browse files Browse the repository at this point in the history
✨ judge output parsing logic
  • Loading branch information
comavius authored Feb 18, 2025
2 parents 963dd1d + 5ad580c commit 65b7930
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/judge_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
serde ={ workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
anyhow = { workspace = true }
futures = { workspace = true }
Expand Down
63 changes: 63 additions & 0 deletions lib/judge_core/src/judge_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JudgeStatus {
AC,
WA,
TLE,
MLE,
OLE,
RE,
CE,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ContinueStatus {
Continue,
Stop,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DisplayableJudgeResult {
pub status: JudgeStatus,
pub time: f64,
pub memory: f64,
pub score: i64,
pub message: Option<String>,
pub continue_status: ContinueStatus,
}

/// This returns from exec container as stdout
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JudgeReport {
/// Frontend-displayable execution result
Displayable(DisplayableJudgeResult),
/// Not displayed to frontend (e.g. for validation)
Hidden,
}

/// This is the final response from judge
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExecutionResponse {
Report(JudgeReport),
EarlyExit,
Error(String),
}

#[derive(Debug, Clone, thiserror::Error)]
pub enum JudgeOutputParseError {
#[error("Invalid JSON: {0}")]
InvalidJson(String),
#[error("Non-zero exit code")]
NonZeroExitCode,
}

pub fn parse(output: &std::process::Output) -> Result<JudgeReport, JudgeOutputParseError> {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
if !output.status.success() {
return Err(JudgeOutputParseError::NonZeroExitCode);
}
let judge_report: JudgeReport = serde_json::from_str(&stdout)
.map_err(|e| JudgeOutputParseError::InvalidJson(e.to_string()))?;
Ok(judge_report)
}
1 change: 1 addition & 0 deletions lib/judge_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod identifiers;
pub mod job;
pub mod judge_output;
pub mod problem_registry;
pub mod procedure;
pub mod runner;
Expand Down

0 comments on commit 65b7930

Please sign in to comment.