Skip to content

Commit 65b7930

Browse files
authored
Merge pull request #128 from traP-jp/feat/#127-stdout-interpreter
✨ judge output parsing logic
2 parents 963dd1d + 5ad580c commit 65b7930

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/judge_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
serde ={ workspace = true }
8+
serde_json = { workspace = true }
89
thiserror = { workspace = true }
910
anyhow = { workspace = true }
1011
futures = { workspace = true }

lib/judge_core/src/judge_output.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Clone, Serialize, Deserialize)]
4+
pub enum JudgeStatus {
5+
AC,
6+
WA,
7+
TLE,
8+
MLE,
9+
OLE,
10+
RE,
11+
CE,
12+
}
13+
14+
#[derive(Debug, Clone, Serialize, Deserialize)]
15+
pub enum ContinueStatus {
16+
Continue,
17+
Stop,
18+
}
19+
20+
#[derive(Debug, Clone, Serialize, Deserialize)]
21+
pub struct DisplayableJudgeResult {
22+
pub status: JudgeStatus,
23+
pub time: f64,
24+
pub memory: f64,
25+
pub score: i64,
26+
pub message: Option<String>,
27+
pub continue_status: ContinueStatus,
28+
}
29+
30+
/// This returns from exec container as stdout
31+
#[derive(Debug, Clone, Serialize, Deserialize)]
32+
pub enum JudgeReport {
33+
/// Frontend-displayable execution result
34+
Displayable(DisplayableJudgeResult),
35+
/// Not displayed to frontend (e.g. for validation)
36+
Hidden,
37+
}
38+
39+
/// This is the final response from judge
40+
#[derive(Debug, Clone, Serialize, Deserialize)]
41+
pub enum ExecutionResponse {
42+
Report(JudgeReport),
43+
EarlyExit,
44+
Error(String),
45+
}
46+
47+
#[derive(Debug, Clone, thiserror::Error)]
48+
pub enum JudgeOutputParseError {
49+
#[error("Invalid JSON: {0}")]
50+
InvalidJson(String),
51+
#[error("Non-zero exit code")]
52+
NonZeroExitCode,
53+
}
54+
55+
pub fn parse(output: &std::process::Output) -> Result<JudgeReport, JudgeOutputParseError> {
56+
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
57+
if !output.status.success() {
58+
return Err(JudgeOutputParseError::NonZeroExitCode);
59+
}
60+
let judge_report: JudgeReport = serde_json::from_str(&stdout)
61+
.map_err(|e| JudgeOutputParseError::InvalidJson(e.to_string()))?;
62+
Ok(judge_report)
63+
}

lib/judge_core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod identifiers;
22
pub mod job;
3+
pub mod judge_output;
34
pub mod problem_registry;
45
pub mod procedure;
56
pub mod runner;

0 commit comments

Comments
 (0)