use crate::sys;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
Hresult(i32),
OpenFailed(i32),
InteriorNul,
Closed,
}
impl Error {
#[inline]
pub(crate) fn from_hresult(hr: sys::HRESULT) -> Result<()> {
if hr >= 0 {
Ok(())
} else {
Err(Error::Hresult(hr as i32))
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Hresult(hr) => write!(f, "SimConnect HRESULT 0x{:08X}", *hr as u32),
Error::OpenFailed(hr) => {
write!(f, "SimConnect_Open failed with HRESULT 0x{:08X}", *hr as u32)
}
Error::InteriorNul => write!(f, "string contained an interior NUL byte"),
Error::Closed => write!(f, "SimConnect handle has been closed"),
}
}
}
impl std::error::Error for Error {}
impl From<std::ffi::NulError> for Error {
fn from(_: std::ffi::NulError) -> Self {
Error::InteriorNul
}
}