{-# OPTIONS_GHC -Wno-redundant-constraints #-}

-- | Provides a static effect for optparse-applicative.
--
-- @since 0.1
module Effectful.Optparse.Static
  ( -- * Effect
    Optparse,
    execParser,
    customExecParser,
    handleParseResult,

    -- ** Handler
    runOptparse,

    -- * Misc
    Utils.OsPath,
    Utils.osPath,
    Utils.validOsPath,
  )
where

import Effectful
  ( Dispatch (Static),
    DispatchOf,
    Eff,
    Effect,
    IOE,
    type (:>),
  )
import Effectful.Dispatch.Static
  ( HasCallStack,
    SideEffects (WithSideEffects),
    StaticRep,
    evalStaticRep,
    unsafeEff_,
  )
import Effectful.Optparse.Utils qualified as Utils
import Options.Applicative (ParserInfo, ParserPrefs, ParserResult)
import Options.Applicative qualified as OA

-- | Static effect for optparse-applicative.
--
-- @since 0.1
data Optparse :: Effect

type instance DispatchOf Optparse = Static WithSideEffects

data instance StaticRep Optparse = MkOptparse

-- | Runs an Optparse effect.
--
-- @since 0.1
runOptparse :: (HasCallStack, IOE :> es) => Eff (Optparse : es) a -> Eff es a
runOptparse :: forall (es :: [Effect]) a.
(HasCallStack, IOE :> es) =>
Eff (Optparse : es) a -> Eff es a
runOptparse = StaticRep Optparse -> Eff (Optparse : es) a -> Eff es a
forall (e :: Effect) (sideEffects :: SideEffects) (es :: [Effect])
       a.
(HasCallStack, DispatchOf e ~ 'Static sideEffects,
 MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep StaticRep Optparse
MkOptparse

-- | Lifted 'OA.execParser'.
--
-- @since 0.1
execParser :: (HasCallStack, Optparse :> es) => ParserInfo a -> Eff es a
execParser :: forall (es :: [Effect]) a.
(HasCallStack, Optparse :> es) =>
ParserInfo a -> Eff es a
execParser = IO a -> Eff es a
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO a -> Eff es a)
-> (ParserInfo a -> IO a) -> ParserInfo a -> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserInfo a -> IO a
forall a. ParserInfo a -> IO a
OA.execParser

-- | Lifted 'OA.execParser'.
--
-- @since 0.1
customExecParser ::
  (HasCallStack, Optparse :> es) =>
  ParserPrefs ->
  ParserInfo a ->
  Eff es a
customExecParser :: forall (es :: [Effect]) a.
(HasCallStack, Optparse :> es) =>
ParserPrefs -> ParserInfo a -> Eff es a
customExecParser ParserPrefs
prefs = IO a -> Eff es a
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO a -> Eff es a)
-> (ParserInfo a -> IO a) -> ParserInfo a -> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserPrefs -> ParserInfo a -> IO a
forall a. ParserPrefs -> ParserInfo a -> IO a
OA.customExecParser ParserPrefs
prefs

-- | Lifted 'OA.execParser'.
--
-- @since 0.1
handleParseResult :: (HasCallStack, Optparse :> es) => ParserResult a -> Eff es a
handleParseResult :: forall (es :: [Effect]) a.
(HasCallStack, Optparse :> es) =>
ParserResult a -> Eff es a
handleParseResult = IO a -> Eff es a
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO a -> Eff es a)
-> (ParserResult a -> IO a) -> ParserResult a -> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserResult a -> IO a
forall a. ParserResult a -> IO a
OA.handleParseResult