use core::fmt::Display; use core::num::ParseIntError; use std::error::Error; use std::ffi::OsString; use crate::{FD_NAMES_VAR, FD_NUMBER_VAR, PID_VAR}; #[derive(Debug)] pub enum ReceiveError { NoListenPID, NotUnicodeListenPID(OsString), ListenPIDParse(ParseIntError), NoListenFD, NotUnicodeListenFD(OsString), ListenFDParse(ParseIntError), PidMismatch { expected: u32, found: u32 }, GetFds(GetFdsError), } impl Display for ReceiveError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Couldn't receive file descriptors :")?; match self { ReceiveError::NoListenPID => write!(f, "The variable {PID_VAR} doesn't exists."), ReceiveError::NotUnicodeListenPID(var) => write!(f, "The variable {PID_VAR} isn't unicode (this should never happen): it is {var:?}"), ReceiveError::ListenPIDParse(error) => write!(f, "Couldn't parse {PID_VAR} as a `u32`: {error}"), ReceiveError::NoListenFD => write!(f, "The variable {FD_NUMBER_VAR} doesn't exists."), ReceiveError::NotUnicodeListenFD(var) => write!(f, "The variable {FD_NUMBER_VAR} isn't unicode (this should never happen): it is {var:?}"), ReceiveError::ListenFDParse(error) => write!(f, "Couldn't parse {FD_NUMBER_VAR} as a `u32`: {error}"), ReceiveError::PidMismatch{expected, found} => write!(f, "PID mismatch! Was {found} but should have been {expected}."), ReceiveError::GetFds(error) => Display::fmt(error, f), } } } impl Error for ReceiveError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { ReceiveError::NoListenPID => None, ReceiveError::NotUnicodeListenPID(_) => None, ReceiveError::ListenPIDParse(error) => Some(error), ReceiveError::NoListenFD => None, ReceiveError::NotUnicodeListenFD(_) => None, ReceiveError::ListenFDParse(error) => Some(error), ReceiveError::PidMismatch { expected: _, found: _, } => None, ReceiveError::GetFds(error) => Some(error), } } } #[derive(Debug)] pub enum ReceiveNameError { NoListenFDName, Receive(ReceiveError), } impl From for ReceiveNameError { fn from(value: ReceiveError) -> Self { Self::Receive(value) } } impl Display for ReceiveNameError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ReceiveNameError::NoListenFDName => write!( f, "Couldn't find FDs name : the variable {FD_NAMES_VAR} doesn't exists." ), ReceiveNameError::Receive(error) => Display::fmt(error, f), } } } impl Error for ReceiveNameError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { ReceiveNameError::NoListenFDName => None, ReceiveNameError::Receive(error) => Some(error), } } } #[derive(Debug)] pub enum GetFdsError { TooManyFDs(usize), } impl Display for GetFdsError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { GetFdsError::TooManyFDs(size) => write!(f, "Too many file descriptors ({size})"), } } } impl Error for GetFdsError {}