{-# options_haddock prune #-}

-- |Description: Process Interpreters for stdio, Internal
module Polysemy.Process.Interpreter.ProcessStdio where

import Polysemy.Conc.Effect.Race (Race)
import Polysemy.Conc.Effect.Scoped (Scoped)
import Polysemy.Resume (type (!!))
import System.Process.Typed (ProcessConfig)

import Polysemy.Process.Data.ProcessError (ProcessError)
import Polysemy.Process.Data.SystemProcessError (SystemProcessError)
import Polysemy.Process.Effect.Process (Process)
import Polysemy.Process.Interpreter.Process (
  interpretProcessByteString,
  interpretProcessByteStringLines,
  interpretProcessText,
  interpretProcessTextLines,
  )
import Polysemy.Process.Interpreter.SystemProcess (PipesProcess, interpretSystemProcessNative)

-- |Interpret 'Process' as a native 'Polysemy.Process.SystemProcess', producing unaccumulated chunks of 'ByteString'.
interpretProcessByteStringNative ::
  Members [Resource, Race, Async, Embed IO] r =>
  -- |Whether to discard output chunks if the queue is full.
  Bool ->
  -- |Maximum number of chunks allowed to be queued for each of the three standard pipes.
  Int ->
  -- |Basic config. The pipes will be changed to 'System.IO.Handle' by the interpreter.
  ProcessConfig () () () ->
  InterpreterFor (Scoped () (Process ByteString ByteString ByteString) !! ProcessError) r
interpretProcessByteStringNative :: forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Async, Embed IO] r =>
Bool
-> Int
-> ProcessConfig () () ()
-> InterpreterFor
     (Scoped () (Process ByteString ByteString ByteString)
      !! ProcessError)
     r
