From 5c420970c162fdee432866a13861e3fa13176a90 Mon Sep 17 00:00:00 2001 From: Edsko de Vries Date: Tue, 21 Mar 2023 13:14:22 +0100 Subject: [PATCH] Introduce `throwFailure` --- src/Foreign/Rust/Failure.hs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Foreign/Rust/Failure.hs b/src/Foreign/Rust/Failure.hs index 3043442..1b81cf6 100644 --- a/src/Foreign/Rust/Failure.hs +++ b/src/Foreign/Rust/Failure.hs @@ -2,10 +2,13 @@ module Foreign.Rust.Failure ( Failure -- Opaque , failureMessage , mkFailure + , throwFailure + , throwFailureIO ) where -import GHC.Stack +import Control.Exception import Data.Text (Text) +import GHC.Stack -- | Failure reported by a Rust function -- @@ -14,7 +17,8 @@ data Failure = Failure { failureMessage :: Text , failureCallstackHaskell :: PrettyCallStack } - deriving (Show) + deriving stock (Show) + deriving anyclass (Exception) mkFailure :: HasCallStack => Text -> Failure mkFailure e = Failure e (PrettyCallStack callStack) @@ -24,4 +28,10 @@ newtype PrettyCallStack = PrettyCallStack CallStack instance Show PrettyCallStack where show (PrettyCallStack stack) = prettyCallStack stack +throwFailure :: Either Failure a -> a +throwFailure (Left err) = throw err +throwFailure (Right a) = a +throwFailureIO :: Either Failure a -> IO a +throwFailureIO (Left err) = throwIO err +throwFailureIO (Right a) = return a \ No newline at end of file