use std::ffi::NulError;
use std::fmt;
use std::io;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Nul(NulError),
Null,
Operation {
op: i32,
code: i32,
},
NotFound,
Interrupted,
}
impl Error {
pub(crate) fn operation(op: i32, code: i32) -> Self {
Error::Operation { op, code }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {e}"),
Error::Nul(e) => write!(f, "interior NUL byte in string: {e}"),
Error::Null => write!(f, "libedit returned an unexpected null pointer"),
Error::Operation { op, code } => {
write!(f, "libedit operation {op} failed with code {code}")
}
Error::NotFound => write!(f, "history entry not found"),
Error::Interrupted => write!(f, "read interrupted by signal"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Nul(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
impl From<NulError> for Error {
fn from(e: NulError) -> Self {
Error::Nul(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;