interpretProcessByteStringNative Bool
discard Int
qSize ProcessConfig () () ()
conf =
  ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Embed IO] r =>
ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
interpretSystemProcessNative ProcessConfig () () ()
conf (Sem
   (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
 -> Sem r a)
-> (Sem
      ((Scoped () (Process ByteString ByteString ByteString)
        !! ProcessError)
         : r)
      a
    -> Sem
         (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall resource err (r :: [(* -> *) -> * -> *]).
Members
  '[Scoped resource (SystemProcess !! err), Resource, Race, Async,
    Embed IO]
  r =>
Bool
-> Int
-> InterpreterFor
     (Scoped () (Process ByteString ByteString ByteString)
      !! ProcessError)
     r
interpretProcessByteString @PipesProcess @SystemProcessError Bool
discard Int
qSize (Sem
   ((Scoped () (Process ByteString ByteString ByteString)
     !! ProcessError)
      : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
   a
 -> Sem
      (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> (Sem
      ((Scoped () (Process ByteString ByteString ByteString)
        !! ProcessError)
         : r)
      a
    -> Sem
         ((Scoped () (Process ByteString ByteString ByteString)
           !! ProcessError)
            : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
         a)
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : r)
     a
-> Sem
     (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((Scoped () (Process ByteString ByteString ByteString)
    !! ProcessError)
     : r)
  a
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder

-- |Interpret 'Process' as a native 'Polysemy.Process.SystemProcess', producing lines of 'ByteString'.
interpretProcessByteStringLinesNative ::
  Members [Resource, Race, Async, Embed IO] r =>
  -- |Whether to discard output chunks if the queue is full.
  Bool ->
  -- |Maximum number of chunks allowed to be queued for each of the three standard pipes.
  Int ->
  -- |Basic config. The pipes will be changed to 'System.IO.Handle' by the interpreter.
  ProcessConfig () () () ->
  InterpreterFor (Scoped () (Process ByteString ByteString ByteString) !! ProcessError) r
interpretProcessByteStringLinesNative :: forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Async, Embed IO] r =>
Bool
-> Int
-> ProcessConfig () () ()
-> InterpreterFor
     (Scoped () (Process ByteString ByteString ByteString)
      !! ProcessError)
     r
interpretProcessByteStringLinesNative Bool
discard Int
qSize ProcessConfig () () ()
conf =
  ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Embed IO] r =>
ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
interpretSystemProcessNative ProcessConfig () () ()
conf (Sem
   (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
 -> Sem r a)
-> (Sem
      ((Scoped () (Process ByteString ByteString ByteString)
        !! ProcessError)
         : r)
      a
    -> Sem
         (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall resource err (r :: [(* -> *) -> * -> *]).
Members
  '[Scoped resource (SystemProcess !! err), Resource, Race, Async,
    Embed IO]
  r =>
Bool
-> Int
-> InterpreterFor
     (Scoped () (Process ByteString ByteString ByteString)
      !! ProcessError)
     r
interpretProcessByteStringLines @PipesProcess @SystemProcessError Bool
discard Int
qSize (Sem
   ((Scoped () (Process ByteString ByteString ByteString)
     !! ProcessError)
      : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
   a
 -> Sem
      (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> (Sem
      ((Scoped () (Process ByteString ByteString ByteString)
        !! ProcessError)
         : r)
      a
    -> Sem
         ((Scoped () (Process ByteString ByteString ByteString)
           !! ProcessError)
            : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
         a)
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : r)
     a
-> Sem
     (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((Scoped () (Process ByteString ByteString ByteString)
    !! ProcessError)
     : r)
  a
-> Sem
     ((Scoped () (Process ByteString ByteString ByteString)
       !! ProcessError)
        : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder

-- |Interpret 'Process' as a native 'Polysemy.Process.SystemProcess', producing unaccumulated chunks of 'Text'.
interpretProcessTextNative ::
  Members [Resource, Race, Async, Embed IO] r =>
  -- |Whether to discard output chunks if the queue is full.
  Bool ->
  -- |Maximum number of chunks allowed to be queued for each of the three standard pipes.
  Int ->
  -- |Basic config. The pipes will be changed to 'System.IO.Handle' by the interpreter.
  ProcessConfig () () () ->
  InterpreterFor (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessTextNative :: forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Async, Embed IO] r =>
Bool
-> Int
-> ProcessConfig () () ()
-> InterpreterFor
     (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessTextNative Bool
discard Int
qSize ProcessConfig () () ()
conf =
  ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Embed IO] r =>
ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
interpretSystemProcessNative ProcessConfig () () ()
conf (Sem
   (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
 -> Sem r a)
-> (Sem
      ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
    -> Sem
         (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall resource err (r :: [(* -> *) -> * -> *]).
Members
  '[Scoped resource (SystemProcess !! err), Resource, Race, Async,
    Embed IO]
  r =>
Bool
-> Int
-> InterpreterFor
     (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessText @PipesProcess @SystemProcessError Bool
discard Int
qSize (Sem
   ((Scoped () (Process ByteString Text Text) !! ProcessError)
      : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
   a
 -> Sem
      (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> (Sem
      ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
    -> Sem
         ((Scoped () (Process ByteString Text Text) !! ProcessError)
            : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
         a)
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem
     (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError)
        : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder

-- |Interpret 'Process' as a native 'Polysemy.Process.SystemProcess', producing lines of 'Text'.
interpretProcessTextLinesNative ::
  Members [Resource, Race, Async, Embed IO] r =>
  -- |Whether to discard output chunks if the queue is full.
  Bool ->
  -- |Maximum number of chunks allowed to be queued for each of the three standard pipes.
  Int ->
  -- |Basic config. The pipes will be changed to 'System.IO.Handle' by the interpreter.
  ProcessConfig () () () ->
  InterpreterFor (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessTextLinesNative :: forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Race, Async, Embed IO] r =>
Bool
-> Int
-> ProcessConfig () () ()
-> InterpreterFor
     (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessTextLinesNative Bool
discard Int
qSize ProcessConfig () () ()
conf =
  ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
forall (r :: [(* -> *) -> * -> *]).
Members '[Resource, Embed IO] r =>
ProcessConfig () () ()
-> InterpreterFor
     (Scoped PipesProcess (SystemProcess !! SystemProcessError)) r
interpretSystemProcessNative ProcessConfig () () ()
conf (Sem
   (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
 -> Sem r a)
-> (Sem
      ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
    -> Sem
         (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall resource err (r :: [(* -> *) -> * -> *]).
Members
  '[Scoped resource (SystemProcess !! err), Resource, Race, Async,
    Embed IO]
  r =>
Bool
-> Int
-> InterpreterFor
     (Scoped () (Process ByteString Text Text) !! ProcessError) r
interpretProcessTextLines @PipesProcess @SystemProcessError Bool
discard Int
qSize (Sem
   ((Scoped () (Process ByteString Text Text) !! ProcessError)
      : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
   a
 -> Sem
      (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a)
-> (Sem
      ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
    -> Sem
         ((Scoped () (Process ByteString Text Text) !! ProcessError)
            : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
         a)
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem
     (Scoped PipesProcess (SystemProcess !! SystemProcessError) : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  ((Scoped () (Process ByteString Text Text) !! ProcessError) : r) a
-> Sem
     ((Scoped () (Process ByteString Text Text) !! ProcessError)
        : Scoped PipesProcess (SystemProcess !! SystemProcessError) : r)
     a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder