use std::fmt;
#[derive(Debug)]
pub enum ColorError {
InvalidColor(InvalidColorError),
Conversion(ColorConversionError)
}
#[derive(Debug)]
pub struct InvalidColorError {
pub color_value: String,
pub message: String
}
#[derive(Debug)]
pub struct ColorConversionError {
pub from_format: String,
pub to_format: String,
pub value: String,
pub message: String
}
impl InvalidColorError {
pub fn new(color_value: impl ToString, message: impl ToString) -> Self {
Self {
color_value: color_value.to_string(),
message: message.to_string()
}
}
}
impl ColorConversionError {
pub fn new(from_format: impl ToString, to_format: impl ToString, value: impl ToString) -> Self {
let from: String = from_format.to_string();
let to: String = to_format.to_string();
let val: String = value.to_string();
Self {
message: format!("Failed to convert {} from {} to {}", val, from, to),
from_format: from,
to_format: to,
value: val
}
}
}
impl fmt::Display for ColorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ColorError::InvalidColor(e) => write!(f, "{}", e.message),
ColorError::Conversion(e) => write!(f, "{}", e.message)
}
}
}
impl std::error::Error for ColorError {}
impl From<InvalidColorError> for ColorError {
fn from(e: InvalidColorError) -> Self {
ColorError::InvalidColor(e)
}
}
impl From<ColorConversionError> for ColorError {
fn from(e: ColorConversionError) -> Self {
ColorError::Conversion(e)
}
}