{-# LANGUAGE CPP                 #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE TypeOperators       #-}

{-|
Module      : GHCup.Prelude
Description : MegaParsec utilities
Copyright   : (c) Julian Ospald, 2020
License     : LGPL-3.0
Maintainer  : hasufell@hasufell.de
Stability   : experimental
Portability : portable

GHCup specific prelude. Lots of Excepts functionality.
-}
module GHCup.Prelude
  (module GHCup.Prelude,
   module GHCup.Prelude.Internal,
#if defined(IS_WINDOWS)
   module GHCup.Prelude.Windows
#else
   module GHCup.Prelude.Posix
#endif
  )
where

import           GHCup.Errors
import           GHCup.Prelude.Internal
import           GHCup.Types.Optics   (HasLog)
import           GHCup.Prelude.Logger (logWarn)
#if defined(IS_WINDOWS)
import GHCup.Prelude.Windows
#else
import GHCup.Prelude.Posix
#endif

import           Control.Monad.IO.Class
import           Control.Monad.Reader
import           Haskus.Utils.Variant.Excepts
import           Text.PrettyPrint.HughesPJClass ( Pretty )
import qualified Data.Text                     as T



-- for some obscure reason... this won't type-check if we move it to a different module
catchWarn :: forall es m env . ( Pretty (V es)
                             , HFErrorProject (V es)
                             , MonadReader env m
                             , HasLog env
                             , MonadIO m
                             , Monad m) => Excepts es m () -> Excepts '[] m ()
catchWarn :: forall (es :: [*]) (m :: * -> *) env.
(Pretty (V es), HFErrorProject (V es), MonadReader env m,
 HasLog env, MonadIO m, Monad m) =>
Excepts es m () -> Excepts '[] m ()
catchWarn = forall (m :: * -> *) (es :: [*]) (es' :: [*]) a.
Monad m =>
(V es -> Excepts es' m a) -> Excepts es m a -> Excepts es' m a
catchAllE @_ @es (\V es
v -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall env (m :: * -> *).
(MonadReader env m,
 LabelOptic' "loggerConfig" A_Lens env LoggerConfig, MonadIO m) =>
Text -> m ()
logWarn (String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. (Pretty e, HFErrorProject e) => e -> String
prettyHFError forall a b. (a -> b) -> a -> b
$ V es
v))


runBothE' :: forall e m a b .
             ( Monad m
             , Show (V e)
             , Pretty (V e)
             , HFErrorProject (V e)
             , PopVariant InstallSetError e
             , LiftVariant' e (InstallSetError ': e)
             , e :<< (InstallSetError ': e)
             )
          => Excepts e m a
          -> Excepts e m b
          -> Excepts (InstallSetError ': e) m ()
runBothE' :: forall (e :: [*]) (m :: * -> *) a b.
(Monad m, Show (V e), Pretty (V e), HFErrorProject (V e),
 PopVariant InstallSetError e, LiftVariant' e (InstallSetError : e),
 e :<< (InstallSetError : e)) =>
Excepts e m a
-> Excepts e m b -> Excepts (InstallSetError : e) m ()
runBothE' Excepts e m a
a1 Excepts e m b
a2 = do
   VEither e a
r1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (es :: [*]) a (m :: * -> *).
Excepts es m a -> m (VEither es a)
runE @e Excepts e m a
a1
   VEither e b
r2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (es :: [*]) a (m :: * -> *).
Excepts es m a -> m (VEither es a)
runE @e Excepts e m b
a2
   case (VEither e a
r1, VEither e b
r2) of
      (VLeft V e
e1, VLeft V e
e2) -> forall e (es :: [*]) a (m :: * -> *).
(Monad m, e :< es) =>
e -> Excepts es m a
throwE (forall (xs1 :: [*]) (xs2 :: [*]).
(Show (V xs1), Pretty (V xs1), HFErrorProject (V xs1),
 Show (V xs2), Pretty (V xs2), HFErrorProject (V xs2)) =>
V xs1 -> V xs2 -> InstallSetError
InstallSetError V e
e1 V e
e2)
      (VLeft V e
e , VEither e b
_       ) -> forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, LiftVariant es' es) =>
V es' -> Excepts es m a
throwSomeE V e
e
      (VEither e a
_       , VLeft V e
e ) -> forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, LiftVariant es' es) =>
V es' -> Excepts es m a
throwSomeE V e
e
      (VRight a
_, VRight b
_) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- "throwSomeE" function has been upstreamed in haskus-utils-variant-3.3
-- So, only conditionally include this shim if
-- haskus-utils-variant version is < 3.3

#if MIN_VERSION_haskus_utils_variant(3,3,0)
#else
-- | Throw some exception
throwSomeE :: forall es' es a m. (Monad m, LiftVariant es' es) => V es' -> Excepts es m a
{-# INLINABLE throwSomeE #-}
throwSomeE :: forall (es' :: [*]) (es :: [*]) a (m :: * -> *).
(Monad m, LiftVariant es' es) =>
V es' -> Excepts es m a
throwSomeE = forall (es :: [*]) (m :: * -> *) a.
m (VEither es a) -> Excepts es m a
Excepts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x (xs :: [*]). V xs -> VEither xs x
VLeft forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (ys :: [*]) (xs :: [*]). LiftVariant xs ys => V xs -> V ys
liftVariant
#endif