use crate::{fmt, io::ErrorKind};
use super::abi;
#[derive(Debug, Copy, Clone)]
pub struct ItronError {
er: abi::ER,
}
impl ItronError {
#[inline]
pub fn new(er: abi::ER) -> Option<Self> {
if er < 0 { Some(Self { er }) } else { None }
}
#[inline]
pub fn err_if_negative(er: abi::ER) -> Result<abi::ER, Self> {
if let Some(error) = Self::new(er) { Err(error) } else { Ok(er) }
}
#[inline]
pub fn as_raw(&self) -> abi::ER {
self.er
}
}
impl fmt::Display for ItronError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(name) = crate::sys::error::error_name(self.er) {
write!(f, "{} ({})", name, self.er)
} else {
write!(f, "{}", self.er)
}
}
}
pub fn error_name(er: abi::ER) -> Option<&'static str> {
match er {
er if er >= 0 => None,
abi::E_SYS => Some("system error"),
abi::E_NOSPT => Some("unsupported function"),
abi::E_RSFN => Some("reserved function code"),
abi::E_RSATR => Some("reserved attribute"),
abi::E_PAR => Some("parameter error"),
abi::E_ID => Some("invalid ID number"),
abi::E_CTX => Some("context error"),
abi::E_MACV => Some("memory access violation"),
abi::E_OACV => Some("object access violation"),
abi::E_ILUSE => Some("illegal service call use"),
abi::E_NOMEM => Some("insufficient memory"),
abi::E_NOID => Some("no ID number available"),
abi::E_OBJ => Some("object state error"),
abi::E_NOEXS => Some("non-existent object"),
abi::E_QOVR => Some("queue overflow"),
abi::E_RLWAI => Some("forced release from waiting"),
abi::E_TMOUT => Some("polling failure or timeout"),
abi::E_DLT => Some("waiting object deleted"),
abi::E_CLS => Some("waiting object state changed"),
abi::E_WBLK => Some("non-blocking code accepted"),
abi::E_BOVR => Some("buffer overflow"),
abi::E_NORES => Some("insufficient system resources"),
abi::E_RASTER => Some("termination request raised"),
abi::E_COMM => Some("communication failure"),
_ => None,
}
}
pub fn decode_error_kind(er: abi::ER) -> ErrorKind {
match er {
er if er >= 0 => ErrorKind::Uncategorized,
abi::E_NOSPT => ErrorKind::Unsupported, abi::E_RSFN => ErrorKind::InvalidInput, abi::E_RSATR => ErrorKind::InvalidInput, abi::E_PAR => ErrorKind::InvalidInput, abi::E_ID => ErrorKind::NotFound, abi::E_MACV => ErrorKind::PermissionDenied, abi::E_OACV => ErrorKind::PermissionDenied, abi::E_NOMEM => ErrorKind::OutOfMemory, abi::E_NOID => ErrorKind::OutOfMemory, abi::E_NOEXS => ErrorKind::NotFound, abi::E_RLWAI => ErrorKind::Interrupted, abi::E_TMOUT => ErrorKind::TimedOut,
abi::E_NORES => ErrorKind::OutOfMemory, _ => ErrorKind::Uncategorized,
}
}
#[inline]
pub fn expect_success(er: abi::ER, msg: &&str) -> abi::ER {
match ItronError::err_if_negative(er) {
Ok(x) => x,
Err(e) => fail(e, msg),
}
}
#[inline]
pub fn expect_success_aborting(er: abi::ER, msg: &&str) -> abi::ER {
match ItronError::err_if_negative(er) {
Ok(x) => x,
Err(e) => fail_aborting(e, msg),
}
}
#[cold]
pub fn fail(e: impl fmt::Display, msg: &&str) -> ! {
if crate::thread::panicking() {
fail_aborting(e, msg)
} else {
panic!("{} failed: {}", *msg, e)
}
}
#[cold]
pub fn fail_aborting(e: impl fmt::Display, msg: &&str) -> ! {
rtabort!("{} failed: {}", *msg, e)
}