ac-algorithm 0.2.0

Algorithm competition domain logic for ac
Documentation
//! Verdict vocabulary for the Algorithm domain.
//!
//! Two distinct verdict families exist and must never be conflated
//! (ADR-0005 invariant 1): [`JudgeVerdict`] mirrors judge/official result
//! codes, while [`LocalVerdict`] describes local test runs only. A local
//! pass is never an official acceptance.

use serde::{Deserialize, Serialize};

/// Judge-side verdict codes, mirroring `AtCoder`'s result notation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JudgeVerdict {
    /// Accepted.
    Accepted,
    /// Wrong answer.
    WrongAnswer,
    /// Time limit exceeded.
    TimeLimitExceeded,
    /// Memory limit exceeded.
    MemoryLimitExceeded,
    /// Runtime error (including crashes and forbidden syscalls).
    RuntimeError,
    /// Compilation error.
    CompilationError,
    /// Internal error on the judge side.
    InternalError,
}

impl JudgeVerdict {
    /// Returns the canonical two-letter code used by `AtCoder`.
    #[must_use]
    pub const fn as_code(self) -> &'static str {
        match self {
            Self::Accepted => "AC",
            Self::WrongAnswer => "WA",
            Self::TimeLimitExceeded => "TLE",
            Self::MemoryLimitExceeded => "MLE",
            Self::RuntimeError => "RE",
            Self::CompilationError => "CE",
            Self::InternalError => "IE",
        }
    }

    /// Parses a verdict code. Unknown or empty codes return `None`; callers
    /// decide how to surface unrecognized official values.
    #[must_use]
    pub fn from_code(code: &str) -> Option<Self> {
        match code {
            "AC" => Some(Self::Accepted),
            "WA" => Some(Self::WrongAnswer),
            "TLE" => Some(Self::TimeLimitExceeded),
            "MLE" => Some(Self::MemoryLimitExceeded),
            "RE" => Some(Self::RuntimeError),
            "CE" => Some(Self::CompilationError),
            "IE" => Some(Self::InternalError),
            _ => None,
        }
    }
}

/// Why a local run could not produce a pass/fail verdict. Carried next to
/// [`LocalVerdict::Error`] so execution-check failures keep their RE/TLE/
/// MLE/CE distinction without ever borrowing judge verdict codes
/// (ADR-0005 invariant 1).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LocalFailure {
    /// The solution failed to compile.
    Compile,
    /// The process exited non-zero or died on a signal.
    Runtime,
    /// The wall-clock limit was exceeded.
    TimeLimit,
    /// Peak memory exceeded the problem's memory limit.
    MemoryLimit,
    /// The combined output cap was exceeded.
    OutputLimit,
    /// The run was cancelled.
    Cancelled,
    /// Harness or infrastructure failure (spawn, I/O, lost job).
    Harness,
}

/// Outcome of a local test run. Deliberately separate from
/// [`JudgeVerdict`]: these describe what the local harness observed, not
/// what the official judge decided.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LocalVerdict {
    /// Output matched the expected output ("local case pass").
    CasePass,
    /// The program ran to completion but no expected output exists, so only
    /// execution was verified ("execution check pass").
    ExecutionPass,
    /// Output differed from the expected output or a checker rejected it.
    CaseFail,
    /// Compilation, runtime, or harness failure prevented a verdict.
    Error,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn judge_verdict_codes_roundtrip() {
        for verdict in [
            JudgeVerdict::Accepted,
            JudgeVerdict::WrongAnswer,
            JudgeVerdict::TimeLimitExceeded,
            JudgeVerdict::MemoryLimitExceeded,
            JudgeVerdict::RuntimeError,
            JudgeVerdict::CompilationError,
            JudgeVerdict::InternalError,
        ] {
            assert_eq!(JudgeVerdict::from_code(verdict.as_code()), Some(verdict));
        }
    }

    #[test]
    fn judge_verdict_rejects_unknown_codes() {
        assert_eq!(JudgeVerdict::from_code(""), None);
        assert_eq!(JudgeVerdict::from_code("AC "), None);
        assert_eq!(JudgeVerdict::from_code("OK"), None);
    }
}