//! Error //! //! This module is used to create and log Rorolala standard errors use std::{ fmt::{Display, Formatter}, io::Error as IoError, }; mod io; /// Rorolala standard error #[derive(Default)] pub struct RolaError { pub module: RolaModule, pub data: RolaErrorData, pub message: String, } /// Rorolala module, used to locate the source of an error #[derive(Debug, Eq, Default)] #[repr(u8)] pub enum RolaModule { #[default] Empty, Bucket, Workdraft, } /// Error data #[derive(Debug, Default)] pub enum RolaErrorData { #[default] Empty, /// IO error IO(IoError), } impl RolaError { /// Create a new RolaError pub fn new(module: RolaModule, data: RolaErrorData, message: String) -> Self { Self { module, data, message, } } /// Create an empty RolaError pub fn empty() -> Self { Self { module: RolaModule::Empty, data: RolaErrorData::Empty, message: String::new(), } } /// Set the module pub fn with_module(mut self, module: RolaModule) -> Self { self.module = module; self } /// Set the message pub fn with_message(mut self, message: String) -> Self { self.message = message; self } /// Set the error data pub fn with_data(mut self, data: RolaErrorData) -> Self { self.data = data; self } } impl Display for RolaError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.message) } } impl PartialEq for RolaError { fn eq(&self, other: &Self) -> bool { self.message == other.message && self.module == other.module } } impl Display for RolaModule { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } } impl PartialEq for RolaModule { fn eq(&self, other: &Self) -> bool { matches!( (self, other), (RolaModule::Bucket, RolaModule::Bucket) | (RolaModule::Workdraft, RolaModule::Workdraft) ) } }