use std::fmt;
#[derive(Debug)]
pub enum Error {
Spawn {
path: String,
source: std::io::Error,
},
PackNotFound {
tried: Vec<String>,
},
Protocol {
expected: u32,
got: u32,
},
Handshake(String),
WorkerGone,
Desync {
expected: u64,
got: Option<u64>,
},
Worker {
kind: String,
detail: String,
},
Malformed(String),
Io(std::io::Error),
Json(serde_json::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Spawn { path, source } => {
write!(f, "could not start the judge worker ({path}): {source}")
}
Error::PackNotFound { tried } => write!(
f,
"could not find the hornguard pack; set HORNGUARD_HOME or pass a path (tried: {})",
tried.join(", ")
),
Error::Protocol { expected, got } => write!(
f,
"judge worker speaks protocol {got}, this crate speaks {expected}"
),
Error::Handshake(s) => write!(f, "judge worker did not greet us: {s}"),
Error::WorkerGone => f.write_str("the judge worker exited"),
Error::Desync { expected, got } => match got {
Some(g) => write!(f, "response id {g} for request {expected}"),
None => write!(f, "response with no id for request {expected}"),
},
Error::Worker { kind, detail } => {
write!(f, "judge worker refused the request ({kind}): {detail}")
}
Error::Malformed(s) => write!(f, "could not read the judge worker's answer: {s}"),
Error::Io(e) => write!(f, "{e}"),
Error::Json(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Spawn { source, .. } => Some(source),
Error::Io(e) => Some(e),
Error::Json(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;