use std::fmt;
use std::io;
use std::time::Duration;
use crate::report::Report;
#[derive(Debug)]
pub struct Failure {
pub message: String,
pub exit_code: i32,
pub report: Option<Box<Report>>,
pub stderr: String,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Failed(Failure),
Usage(Failure),
ProblemsFound(Failure),
BinaryNotFound(String),
InvalidArgument(String),
Timeout(Duration),
Io(io::Error),
}
impl Error {
pub fn failure(&self) -> Option<&Failure> {
match self {
Error::Failed(f) | Error::Usage(f) | Error::ProblemsFound(f) => Some(f),
_ => None,
}
}
pub fn report(&self) -> Option<&Report> {
self.failure().and_then(|f| f.report.as_deref())
}
pub fn exit_code(&self) -> Option<i32> {
self.failure().map(|f| f.exit_code)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Failed(x) | Error::Usage(x) | Error::ProblemsFound(x) => f.write_str(&x.message),
Error::BinaryNotFound(m) | Error::InvalidArgument(m) => f.write_str(m),
Error::Timeout(d) => write!(f, "postext did not finish within {:.1} s", d.as_secs_f64()),
Error::Io(e) => write!(f, "postext: {e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;