{-# LANGUAGE OverloadedStrings #-}
module SDJWT.Internal.Monad
( SDJWTIO
, runSDJWTIO
, eitherToExceptT
, partitionAndHandle
) where
import SDJWT.Internal.Types (SDJWTError)
import Control.Monad.Except (ExceptT, runExceptT, throwError)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Either (partitionEithers)
type SDJWTIO = ExceptT SDJWTError IO
runSDJWTIO :: SDJWTIO a -> IO (Either SDJWTError a)
runSDJWTIO :: forall a. SDJWTIO a -> IO (Either SDJWTError a)
runSDJWTIO = ExceptT SDJWTError IO a -> IO (Either SDJWTError a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
eitherToExceptT :: Monad m => Either SDJWTError a -> ExceptT SDJWTError m a
eitherToExceptT :: forall (m :: * -> *) a.
Monad m =>
Either SDJWTError a -> ExceptT SDJWTError m a
eitherToExceptT = (SDJWTError -> ExceptT SDJWTError m a)
-> (a -> ExceptT SDJWTError m a)
-> Either SDJWTError a
-> ExceptT SDJWTError m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SDJWTError -> ExceptT SDJWTError m a
forall a. SDJWTError -> ExceptT SDJWTError m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError a -> ExceptT SDJWTError m a
forall a. a -> ExceptT SDJWTError m a
forall (m :: * -> *) a. Monad m => a -> m a
return
handlePartitionEithers
:: Monad m
=> [SDJWTError]
-> [a]
-> ([a] -> ExceptT SDJWTError m b)
-> ExceptT SDJWTError m b
handlePartitionEithers :: forall (m :: * -> *) a b.
Monad m =>
[SDJWTError]
-> [a] -> ([a] -> ExceptT SDJWTError m b) -> ExceptT SDJWTError m b
handlePartitionEithers [SDJWTError]
errors [a]
successes [a] -> ExceptT SDJWTError m b
handler =
case [SDJWTError]
errors of
(SDJWTError
err:[SDJWTError]
_) -> SDJWTError -> ExceptT SDJWTError m b
forall a. SDJWTError -> ExceptT SDJWTError m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError SDJWTError
err
[] -> [a] -> ExceptT SDJWTError m b
handler [a]
successes
partitionAndHandle
:: Monad m
=> [Either SDJWTError a]
-> ([a] -> ExceptT SDJWTError m b)
-> ExceptT SDJWTError m b
partitionAndHandle :: forall (m :: * -> *) a b.
Monad m =>
[Either SDJWTError a]
-> ([a] -> ExceptT SDJWTError m b) -> ExceptT SDJWTError m b
partitionAndHandle [Either SDJWTError a]
results [a] -> ExceptT SDJWTError m b
handler =
let ([SDJWTError]
errors, [a]
successes) = [Either SDJWTError a] -> ([SDJWTError], [a])
forall a b. [Either a b] -> ([a], [b])
partitionEithers [Either SDJWTError a]
results
in [SDJWTError]
-> [a] -> ([a] -> ExceptT SDJWTError m b) -> ExceptT SDJWTError m b
forall (m :: * -> *) a b.
Monad m =>
[SDJWTError]
-> [a] -> ([a] -> ExceptT SDJWTError m b) -> ExceptT SDJWTError m b
handlePartitionEithers [SDJWTError]
errors [a]
successes [a] -> ExceptT SDJWTError m b
handler