ac-algorithm 0.2.0

Algorithm competition domain logic for ac
Documentation
//! cargo-compete-compatible testcase YAML import (issue #26, AC-05).
//!
//! ac-test wrote `testcases/<alias>.yml` documents of the form:
//!
//! ```yaml
//! # <problem URL>
//! ---
//! type: Batch
//! timelimit: 2s
//! match: Lines
//! cases:
//!   - name: sample1
//!     in: |
//!       ...
//!     out: |
//!       ...
//! extend: []
//! ```
//!
//! This module parses that shape leniently: only `cases[].name`,
//! `cases[].in`, and optional `cases[].out` are required; every other
//! key is ignored. Cases without `out` stay execution-check only —
//! they are never treated as correctness passes (ADR-0005).

use serde::Deserialize;

/// One `cases:` entry from a cargo-compete testcase YAML document.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct YamlCase {
    /// Case name (`sample1`, `generated-min`, …).
    pub name: String,
    /// stdin payload (the `in:` block scalar).
    #[serde(rename = "in")]
    pub input: String,
    /// Expected stdout (the `out:` block scalar). `None` means the case
    /// carries no expected output and is an execution check only.
    #[serde(rename = "out", default)]
    pub output: Option<String>,
}

/// Top-level document; unknown keys (`type`, `timelimit`, `match`,
/// `extend`, comments) are ignored for forward compatibility.
#[derive(Debug, Deserialize)]
struct Document {
    /// Case list; absent or empty yields no cases.
    #[serde(default)]
    cases: Vec<YamlCase>,
}

/// Parses a cargo-compete testcase YAML document into its cases.
///
/// # Errors
///
/// Returns a description when the text is not valid YAML or the
/// `cases` entries do not match the expected shape.
pub fn parse_cases(yaml: &str) -> Result<Vec<YamlCase>, String> {
    let document: Document =
        serde_yaml::from_str(yaml).map_err(|err| format!("cannot parse testcase YAML: {err}"))?;
    for case in &document.cases {
        if case.name.trim().is_empty() {
            return Err("testcase YAML contains a case with an empty name".to_owned());
        }
    }
    Ok(document.cases)
}

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

    const SAMPLE: &str = r#"# https://atcoder.jp/contests/abc351/tasks/abc351_a
---
type: Batch
timelimit: 2s
match: Lines

cases:
  - name: sample1
    in: |
      3
    out: |
      6
  - name: generated-min
    in: |
      1
      1 2 3
  - name: edge
    in: "5\n"
    out: ""

extend: []
"#;

    #[test]
    fn parses_cargo_compete_yaml() {
        let cases = parse_cases(SAMPLE).expect("parse");
        assert_eq!(cases.len(), 3);
        assert_eq!(cases[0].name, "sample1");
        assert_eq!(cases[0].input, "3\n");
        assert_eq!(cases[0].output.as_deref(), Some("6\n"));
        // Generated cases carry no `out` → execution check only.
        assert_eq!(cases[1].output, None);
        // Empty-string `out` is still an expected output.
        assert_eq!(cases[2].output.as_deref(), Some(""));
    }

    #[test]
    fn rejects_malformed_yaml() {
        assert!(parse_cases("cases: [unclosed").is_err());
        assert!(parse_cases("cases:\n  - in: |\n    x\n").is_err());
        assert!(parse_cases("cases:\n  - name: \"\"\n    in: x\n").is_err());
    }
}