use {crate::fh::ComponentInformation, std::rc::Rc};
#[derive(Debug)]
pub enum LewpErrorKind {
LoopDetection,
ModuleNotFound,
Css,
FileHierarchy,
FileHierarchyComponent,
Runtime,
Render,
}
#[derive(Debug)]
pub struct LewpError {
pub kind: LewpErrorKind,
pub message: String,
pub source_component: Rc<ComponentInformation>,
}
impl From<Rc<ComponentInformation>> for LewpError {
fn from(meta: Rc<ComponentInformation>) -> Self {
Self {
kind: LewpErrorKind::FileHierarchyComponent,
message: String::from("Unspecified error occured!"),
source_component: meta,
}
}
}
impl LewpError {
pub fn new(
kind: LewpErrorKind,
message: &str,
meta: Rc<ComponentInformation>,
) -> Self {
Self {
kind,
message: message.to_string(),
source_component: meta,
}
}
pub fn from_with_message(
meta: Rc<ComponentInformation>,
message: &str,
) -> Self {
Self {
kind: LewpErrorKind::FileHierarchyComponent,
message: message.to_string(),
source_component: meta,
}
}
}
impl std::fmt::Display for LewpError {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
use LewpErrorKind::*;
let kind = match self.kind {
LoopDetection => "LOOP DETECTION",
ModuleNotFound => "MODULE NOT FOUND",
Css => "CSS PROCESSING",
FileHierarchy => "FILE HIERARCHY",
FileHierarchyComponent => "FILE HIERARCHY COMPONENT",
Runtime => "Runtime",
Render => "RENDER",
};
write!(
f,
"Component with id '{}', on level '{}' of type '{}' returned [{}]: {}",
&self.source_component.id,
&self.source_component.level,
&self.source_component.kind,
kind,
self.message
)
}
}