{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}

-----------------------------------------------------------------------------

-- |

-- Module      :  System.Exit.Lens

-- Copyright   :  (C) 2013-16 Edward Kmett

-- License     :  BSD-style (see the file LICENSE)

-- Maintainer  :  Edward Kmett <ekmett@gmail.com>

-- Stability   :  provisional

-- Portability :  Control.Exception

--

-- These prisms can be used with the combinators in "Control.Exception.Lens".

----------------------------------------------------------------------------

module System.Exit.Lens
  ( AsExitCode(..)
  , _ExitFailure
  , _ExitSuccess
  , pattern ExitFailure_
  , pattern ExitSuccess_
  ) where

import Prelude ()

import Control.Exception
import Control.Exception.Lens
import Control.Lens
import Control.Lens.Internal.Prelude
import System.Exit

-- | Exit codes that a program can return with:

class AsExitCode t where
  _ExitCode :: Prism' t ExitCode

instance AsExitCode ExitCode where
  _ExitCode :: Prism' ExitCode ExitCode
_ExitCode = forall a. a -> a
id
  {-# INLINE _ExitCode #-}

instance AsExitCode SomeException where
  _ExitCode :: Prism' SomeException ExitCode
_ExitCode = forall a. Exception a => Prism' SomeException a
exception
  {-# INLINE _ExitCode #-}

-- | indicates successful termination;

--

-- @

-- '_ExitSuccess' :: 'Prism'' 'ExitCode'      ()

-- '_ExitSuccess' :: 'Prism'' 'SomeException' ()

-- @

_ExitSuccess :: AsExitCode t => Prism' t ()
_ExitSuccess :: forall t. AsExitCode t => Prism' t ()
_ExitSuccess = forall t. AsExitCode t => Prism' t ExitCode
_ExitCode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap forall {f :: * -> *}.
Applicative f =>
ExitCode -> Either (f ExitCode) ()
seta (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id forall a. a -> a
id) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap (ExitCode
ExitSuccess forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$) where
  seta :: ExitCode -> Either (f ExitCode) ()
seta ExitCode
ExitSuccess = forall a b. b -> Either a b
Right ()
  seta ExitCode
t           = forall a b. a -> Either a b
Left  (forall (f :: * -> *) a. Applicative f => a -> f a
pure ExitCode
t)
{-# INLINE _ExitSuccess #-}


-- | indicates program failure with an exit code. The exact interpretation of the code is operating-system dependent. In particular, some values may be prohibited (e.g. 0 on a POSIX-compliant system).

--

-- @

-- '_ExitFailure' :: 'Prism'' 'ExitCode'      'Int'

-- '_ExitFailure' :: 'Prism'' 'SomeException' 'Int'

-- @

_ExitFailure :: AsExitCode t => Prism' t Int
_ExitFailure :: forall t. AsExitCode t => Prism' t Int
_ExitFailure = forall t. AsExitCode t => Prism' t ExitCode
_ExitCode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap forall {f :: * -> *}.
Applicative f =>
ExitCode -> Either (f ExitCode) Int
seta (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id forall a. a -> a
id) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) a b c.
Choice p =>
p a b -> p (Either c a) (Either c b)
right' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> ExitCode
ExitFailure) where
  seta :: ExitCode -> Either (f ExitCode) Int
seta (ExitFailure Int
i) = forall a b. b -> Either a b
Right Int
i
  seta ExitCode
t               = forall a b. a -> Either a b
Left  (forall (f :: * -> *) a. Applicative f => a -> f a
pure ExitCode
t)
{-# INLINE _ExitFailure #-}

pattern ExitSuccess_ :: AsExitCode s => s
pattern $bExitSuccess_ :: forall s. AsExitCode s => s
$mExitSuccess_ :: forall {r} {s}.
AsExitCode s =>
s -> ((# #) -> r) -> ((# #) -> r) -> r
ExitSuccess_ <- (has _ExitSuccess -> True) where
  ExitSuccess_ = forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review forall t. AsExitCode t => Prism' t ()
_ExitSuccess ()

pattern ExitFailure_ :: AsExitCode s => Int -> s
pattern $bExitFailure_ :: forall s. AsExitCode s => Int -> s
$mExitFailure_ :: forall {r} {s}.
AsExitCode s =>
s -> (Int -> r) -> ((# #) -> r) -> r
ExitFailure_ a <- (preview _ExitFailure -> Just a) where
  ExitFailure_ Int
a = forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review forall t. AsExitCode t => Prism' t Int
_ExitFailure Int
a