-- |Combinators that catch exceptions in 'IO' and embed them into 'Sem'.
module Incipit.Exception where

import qualified Control.Exception as Base
import Control.Exception (Exception, SomeException)
import Data.Bifunctor (first)
import Data.Either (Either)
import Data.Function ((.))
import Data.Functor (fmap, void)
import Data.Maybe (Maybe)
import Polysemy (Embed, Member, Sem, embed)
import System.IO (IO)
import System.IO.Error (IOError)

import Incipit.Either (rightToMaybe)
import Incipit.String.Conversion (show)
import Incipit.String.Reexport (Text)

-- |Run an 'IO' via 'Embed' and catch exceptions of type @e@, returning 'Either'.
-- Unlike all other combinators, this doesn't convert the exception to 'Text'.
tryIOE ::
   e r a .
  Exception e =>
  Member (Embed IO) r =>
  IO a ->
  Sem r (Either e a)
tryIOE :: forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either e a)
tryIOE =
  forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed @IO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => IO a -> IO (Either e a)
Base.try @e
{-# inline tryIOE #-}

-- |Run an 'IO' via 'Embed' and catch exceptions of type @e@, returning 'Either'.
tryIO ::
   e r a .
  Exception e =>
  Member (Embed IO) r =>
  IO a ->
  Sem r (Either Text a)
tryIO :: forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO =
  forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed @IO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first forall b a. (Show a, IsString b) => a -> b
show) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => IO a -> IO (Either e a)
Base.try @e
{-# inline tryIO #-}

-- |Run an 'IO' via 'Embed' and catch IOError, returning 'Either'.
tryIOError ::
   r a .
  Member (Embed IO) r =>
  IO a ->
  Sem r (Either Text a)
tryIOError :: forall (r :: EffectRow) a.
Member (Embed IO) r =>
IO a -> Sem r (Either Text a)
tryIOError =
  forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @IOError
{-# inline tryIOError #-}

-- |Run an 'IO' via 'Embed' and catch all exceptions, returning 'Either'.
tryAny ::
   r a .
  Member (Embed IO) r =>
  IO a ->
  Sem r (Either Text a)
tryAny :: forall (r :: EffectRow) a.
Member (Embed IO) r =>
IO a -> Sem r (Either Text a)
tryAny =
  forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @SomeException
{-# inline tryAny #-}

-- |Run an 'IO' via 'Embed' and catch exceptions of type @e@, returning 'Maybe'.
tryIOMaybe ::
   e r a .
  Exception e =>
  Member (Embed IO) r =>
  IO a ->
  Sem r (Maybe a)
tryIOMaybe :: forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Maybe a)
tryIOMaybe =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall l r. Either l r -> Maybe r
rightToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @e
{-# inline tryIOMaybe #-}

-- |Run an 'IO' via 'Embed' and catch 'IOError', returning 'Maybe'.
tryIOErrorMaybe ::
   r a .
  Member (Embed IO) r =>
  IO a ->
  Sem r (Maybe a)
tryIOErrorMaybe :: forall (r :: EffectRow) a.
Member (Embed IO) r =>
IO a -> Sem r (Maybe a)
tryIOErrorMaybe =
  forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Maybe a)
tryIOMaybe @IOError
{-# inline tryIOErrorMaybe #-}

-- |Run an 'IO' via 'Embed' and catch all exceptions, returning 'Maybe'.
tryMaybe ::
   r a .
  Member (Embed IO) r =>
  IO a ->
  Sem r (Maybe a)
tryMaybe :: forall (r :: EffectRow) a.
Member (Embed IO) r =>
IO a -> Sem r (Maybe a)
tryMaybe =
  forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Maybe a)
tryIOMaybe @SomeException
{-# inline tryMaybe #-}

-- |Run an 'IO' via 'Embed' and catch and ignore exceptions of type @e@.
tryIO_ ::
   e r .
  Exception e =>
  Member (Embed IO) r =>
  IO () ->
  Sem r ()
tryIO_ :: forall e (r :: EffectRow).
(Exception e, Member (Embed IO) r) =>
IO () -> Sem r ()
tryIO_ =
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @e
{-# inline tryIO_ #-}

-- |Run an 'IO' via 'Embed' and catch and ignore 'IOError'.
tryIOError_ ::
   r .
  Member (Embed IO) r =>
  IO () ->
  Sem r ()
tryIOError_ :: forall (r :: EffectRow). Member (Embed IO) r => IO () -> Sem r ()
tryIOError_ =
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @IOError
{-# inline tryIOError_ #-}

-- |Run an 'IO' via 'Embed' and catch and ignore all exceptions.
tryAny_ ::
   r .
  Member (Embed IO) r =>
  IO () ->
  Sem r ()
tryAny_ :: forall (r :: EffectRow). Member (Embed IO) r => IO () -> Sem r ()
tryAny_ =
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (r :: EffectRow) a.
(Exception e, Member (Embed IO) r) =>
IO a -> Sem r (Either Text a)
tryIO @SomeException
{-# inline tryAny_ #-}

-- |Run an 'IO' via 'Embed' and catch and ignore all exceptions.
ignoreException ::
  Member (Embed IO) r =>
  IO () ->
  Sem r ()
ignoreException :: forall (r :: EffectRow). Member (Embed IO) r => IO () -> Sem r ()
ignoreException =
  forall (r :: EffectRow). Member (Embed IO) r => IO () -> Sem r ()
tryAny_
{-# inline ignoreException #-}
{-# deprecated ignoreException "renamed to 'tryAny_'" #-}