use crate::bindings;
extern "C" {
pub fn __errno() -> *mut i32;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum PROSErr {
None = 0,
NoEntry = 2,
IO = 5,
NXIO = 6,
Again = 11,
Access = 13,
Busy = 16,
Exists = 17,
NoDev = 19,
Invalid = 22,
ReadOnlyFS = 30,
NoMoreFiles = 89,
NoBuffSpace = 105,
AddrInUse = 112,
}
pub fn from_errno() -> PROSErr {
unsafe {
let errno: *const i32 = __errno() as *const i32;
*(errno as *const PROSErr) }
}
pub trait PROSResult: Sized {
fn check(self) -> Result<Self, PROSErr>;
}
impl PROSResult for i32 {
fn check(self) -> Result<Self, PROSErr> {
if self == bindings::PROS_ERR_ {
Err(from_errno())
} else {
Ok(self)
}
}
}
impl PROSResult for f64 {
fn check(self) -> Result<Self, PROSErr> {
if self == bindings::PROS_ERR_F_ {
Err(from_errno())
} else {
Ok(self)
}
}
}
impl<T> PROSResult for *mut T {
fn check(self) -> Result<Self, PROSErr> {
if self.is_null() {
Err(from_errno())
} else {
Ok(self)
}
}
}