//! The only impure part: read lines from stdin, write lines to stdout. use rechner::game::Decision; use rechner::wire::encode::encode; use rechner::wire::respond::respond; use std::io::{self, BufRead, Write}; use std::panic::{catch_unwind, set_hook, AssertUnwindSafe}; /// A panic anywhere in the core still yields a valid ABSTAIN line. fn guarded(line: &str) -> String { catch_unwind(AssertUnwindSafe(|| respond(line))) .unwrap_or_else(|_| encode(&Decision::abstain("internal error"))) } fn serve(input: impl BufRead, mut output: impl Write) -> io::Result<()> { for line in input.lines() { let line = line?; if line.trim().is_empty() { continue; } writeln!(output, "{}", guarded(&line))?; output.flush()?; } Ok(()) } fn main() { set_hook(Box::new(|_| {})); let stdin = io::stdin(); let stdout = io::stdout(); if let Err(error) = serve(stdin.lock(), stdout.lock()) { eprintln!("io error: {error}"); std::process::exit(1); } }