use std::borrow::Cow;
use std::ffi::CStr;
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct SysError(i32);
impl std::fmt::Display for SysError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_str())
}
}
impl std::fmt::Debug for SysError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
r#"SysError {{ Code={}, Reason={:?} }}"#,
self.0,
self.to_str()
)
}
}
impl std::error::Error for SysError {}
impl From<i32> for SysError {
fn from(val: i32) -> Self {
Self { 0: val }
}
}
impl Into<i32> for SysError {
fn into(self) -> i32 {
self.0
}
}
impl SysError {
#[cfg(unix)]
pub fn last() -> Self {
unsafe {
Self {
0: *(libc::__errno_location()),
}
}
}
pub fn is_err(self) -> bool {
self.0 != 0
}
pub fn is_ok(self) -> bool {
!self.is_err()
}
pub fn map_or<T>(self, other: T) -> Result<T, SysError> {
if self.is_err() {
Err(self)
} else {
Ok(other)
}
}
#[cfg(unix)]
pub fn to_str(&self) -> Cow<str> {
unsafe { CStr::from_ptr(libc::strerror(self.0)).to_string_lossy() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sys_error() {
let err = SysError::last();
assert_eq!(err.is_err(), false);
}
}