{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
module System.Exit.Lens
( AsExitCode(..)
, _ExitFailure
, _ExitSuccess
) where
import Control.Exception
import Control.Exception.Lens
import Control.Lens
import System.Exit
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
class AsExitCode p f t where
_ExitCode :: Optic' p f t ExitCode
instance AsExitCode p f ExitCode where
_ExitCode = id
{-# INLINE _ExitCode #-}
instance (Choice p, Applicative f) => AsExitCode p f SomeException where
_ExitCode = exception
{-# INLINE _ExitCode #-}
_ExitSuccess :: (AsExitCode p f t, Choice p, Applicative f) => Optic' p f t ()
_ExitSuccess = _ExitCode . dimap seta (either id id) . right' . rmap (ExitSuccess <$) where
seta ExitSuccess = Right ()
seta t = Left (pure t)
{-# INLINE _ExitSuccess #-}
_ExitFailure :: (AsExitCode p f t, Choice p, Applicative f) => Optic' p f t Int
_ExitFailure = _ExitCode . dimap seta (either id id) . right' . rmap (fmap ExitFailure) where
seta (ExitFailure i) = Right i
seta t = Left (pure t)
{-# INLINE _ExitFailure #-}