|
| 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 | +} |
0 commit comments