module Sys.StdStream(
StdStream(..)
, AsStdStream(..)
, AsInherit(..)
, AsUseHandle(..)
, AsCreatePipe
) where
import Control.Applicative(Applicative)
import Control.Category(Category(id))
import Control.Lens(Optic', Choice, Profunctor, prism', iso)
import Data.Maybe
import Data.Eq(Eq)
import Data.Functor(Functor)
import Prelude(Show)
import System.IO(Handle)
import qualified System.Process as Process
data StdStream =
Inherit
| UseHandle Handle
| CreatePipe
deriving (Eq, Show)
class AsStdStream p f s where
_StdStream ::
Optic' p f s StdStream
instance AsStdStream p f StdStream where
_StdStream =
id
instance (Profunctor p, Functor f) => AsStdStream p f Process.StdStream where
_StdStream =
iso
(\s -> case s of
Process.Inherit ->
Inherit
Process.UseHandle h ->
UseHandle h
Process.CreatePipe ->
CreatePipe)
(\s -> case s of
Inherit ->
Process.Inherit
UseHandle h ->
Process.UseHandle h
CreatePipe ->
Process.CreatePipe)
class AsInherit p f s where
_Inherit ::
Optic' p f s ()
instance AsInherit p f () where
_Inherit =
id
instance (Choice p, Applicative f) => AsInherit p f StdStream where
_Inherit =
prism'
(\() -> Inherit)
(\s -> case s of
Inherit ->
Just ()
UseHandle _ ->
Nothing
CreatePipe ->
Nothing)
class AsUseHandle p f s where
_UseHandle ::
Optic' p f s Handle
instance AsUseHandle p f Handle where
_UseHandle =
id
instance (Choice p, Applicative f) => AsUseHandle p f StdStream where
_UseHandle =
prism'
UseHandle
(\s -> case s of
Inherit ->
Nothing
UseHandle h ->
Just h
CreatePipe ->
Nothing)
class AsCreatePipe p f s where
_CreatePipe ::
Optic' p f s ()
instance AsCreatePipe p f () where
_CreatePipe =
id
instance (Choice p, Applicative f) => AsCreatePipe p f StdStream where
_CreatePipe =
prism'
(\() -> CreatePipe)
(\s -> case s of
Inherit ->
Nothing
UseHandle _ ->
Nothing
CreatePipe ->
Just ())