use std::fmt;
use std::io;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Parse(String),
Format(String),
InvalidInput(String),
NotImplemented(String),
Other(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(err) => write!(f, "I/O error: {}", err),
Error::Parse(msg) => write!(f, "Parse error: {}", msg),
Error::Format(msg) => write!(f, "Format error: {}", msg),
Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
Error::NotImplemented(msg) => write!(f, "Not implemented: {}", msg),
Error::Other(msg) => write!(f, "Error: {}", msg),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
_ => None,
}
}
}
impl Error {
pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
Error::InvalidInput(msg.into())
}
pub fn parse<S: Into<String>>(msg: S) -> Self {
Error::Parse(msg.into())
}
pub fn format<S: Into<String>>(msg: S) -> Self {
Error::Format(msg.into())
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
impl From<String> for Error {
fn from(msg: String) -> Self {
Error::Other(msg)
}
}
impl From<&str> for Error {
fn from(msg: &str) -> Self {
Error::Other(msg.to_string())
}
}