Implement Error for ReadError

This commit is contained in:
NGnius (Graham) 2023-01-09 20:24:23 -05:00
parent 8387c8024e
commit 9755eb24b7

View file

@ -13,6 +13,7 @@ pub fn write_single<P: AsRef<Path>, D: Display>(path: P, display: D) -> Result<(
} }
/// read_single error /// read_single error
#[derive(Debug)]
pub enum ReadError<E> { pub enum ReadError<E> {
/// IO Error /// IO Error
Io(io::Error), Io(io::Error),
@ -20,6 +21,19 @@ pub enum ReadError<E> {
Parse(E), Parse(E),
} }
impl<E: std::error::Error> std::fmt::Display for ReadError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(io) => write!(f, "io: {}", io),
Self::Parse(e) => write!(f, "parse: {}", e),
}
}
}
impl<E: std::error::Error> std::error::Error for ReadError<E> {
}
/// Read something from a file. /// Read something from a file.
/// Useful for kernel configuration files. /// Useful for kernel configuration files.
pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, ReadError<E>> { pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, ReadError<E>> {