use std::{error::Error, fmt};
impl Error for ComponentFormatError {}
impl Error for ComponentError {}
#[derive(Debug)]
pub struct ComponentFormatError {
name: String,
message: String,
}
impl ComponentFormatError {
pub fn new(name: &str, msg: &str) -> Self {
Self {
name: name.to_string(),
message: msg.to_string(),
}
}
pub(crate) fn format(&self) -> String {
format!(
"Failed to display '{}' on terminal, {}.\n",
self.name, self.message
)
}
}
impl fmt::Display for ComponentFormatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.format())
}
}
#[derive(Debug)]
pub enum ComponentError {
ComponentFormatErrors(Vec<ComponentFormatError>),
}
impl fmt::Display for ComponentError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ComponentError::ComponentFormatErrors(errs) => {
let mut result = String::new();
for e in errs {
result += &e.format();
}
result += "/n";
write!(f, "{}", result)
}
}
}
}