use std::{ffi::NulError, fmt, os::raw::c_int};
pub type PiperResult<T> = Result<T, PiperError>;
#[derive(Debug)]
pub enum PiperError {
InternalNullByte(NulError),
NullPointer,
GenericError,
UnknownError(c_int),
Io(std::io::Error),
}
impl fmt::Display for PiperError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PiperError::InternalNullByte(e) => {
write!(f, "got a null byte in a string passed to piper: {}", e)
}
PiperError::NullPointer => f.write_str("piper returned a null pointer"),
PiperError::GenericError => f.write_str("piper returned an error"),
PiperError::UnknownError(e) => {
write!(f, "piper returned an unknown error code: {}", e)
}
PiperError::Io(e) => write!(f, "IO error: {}", e),
}
}
}
impl std::error::Error for PiperError {}
impl From<NulError> for PiperError {
fn from(err: NulError) -> PiperError {
PiperError::InternalNullByte(err)
}
}
impl From<std::io::Error> for PiperError {
fn from(err: std::io::Error) -> PiperError {
PiperError::Io(err)
}
}