sesh_shared/
error.rs

1#![allow(clippy::not_unsafe_ptr_arg_deref)]
2
3pub trait CResult<T, E>: Sized {
4    fn to_result(self) -> Result<T, E>;
5}
6
7impl CResult<libc::c_int, anyhow::Error> for libc::c_int {
8    fn to_result(self) -> Result<libc::c_int, anyhow::Error> {
9        match self {
10            -1 => Err(anyhow::anyhow!("C Error")),
11            res => Ok(res),
12        }
13    }
14}
15
16impl CResult<libc::passwd, anyhow::Error> for *mut libc::passwd {
17    fn to_result(self) -> Result<libc::passwd, anyhow::Error> {
18        if self.is_null() {
19            Err(anyhow::anyhow!("Could not get passwd entry"))
20        } else {
21            Ok(unsafe { *self })
22        }
23    }
24}