From 8c66b98bca6ed0a2990903fe8e0ea72def5c7be8 Mon Sep 17 00:00:00 2001
From: HampusM <hampus@hampusmat.com>
Date: Sun, 21 Aug 2022 14:19:07 +0200
Subject: refactor!: change errors to be more sane

BREAKING CHANGE: Major improvements have been made to error types and the error_stack crate is no longer used
---
 src/libs/intertrait/cast/arc.rs   | 15 ++++++---------
 src/libs/intertrait/cast/box.rs   | 15 ++++++---------
 src/libs/intertrait/cast/error.rs | 21 +++++++--------------
 src/libs/intertrait/cast/rc.rs    | 15 ++++++---------
 4 files changed, 25 insertions(+), 41 deletions(-)

(limited to 'src/libs/intertrait/cast')

diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs
index f8e0f11..65ae1ef 100644
--- a/src/libs/intertrait/cast/arc.rs
+++ b/src/libs/intertrait/cast/arc.rs
@@ -12,8 +12,6 @@
 use std::any::type_name;
 use std::sync::Arc;
 
-use error_stack::report;
-
 use crate::libs::intertrait::cast::error::CastError;
 use crate::libs::intertrait::{caster, CastFromSync};
 
@@ -22,7 +20,7 @@ pub trait CastArc
     /// Casts an `Arc` for this trait into that for type `OtherTrait`.
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Arc<Self>,
-    ) -> error_stack::Result<Arc<OtherTrait>, CastError>;
+    ) -> Result<Arc<OtherTrait>, CastError>;
 }
 
 /// A blanket implementation of `CastArc` for traits extending `CastFrom`, `Sync`, and `Send`.
@@ -30,15 +28,14 @@ impl<CastFromSelf: ?Sized + CastFromSync> CastArc for CastFromSelf
 {
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Arc<Self>,
-    ) -> error_stack::Result<Arc<OtherTrait>, CastError>
+    ) -> Result<Arc<OtherTrait>, CastError>
     {
         match caster::<OtherTrait>((*self).type_id()) {
             Some(caster) => Ok((caster.cast_arc)(self.arc_any())),
-            None => Err(report!(CastError).attach_printable(format!(
-                "From {} to {}",
-                type_name::<CastFromSelf>(),
-                type_name::<OtherTrait>()
-            ))),
+            None => Err(CastError::CastFailed {
+                from: type_name::<CastFromSelf>(),
+                to: type_name::<OtherTrait>(),
+            }),
         }
     }
 }
diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs
index 11f631a..31f06db 100644
--- a/src/libs/intertrait/cast/box.rs
+++ b/src/libs/intertrait/cast/box.rs
@@ -12,8 +12,6 @@
 
 use std::any::type_name;
 
-use error_stack::report;
-
 use crate::libs::intertrait::cast::error::CastError;
 use crate::libs::intertrait::{caster, CastFrom};
 
@@ -22,7 +20,7 @@ pub trait CastBox
     /// Casts a box to this trait into that of type `OtherTrait`.
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Box<Self>,
-    ) -> error_stack::Result<Box<OtherTrait>, CastError>;
+    ) -> Result<Box<OtherTrait>, CastError>;
 }
 
 /// A blanket implementation of `CastBox` for traits extending `CastFrom`.
@@ -30,15 +28,14 @@ impl<CastFromSelf: ?Sized + CastFrom> CastBox for CastFromSelf
 {
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Box<Self>,
-    ) -> error_stack::Result<Box<OtherTrait>, CastError>
+    ) -> Result<Box<OtherTrait>, CastError>
     {
         match caster::<OtherTrait>((*self).type_id()) {
             Some(caster) => Ok((caster.cast_box)(self.box_any())),
-            None => Err(report!(CastError).attach_printable(format!(
-                "From {} to {}",
-                type_name::<CastFromSelf>(),
-                type_name::<OtherTrait>()
-            ))),
+            None => Err(CastError::CastFailed {
+                from: type_name::<CastFromSelf>(),
+                to: type_name::<OtherTrait>(),
+            }),
         }
     }
 }
diff --git a/src/libs/intertrait/cast/error.rs b/src/libs/intertrait/cast/error.rs
index e4211b1..74eb3ca 100644
--- a/src/libs/intertrait/cast/error.rs
+++ b/src/libs/intertrait/cast/error.rs
@@ -1,17 +1,10 @@
-use std::fmt;
-use std::fmt::{Display, Formatter};
-
-use error_stack::Context;
-
-#[derive(Debug)]
-pub struct CastError;
-
-impl Display for CastError
+#[derive(thiserror::Error, Debug)]
+pub enum CastError
 {
-    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result
+    #[error("Failed to cast from trait {from} to trait {to}")]
+    CastFailed
     {
-        fmt.write_str("Failed to cast between traits")
-    }
+        from: &'static str,
+        to: &'static str,
+    },
 }
-
-impl Context for CastError {}
diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs
index 6cd377e..dfb71c2 100644
--- a/src/libs/intertrait/cast/rc.rs
+++ b/src/libs/intertrait/cast/rc.rs
@@ -12,8 +12,6 @@
 use std::any::type_name;
 use std::rc::Rc;
 
-use error_stack::report;
-
 use crate::libs::intertrait::cast::error::CastError;
 use crate::libs::intertrait::{caster, CastFrom};
 
@@ -22,7 +20,7 @@ pub trait CastRc
     /// Casts an `Rc` for this trait into that for type `OtherTrait`.
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Rc<Self>,
-    ) -> error_stack::Result<Rc<OtherTrait>, CastError>;
+    ) -> Result<Rc<OtherTrait>, CastError>;
 }
 
 /// A blanket implementation of `CastRc` for traits extending `CastFrom`.
@@ -30,15 +28,14 @@ impl<CastFromSelf: ?Sized + CastFrom> CastRc for CastFromSelf
 {
     fn cast<OtherTrait: ?Sized + 'static>(
         self: Rc<Self>,
-    ) -> error_stack::Result<Rc<OtherTrait>, CastError>
+    ) -> Result<Rc<OtherTrait>, CastError>
     {
         match caster::<OtherTrait>((*self).type_id()) {
             Some(caster) => Ok((caster.cast_rc)(self.rc_any())),
-            None => Err(report!(CastError).attach_printable(format!(
-                "From {} to {}",
-                type_name::<CastFromSelf>(),
-                type_name::<OtherTrait>()
-            ))),
+            None => Err(CastError::CastFailed {
+                from: type_name::<CastFromSelf>(),
+                to: type_name::<OtherTrait>(),
+            }),
         }
     }
 }
-- 
cgit v1.2.3-18-g5258