From 9b496b92522ffcc4a8d69a6c0f6146ba06fa9b77 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Mon, 19 May 2025 11:15:13 +0200 Subject: [PATCH] added weired MizeErrorWhichImplementsError type --- src/core/error.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/core/error.rs b/src/core/error.rs index eb6ed4b..adeb949 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -1,3 +1,5 @@ +use core::result::Result; +use core::result::Result::Ok; use std::collections::HashMap; use std::fmt::Debug; use std::fmt::Display; @@ -19,6 +21,7 @@ pub type MizeResult = Result; pub trait MizeResultTrait { fn critical(self) -> T; + fn as_std(self) -> Result; } pub trait IntoMizeResult { @@ -160,8 +163,10 @@ impl MizeError { self } + } + /* pub trait MizeResultExtension { fn extra_msg(self, msg: E) -> Result @@ -231,6 +236,14 @@ impl MizeResultTrait for MizeResult { }, } } + fn as_std(self) -> Result { + match self { + Ok(val) => Ok(val), + Err(err) => { + Err(MizeErrorWhichImplementsError { inner: err }) + }, + } + } } impl IntoMizeResult for Result where E: std::fmt::Display { @@ -276,5 +289,34 @@ fn get_error_by_code(code: u32, caller_location: &std::panic::Location) -> MizeE } } +#[derive(Debug, Clone)] +pub struct MizeErrorWhichImplementsError { + inner: MizeError, +} + +impl Display for MizeErrorWhichImplementsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "MizeError envountered!"); + + if let Some(ref location) = self.inner.code_location { + write!(f, "[ {} ] {}", "LOCATION".yellow(), location); + }; + + write!(f, "[ {} ] {}", "BACKTRACE".yellow(), self.inner.backtrace); + + for msg in &self.inner.messages { + write!(f,"[ {} ] {}", "MSG".yellow(), msg); + } + Ok(()) + } +} + +impl From for MizeErrorWhichImplementsError { + fn from(value: MizeError) -> Self { + MizeErrorWhichImplementsError { inner: value } + } +} + +impl std::error::Error for MizeErrorWhichImplementsError {}