use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::result::Result as StdResult;
#[derive(Debug)]
#[non_exhaustive]
pub enum DraconError {
Io(io::Error),
Parse(String),
Widget(String),
Theme(String),
Config(String),
Clipboard(String),
Serialize(String),
User(String),
}
impl Clone for DraconError {
fn clone(&self) -> Self {
match self {
DraconError::Io(e) => DraconError::Io(io::Error::new(e.kind(), e.to_string())),
DraconError::Parse(s) => DraconError::Parse(s.clone()),
DraconError::Widget(s) => DraconError::Widget(s.clone()),
DraconError::Theme(s) => DraconError::Theme(s.clone()),
DraconError::Config(s) => DraconError::Config(s.clone()),
DraconError::Clipboard(s) => DraconError::Clipboard(s.clone()),
DraconError::Serialize(s) => DraconError::Serialize(s.clone()),
DraconError::User(s) => DraconError::User(s.clone()),
}
}
}
impl PartialEq for DraconError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(DraconError::Io(a), DraconError::Io(b)) => {
a.kind() == b.kind() && a.to_string() == b.to_string()
}
(DraconError::Parse(a), DraconError::Parse(b)) => a == b,
(DraconError::Widget(a), DraconError::Widget(b)) => a == b,
(DraconError::Theme(a), DraconError::Theme(b)) => a == b,
(DraconError::Config(a), DraconError::Config(b)) => a == b,
(DraconError::Clipboard(a), DraconError::Clipboard(b)) => a == b,
(DraconError::Serialize(a), DraconError::Serialize(b)) => a == b,
(DraconError::User(a), DraconError::User(b)) => a == b,
_ => false,
}
}
}
impl Eq for DraconError {}
impl fmt::Display for DraconError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DraconError::Io(e) => write!(f, "I/O error: {}", e),
DraconError::Parse(msg) => write!(f, "Parse error: {}", msg),
DraconError::Widget(msg) => write!(f, "Widget error: {}", msg),
DraconError::Theme(msg) => write!(f, "Theme error: {}", msg),
DraconError::Config(msg) => write!(f, "Config error: {}", msg),
DraconError::Clipboard(msg) => write!(f, "Clipboard error: {}", msg),
DraconError::Serialize(msg) => write!(f, "Serialization error: {}", msg),
DraconError::User(msg) => write!(f, "{}", msg),
}
}
}
impl StdError for DraconError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
DraconError::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for DraconError {
fn from(err: io::Error) -> Self {
DraconError::Io(err)
}
}
impl From<serde_json::Error> for DraconError {
fn from(err: serde_json::Error) -> Self {
DraconError::Serialize(err.to_string())
}
}
impl DraconError {
pub fn io_msg(msg: impl Into<String>) -> Self {
let msg = msg.into();
DraconError::Io(io::Error::other(msg))
}
pub fn parse(msg: impl Into<String>) -> Self {
DraconError::Parse(msg.into())
}
pub fn widget(msg: impl Into<String>) -> Self {
DraconError::Widget(msg.into())
}
pub fn theme(msg: impl Into<String>) -> Self {
DraconError::Theme(msg.into())
}
pub fn config(msg: impl Into<String>) -> Self {
DraconError::Config(msg.into())
}
pub fn clipboard(msg: impl Into<String>) -> Self {
DraconError::Clipboard(msg.into())
}
pub fn serialize(msg: impl Into<String>) -> Self {
DraconError::Serialize(msg.into())
}
pub fn user(msg: impl Into<String>) -> Self {
DraconError::User(msg.into())
}
}
impl From<DraconError> for io::Error {
fn from(err: DraconError) -> Self {
match err {
DraconError::Io(e) => e,
other => io::Error::other(other.to_string()),
}
}
}
impl From<DraconError> for String {
fn from(err: DraconError) -> Self {
err.to_string()
}
}
pub type Result<T> = StdResult<T, DraconError>;