From ab55d21013a279568379b97821da98457a10754a Mon Sep 17 00:00:00 2001 From: Sampras Lopes Date: Mon, 8 May 2023 14:45:05 +0530 Subject: [PATCH] feat(errors): add reverse errorswitch trait for foreign errors (#909) --- crates/common_utils/src/errors.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/common_utils/src/errors.rs b/crates/common_utils/src/errors.rs index 9a1e681211..e3f00799e0 100644 --- a/crates/common_utils/src/errors.rs +++ b/crates/common_utils/src/errors.rs @@ -98,9 +98,26 @@ where } /// Allow [error_stack::Report] to convert between error types -/// This autoimplements [ReportSwitchExt] for the corresponding errors +/// This auto-implements [ReportSwitchExt] for the corresponding errors pub trait ErrorSwitch { /// Get the next error type that the source error can be escalated into /// This does not consume the source error since we need to keep it in context fn switch(&self) -> T; } + +/// Allow [error_stack::Report] to convert between error types +/// This serves as an alternative to [ErrorSwitch] +pub trait ErrorSwitchFrom { + /// Convert to an error type that the source can be escalated into + /// This does not consume the source error since we need to keep it in context + fn switch_from(error: &T) -> Self; +} + +impl ErrorSwitch for S +where + T: ErrorSwitchFrom, +{ + fn switch(&self) -> T { + T::switch_from(self) + } +}