use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
ProcessNotFound(u32),
NoConsole(u32),
NoWindow(u32),
HelperUnavailable(String),
HelperFailed {
code: i32,
what: &'static str,
},
Unsupported(&'static str),
Os(std::io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ProcessNotFound(pid) => write!(f, "process {pid} does not exist"),
Error::NoConsole(pid) => write!(f, "process {pid} has no console attached"),
Error::NoWindow(pid) => write!(f, "process {pid} has no visible main window"),
Error::HelperUnavailable(why) => write!(f, "helper process unavailable: {why}"),
Error::HelperFailed { code, what } => {
write!(f, "helper process failed while {what} (exit code {code})")
}
Error::Unsupported(what) => write!(f, "unsupported: {what}"),
Error::Os(e) => write!(f, "OS error: {e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Os(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Os(e)
}
}