use std::fmt;
use std::io;
use crate::ordinance::Fault;
#[derive(Clone, Debug)]
pub enum Error {
Fault(Fault),
Message(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Fault(fault) => fmt::Display::fmt(fault, f),
Self::Message(message) => f.write_str(message),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Fault(fault) => Some(fault),
Self::Message(_) => None,
}
}
}
impl From<Fault> for Error {
fn from(fault: Fault) -> Self {
Self::Fault(fault)
}
}
impl From<String> for Error {
fn from(message: String) -> Self {
Self::Message(message)
}
}
impl From<&str> for Error {
fn from(message: &str) -> Self {
Self::Message(message.to_owned())
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self::Message(error.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Self::Message(error.to_string())
}
}