use lliw::Fg::Red;
use lliw::Reset;
use std::fmt::{Display, Formatter};
use std::num::ParseIntError;
#[macro_export]
macro_rules! pass {
() => {
Ok(())
};
($item:expr) => {{
Ok($item)
}};
}
#[macro_export]
macro_rules! throw {
($kind:expr, $fmt:literal) => ({
return $crate::error::_throw($kind, std::format!($fmt))
});
($kind:expr, $fmt:literal, $($arg:tt)*) => ({
return $crate::error::_throw($kind, std::format!($fmt, $($arg)*))
})
}
#[doc(hidden)]
pub fn _throw<T>(kind: Kind, inner: String) -> Result<T, Error> {
Err(Error::new(kind, inner))
}
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Error {
inner: String,
kind: Kind,
}
impl Error {
#[must_use]
pub fn new(kind: Kind, inner: String) -> Error {
Error { inner, kind }
}
#[must_use]
pub fn kind(&self) -> Kind {
self.kind
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}: {}{}", Red, self.kind, Reset, self.inner)
}
}
impl From<ParseIntError> for Error {
fn from(pie: ParseIntError) -> Self {
Error::new(Kind::VersionParse, pie.to_string())
}
}
impl From<toml::de::Error> for Error {
fn from(te: toml::de::Error) -> Self {
Error::new(Kind::ConfigParse, te.to_string())
}
}
impl From<jargon_args::Error> for Error {
fn from(jae: jargon_args::Error) -> Self {
match jae {
jargon_args::Error::MissingArg(key) => {
Error::new(Kind::ArgumentMissing, key.to_string())
}
jargon_args::Error::Other(s) => Error::new(Kind::JargonInternal, s),
}
}
}
impl std::error::Error for Error {}
#[repr(i32)]
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Kind {
Internal = 1,
Environment,
ConfigOpen,
ConfigRead,
ConfigParse,
ProtonDir,
ProtonSpawn,
ProtonWait,
ProtonMissing,
ProgramMissing,
IndexReadDir,
VersionParse,
ProtonExit,
ArgumentMissing,
JargonInternal,
IndexCache,
ParseRuntimeOpt,
RuntimeMissing,
}
impl Display for Kind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Kind::Internal => "internal error",
Kind::Environment => "failed to read environment",
Kind::ConfigOpen => "failed to open config",
Kind::ConfigRead => "failed to read config",
Kind::ConfigParse => "failed to parse config",
Kind::ProtonDir => "failed to create Proton directory",
Kind::ProtonSpawn => "failed to spawn Proton",
Kind::ProtonWait => "failed to wait for Proton child",
Kind::IndexReadDir => "failed to Index",
Kind::VersionParse => "failed to parse version",
Kind::ProtonMissing => "cannot find Proton",
Kind::ProgramMissing => "cannot find program",
Kind::ProtonExit => "proton exited with",
Kind::ArgumentMissing => "missing command line argument",
Kind::JargonInternal => "jargon args internal error",
Kind::IndexCache => "failed read/write to cache",
Kind::ParseRuntimeOpt => "failed parsing runtime option",
Kind::RuntimeMissing => "failed to find Runtime",
}
)
}
}