module Ribosome.Host.Remote where

import Ribosome.Host.Data.HostConfig (HostConfig)
import Ribosome.Host.Data.Report (Report)
import Ribosome.Host.Data.RpcHandler (RpcHandler)
import Ribosome.Host.Effect.Handlers (Handlers)
import Ribosome.Host.IOStack (BasicStack, IOStack, runBasicStack)
import Ribosome.Host.Interpreter.Handlers (interpretHandlers)
import Ribosome.Host.Interpreter.Host (runHost)
import Ribosome.Host.Interpreter.Process.Stdio (interpretProcessCerealStdio)
import Ribosome.Host.Interpreter.UserError (interpretUserErrorInfo)
import Ribosome.Host.Run (RpcDeps, RpcStack, interpretRpcStack)

type HostRemoteStack =
  RpcStack ++ RpcDeps

type HostRemoteIOStack =
  HostRemoteStack ++ BasicStack

interpretRpcDeps ::
  Members IOStack r =>
  InterpretersFor RpcDeps r
interpretRpcDeps :: forall (r :: [(* -> *) -> * -> *]).
Members IOStack r =>
InterpretersFor RpcDeps r
interpretRpcDeps =
  Sem (UserError : r) a -> Sem r a
forall (r :: [(* -> *) -> * -> *]). InterpreterFor UserError r
interpretUserErrorInfo (Sem (UserError : r) a -> Sem r a)
-> (Sem
      (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
    -> Sem (UserError : r) a)
-> Sem
     (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
-> Sem (UserError : r) a
forall a (r :: [(* -> *) -> * -> *]).
(Serialize a, Members IOStack r) =>
InterpreterFor (Process a (Either Text a)) r
interpretProcessCerealStdio

interpretHostRemote ::
  Members BasicStack r =>
  InterpretersFor HostRemoteStack r
interpretHostRemote :: forall (r :: [(* -> *) -> * -> *]).
Members BasicStack r =>
InterpretersFor HostRemoteStack r
interpretHostRemote =
  Sem (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
-> Sem r a
forall (r :: [(* -> *) -> * -> *]).
Members IOStack r =>
InterpretersFor RpcDeps r
interpretRpcDeps (Sem
   (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
 -> Sem r a)
-> (Sem
      (Log
         : DataLog LogReport : Resumable RpcError Rpc
         : Resumable RpcError (Responses RequestId Response)
         : Events (OutChan Event) Event
         : PScoped () (EventChan Event) (Consume Event) : Reports
         : Process RpcMessage (Either Text RpcMessage) : UserError : r)
      a
    -> Sem
         (Process RpcMessage (Either Text RpcMessage) : UserError : r) a)
-> Sem
     (Log
        : DataLog LogReport : Resumable RpcError Rpc
        : Resumable RpcError (Responses RequestId Response)
        : Events (OutChan Event) Event
        : PScoped () (EventChan Event) (Consume Event) : Reports
        : Process RpcMessage (Either Text RpcMessage) : UserError : r)
     a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  (Log
     : DataLog LogReport : Resumable RpcError Rpc
     : Resumable RpcError (Responses RequestId Response)
     : Events (OutChan Event) Event
     : PScoped () (EventChan Event) (Consume Event) : Reports
     : Process RpcMessage (Either Text RpcMessage) : UserError : r)
  a
-> Sem
     (Process RpcMessage (Either Text RpcMessage) : UserError : r) a
forall (r :: [(* -> *) -> * -> *]).
(Members IOStack r, Members RpcDeps r,
 Members '[Log, Reader LogConfig] r) =>
InterpretersFor RpcStack r
interpretRpcStack

runHostRemote ::
  Members BasicStack r =>
  InterpreterFor (Handlers !! Report) (HostRemoteStack ++ r) ->
  Sem r ()
runHostRemote :: forall (r :: [(* -> *) -> * -> *]).
Members BasicStack r =>
InterpreterFor (Handlers !! Report) (HostRemoteStack ++ r)
-> Sem r ()
runHostRemote InterpreterFor (Handlers !! Report) (HostRemoteStack ++ r)
handlers =
  Sem (HostRemoteStack ++ r) () -> Sem r ()
forall (r :: [(* -> *) -> * -> *]).
Members BasicStack r =>
InterpretersFor HostRemoteStack r
interpretHostRemote (Sem ((Handlers !! Report) : (HostRemoteStack ++ r)) ()
-> Sem (HostRemoteStack ++ r) ()
InterpreterFor (Handlers !! Report) (HostRemoteStack ++ r)
handlers Sem ((Handlers !! Report) : (HostRemoteStack ++ r)) ()
forall er (r :: [(* -> *) -> * -> *]).
Members (HostDeps er) r =>
Sem r ()
runHost)

runHostRemoteIO ::
  HostConfig ->
  [RpcHandler HostRemoteIOStack] ->
  IO ()
runHostRemoteIO :: HostConfig -> [RpcHandler HostRemoteIOStack] -> IO ()
runHostRemoteIO HostConfig
conf [RpcHandler HostRemoteIOStack]
handlers =
  HostConfig -> Sem BasicStack () -> IO ()
runBasicStack HostConfig
conf (Sem BasicStack () -> IO ()) -> Sem BasicStack () -> IO ()
forall a b. (a -> b) -> a -> b
$
  InterpreterFor
  (Handlers !! Report)
  (HostRemoteStack
   ++ '[Log, Tagged "stderr" Log, Tagged "file" Log, Reader LogConfig,
        Reader HostConfig, Time Time Date, Error BootError,
        UninterruptibleMaskIO, UninterruptibleMaskIO, GatesIO, Race, Async,
        Resource, Embed IO, Final IO])
-> Sem
     '[Log, Tagged "stderr" Log, Tagged "file" Log, Reader LogConfig,
       Reader HostConfig, Time Time Date, Error BootError,
       UninterruptibleMaskIO, UninterruptibleMaskIO, GatesIO, Race, Async,
       Resource, Embed IO, Final IO]
     ()
forall (r :: [(* -> *) -> * -> *]).
Members BasicStack r =>
InterpreterFor (Handlers !! Report) (HostRemoteStack ++ r)
-> Sem r ()
runHostRemote ([RpcHandler
   '[Log, DataLog LogReport, Resumable RpcError Rpc,
     Resumable RpcError (Responses RequestId Response),
     Events (OutChan Event) Event,
     PScoped () (EventChan Event) (Consume Event), Reports,
     Process RpcMessage (Either Text RpcMessage), UserError, Log,
     Tagged "stderr" Log, Tagged "file" Log, Reader LogConfig,
     Reader HostConfig, Time Time Date, Error BootError,
     UninterruptibleMaskIO, UninterruptibleMaskIO, GatesIO, Race, Async,
     Resource, Embed IO, Final IO]]
-> InterpreterFor
     (Handlers !! Report)
     '[Log, DataLog LogReport, Resumable RpcError Rpc,
       Resumable RpcError (Responses RequestId Response),
       Events (OutChan Event) Event,
       PScoped () (EventChan Event) (Consume Event), Reports,
       Process RpcMessage (Either Text RpcMessage), UserError, Log,
       Tagged "stderr" Log, Tagged "file" Log, Reader LogConfig,
       Reader HostConfig, Time Time Date, Error BootError,
       UninterruptibleMaskIO, UninterruptibleMaskIO, GatesIO, Race, Async,
       Resource, Embed IO, Final IO]
forall (r :: [(* -> *) -> * -> *]).
Members '[Resumable RpcError Rpc, Log, Error BootError] r =>
[RpcHandler r] -> InterpreterFor (Handlers !! Report) r
interpretHandlers [RpcHandler
   '[Log, DataLog LogReport, Resumable RpcError Rpc,
     Resumable RpcError (Responses RequestId Response),
     Events (OutChan Event) Event,
     PScoped () (EventChan Event) (Consume Event), Reports,
     Process RpcMessage (Either Text RpcMessage), UserError, Log,
     Tagged "stderr" Log, Tagged "file" Log, Reader LogConfig,
     Reader HostConfig, Time Time Date, Error BootError,
     UninterruptibleMaskIO, UninterruptibleMaskIO, GatesIO, Race, Async,
     Resource, Embed IO, Final IO]]
[RpcHandler HostRemoteIOStack]
handlers)