use super::dacecore::*;
use std::error::Error;
use std::ffi::CStr;
use std::fmt::Display;
#[derive(Debug)]
pub struct DACEException {
pub err: u32,
pub msg: String,
pub fun: String,
}
impl DACEException {
fn generate() -> DACEException {
unsafe {
let err = daceGetError();
let msg = CStr::from_ptr(daceGetErrorMSG())
.to_str()
.unwrap_or("Unable to decode error message")
.to_string();
let fun = CStr::from_ptr(daceGetErrorFunName())
.to_str()
.unwrap_or("Unable to decode error function name")
.to_string();
daceClearError();
DACEException { err, msg, fun }
}
}
}
impl Display for DACEException {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Exception #{} \"{}\" in DACE Core function {}",
self.err, self.msg, self.fun
)
}
}
impl Error for DACEException {}
#[inline(always)]
pub fn check_exception() -> Result<(), DACEException> {
if unsafe { daceGetError() } == 0 {
Ok(())
} else {
Err(DACEException::generate())
}
}
#[inline(always)]
pub fn check_exception_panic() {
check_exception().unwrap()
}