{-# LANGUAGE Safe #-}

{- |
Copyright:  (c) 2016 Stephen Diehl
            (c) 2016-2018 Serokell
            (c) 2018-2021 Kowainik
SPDX-License-Identifier: MIT
Maintainer:  Kowainik <xrom.xkov@gmail.com>
Stability:   Stable
Portability: Portable

Lifted versions of functions that work with exit processes.
-}

module Relude.Lifted.Exit
    ( exitWith
    , exitFailure
    , exitSuccess
    , die
    ) where

import Control.Monad.Trans (MonadIO, liftIO)
import Data.String (String)
import System.Exit (ExitCode)

import qualified System.Exit as XIO


-- $setup
-- >>> import Relude
-- >>> import System.Exit (ExitCode (..))


{- | Lifted version of 'System.Exit.exitWith'.

>>> exitWith (ExitFailure 3)
*** Exception: ExitFailure 3

>>> exitWith ExitSuccess
*** Exception: ExitSuccess
-}
exitWith :: MonadIO m => ExitCode -> m a
exitWith :: ExitCode -> m a
exitWith ExitCode
a = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ExitCode -> IO a
forall a. ExitCode -> IO a
XIO.exitWith ExitCode
a)

{- | Lifted version of 'System.Exit.exitFailure'.

>>> exitFailure
*** Exception: ExitFailure 1
-}
exitFailure :: MonadIO m => m a
exitFailure :: m a
exitFailure = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
forall a. IO a
XIO.exitFailure

{- | Lifted version of 'System.Exit.exitSuccess'.

>>> exitSuccess
*** Exception: ExitSuccess
-}
exitSuccess :: MonadIO m => m a
exitSuccess :: m a
exitSuccess = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
forall a. IO a
XIO.exitSuccess

{- | Lifted version of 'System.Exit.die'.

>>> die "Goodbye!"
Goodbye!
*** Exception: ExitFailure 1
-}
die :: MonadIO m => String -> m a
die :: String -> m a
die String
err = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (String -> IO a
forall a. String -> IO a
XIO.die String
err)