1use alloc::{format, string::*};
4use core::{
5 fmt::{self, Debug, Display, Formatter},
6 num::TryFromIntError,
7};
8use cstring_interop::from_cstring_raw;
9
10use crate::bindings;
11
12pub enum Error {
14 System(i32),
18 Custom(String),
21}
22
23impl From<rcstring::Error> for Error {
24 fn from(err: rcstring::Error) -> Self {
25 Error::Custom(format!("{:?}", err))
26 }
27}
28
29impl From<TryFromIntError> for Error {
30 fn from(err: TryFromIntError) -> Self {
31 Error::Custom(format!("{:?}", err))
32 }
33}
34
35impl<T> From<Error> for Result<T, Error> {
36 #[inline]
37 fn from(err: Error) -> Self {
38 Err(err)
39 }
40}
41
42impl Debug for Error {
43 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44 match self {
45 Error::System(n) => write!(f, "System({}) [{}]", n, unsafe {
46 from_cstring_raw(libc::strerror(*n))
47 }),
48 Error::Custom(s) => write!(f, "Custom({:?})", s),
49 }
50 }
51}
52
53impl Display for Error {
54 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
55 match self {
56 Error::System(n) => Display::fmt(unsafe { &from_cstring_raw(libc::strerror(*n)) }, f),
57 Error::Custom(s) => Display::fmt(s, f),
58 }
59 }
60}
61
62pub trait SentinelError: Sized {
67 fn check(self) -> Result<Self, Error>;
70}
71
72impl SentinelError for i32 {
73 fn check(self) -> Result<Self, Error> {
74 if self == bindings::PROS_ERR_ {
75 Err(from_errno())
76 } else {
77 Ok(self)
78 }
79 }
80}
81
82impl SentinelError for f64 {
83 fn check(self) -> Result<Self, Error> {
84 if self == bindings::PROS_ERR_F_ {
85 Err(from_errno())
86 } else {
87 Ok(self)
88 }
89 }
90}
91
92impl<T> SentinelError for *mut T {
93 fn check(self) -> Result<Self, Error> {
94 if self.is_null() {
95 Err(from_errno())
96 } else {
97 Ok(self)
98 }
99 }
100}
101
102extern "C" {
104 fn __errno() -> *mut i32;
105}
106
107#[inline]
109pub fn get_errno() -> libc::c_int {
110 unsafe { *__errno() }
111}
112
113#[inline]
116pub fn from_errno() -> Error {
117 Error::System(get_errno())
118}