use std::{fmt, io};
#[derive(Debug)]
pub enum Error<E> {
App(E),
BadArg(String),
IO(std::io::Error),
Internal(String),
NetIface(String)
}
impl<E> Error<E> {
pub fn bad_arg<S: ToString>(s: S) -> Self {
Error::BadArg(s.to_string())
}
pub fn internal<S: ToString>(s: S) -> Self {
Error::Internal(s.to_string())
}
}
impl<E> std::error::Error for Error<E> where E: std::fmt::Debug {}
impl<E> From<io::Error> for Error<E> {
fn from(err: io::Error) -> Self {
Error::IO(err)
}
}
impl<E> fmt::Display for Error<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::App(_) => {
write!(f, "Application-specific error")
}
Error::BadArg(s) => {
write!(f, "Bad argument; {}", s)
}
Error::Internal(e) => {
write!(f, "Internal error; {}", e)
}
Error::IO(e) => {
write!(f, "I/O error; {}", e)
}
Error::NetIface(s) => {
write!(f, "Bad format error; {}", s)
}
}
}
}