module Data.Streaming.Process.Internal
    ( StreamingProcessHandle (..)
    , InputSource (..)
    , OutputSink (..)
    ) where

import           Control.Concurrent.STM (TMVar)
import           System.Exit            (ExitCode)
import           System.IO              (Handle)
import           System.Process         (ProcessHandle, StdStream (CreatePipe))

-- | Class for all things which can be used to provide standard input.
--
-- Since 0.1.4
class InputSource a where
    isStdStream :: (Maybe Handle -> IO a, Maybe StdStream)
instance InputSource Handle where
    isStdStream :: (Maybe Handle -> IO Handle, Maybe StdStream)
isStdStream = (\(Just Handle
h) -> Handle -> IO Handle
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Handle
h, StdStream -> Maybe StdStream
forall a. a -> Maybe a
Just StdStream
CreatePipe)

-- | Class for all things which can be used to consume standard output or
-- error.
--
-- Since 0.1.4
class OutputSink a where
    osStdStream :: (Maybe Handle -> IO a, Maybe StdStream)
instance OutputSink Handle where
    osStdStream :: (Maybe Handle -> IO Handle, Maybe StdStream)
osStdStream = (\(Just Handle
h) -> Handle -> IO Handle
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Handle
h, StdStream -> Maybe StdStream
forall a. a -> Maybe a
Just StdStream
CreatePipe)

-- | Wraps up the standard @ProcessHandle@ to avoid the @waitForProcess@
-- deadlock. See the linked documentation from the module header for more
-- information.
--
-- Since 0.1.4
data StreamingProcessHandle = StreamingProcessHandle
    ProcessHandle
    (TMVar ExitCode)
    (IO ()) -- cleanup resources