A peer-to-peer stack for code collaboration
Rename 'Storage' to 'MachineBackend'
Thomas Scholtesa06156dde9e5
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hsindex ecf33fd..c62fb8b 100644--- a/exe/RadicleExe.hs+++ b/exe/RadicleExe.hs@@ -9,7 +9,7 @@ import System.Directory (doesFileExist)import Radicleimport Radicle.Internal.Effects (exitCode)-import Radicle.Internal.HttpStorage+import Radicle.Internal.MachineBackend.EvalServerimport Radicle.Internal.Pretty (putPrettyAnsi)main :: IO ()@@ -88,5 +88,5 @@ opts = OptscreateBindings :: (MonadIO m, ReplM m) => [Text] -> IO (Bindings (PrimFns m))createBindings scriptArgs' = do- storage <- createHttpStoragePrimFns- pure $ addPrimFns (replPrimFns scriptArgs' <> storage) pureEnv+ machineBackendPrimFns <- createEvalServerBackendPrimFns+ pure $ addPrimFns (replPrimFns scriptArgs' <> machineBackendPrimFns) pureEnvdiff --git a/exe/Server.hs b/exe/Server.hsindex 31f3488..1fd016e 100644--- a/exe/Server.hs+++ b/exe/Server.hs@@ -48,7 +48,7 @@ import Servant)import Radicle-import Radicle.Internal.HttpStorage+import Radicle.Internal.MachineBackend.EvalServerimport Server.DB-- * Typesdiff --git a/src/Radicle/Internal/HttpStorage.hs b/src/Radicle/Internal/MachineBackend/EvalServer.hssimilarity index 89%rename from src/Radicle/Internal/HttpStorage.hsrename to src/Radicle/Internal/MachineBackend/EvalServer.hsindex d6b415a..216292b 100644--- a/src/Radicle/Internal/HttpStorage.hs+++ b/src/Radicle/Internal/MachineBackend/EvalServer.hs@@ -1,13 +1,14 @@{-# OPTIONS_GHC -fno-warn-orphans #-}--- | Provide Radicle primitives for storing chains through an HTTP API.-module Radicle.Internal.HttpStorage+-- | Defines a machine backend that uses an HTTP controlled server that+-- also evaluates submitted expressions.+module Radicle.Internal.MachineBackend.EvalServer( ChainSubmitEndpoint, chainSubmitEndpoint, ChainSinceEndpoint, chainSinceEndpoint, Values(..)- , createHttpStoragePrimFns+ , createEvalServerBackendPrimFns) whereimport Protolude hiding (TypeError, toList)@@ -21,7 +22,7 @@ import Servant.Clientimport Radicleimport Radicle.Internal.Annotation (thisPos)-import Radicle.Internal.Storage+import Radicle.Internal.MachineBackend.Interface--@@ -82,15 +83,15 @@ chainSinceEndpoint = Proxy-- | Returns definitions for the primitive functions-- @send!@ and @receive@.-createHttpStoragePrimFns :: MonadIO m => IO (PrimFns m)-createHttpStoragePrimFns = do+createEvalServerBackendPrimFns :: MonadIO m => IO (PrimFns m)+createEvalServerBackendPrimFns = dohttpMgr <- HttpClient.newManager HttpClient.defaultManagerSettings- pure $ httpStoragePrimFns' httpMgr+ pure $ httpMachineBackendPrimFns httpMgr-httpStoragePrimFns' :: MonadIO m => HttpClient.Manager -> PrimFns m-httpStoragePrimFns' mgr =- buildStoragePrimFns StorageBackend- { storageSend =+httpMachineBackendPrimFns :: MonadIO m => HttpClient.Manager -> PrimFns m+httpMachineBackendPrimFns mgr =+ buildMachineBackendPrimFns MachineBackend+ { machineUpdate =( "send!", "Given a URL string and a value, sends the value `v` to the remote\\ chain located at the URL for evaluation. Returns the index of the\@@ -103,7 +104,7 @@ httpStoragePrimFns' mgr =Left err -> Left $ "cannot parse server response: " <> errRight n -> Right n)- , storageReceive =+ , machineGetLog =( "receive!", "Given a URL string and a `[:just n]`, where `n` is an integer, returns\\ all inputs from the remote chain after index `n`. If the second argument\diff --git a/src/Radicle/Internal/Storage.hs b/src/Radicle/Internal/MachineBackend/Interface.hssimilarity index 62%rename from src/Radicle/Internal/Storage.hsrename to src/Radicle/Internal/MachineBackend/Interface.hsindex adca797..9fdf5ff 100644--- a/src/Radicle/Internal/Storage.hs+++ b/src/Radicle/Internal/MachineBackend/Interface.hs@@ -1,18 +1,19 @@--- | Defines and abstract interface 'StorageBackend' for Radicle--- chains and 'buildStoragePrimFns' to add the storage backend as--- primitive functions.+-- | This module defines an abstract 'MachineBackend' interface to+-- interact with Radicle State Machines. We provide the+-- 'buildMachinePrimFns' to add an implementation of 'MachineBackend'+-- to the primitive functions of the interpreter----- A storage backend defines two functions 'StorageSend' and--- 'StorageReceive' explained below.+-- A storage backend defines two functions 'MachineUpdate' and+-- 'MachineGetLog' explained below.----- Storage backends use /indices/ to identify entries in a chain. These+-- Machine backends use /indices/ to identify entries in a chain. These-- indices can be used like cursors in a database.---- For usage examples see "Radicle.Internal.HttpStorage" and-- "Radicle.Internal.TestCapabilities".-module Radicle.Internal.Storage- ( StorageBackend(..)- , buildStoragePrimFns+module Radicle.Internal.MachineBackend.Interface+ ( MachineBackend(..)+ , buildMachineBackendPrimFns) whereimport Protolude hiding (TypeError)@@ -29,34 +30,36 @@ import Radicle.Internal.Type---- The first tuple item is the Radicle identifier the function will be-- exposed as. The second tuple item is documentation.-data StorageBackend i m = StorageBackend- { storageSend :: (Text, Text, StorageSend i m)- , storageReceive :: (Text, Text, StorageReceive i m)+data MachineBackend i m = MachineBackend+ { machineUpdate :: (Text, Text, MachineUpdate i m)+ , machineGetLog :: (Text, Text, MachineGetLog i m)}-- | Send a list of expressions to a chain identified by the first-- argument. Returns an error or the index of the expression that was-- sent.-type StorageSend i m = Text -> Seq Value -> m (Either Text i)+type MachineUpdate i m = Text -> Seq Value -> m (Either Text i)--- | Receive all expressions following the given expression and a new--- index for further queries.+-- | Get all inputs to a machine following the given input index and+-- return a new index for further queries.----- If second arugment is @'Just' i@ then we return all expressions that--- follow the expression index by @i@ and not including that--- expression.+-- If second arugment is @'Just' i@ then we return all input expresions+-- that follow the input indexed by @i@ and not including that+-- input.---- If the second argument is 'Nothing' we return all expressions.---- The first item in the tuple returned is the index of the last entry--- in the list of expressions returned.-type StorageReceive i m = Text -> Maybe i -> m (Either Text (i, [Value]))+-- in the list of inputs returned.+type MachineGetLog i m = Text -> Maybe i -> m (Either Text (i, [Value]))-buildStoragePrimFns :: forall i m. (Monad m, FromRad WithPos i, ToRad WithPos i) => StorageBackend i m -> PrimFns m-buildStoragePrimFns backend =+buildMachineBackendPrimFns+ :: forall i m. (Monad m, FromRad WithPos i, ToRad WithPos i)+ => MachineBackend i m -> PrimFns m+buildMachineBackendPrimFns backend =fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]where- (sendName, sendDoc, send) = storageSend backend+ (sendName, sendDoc, send) = machineUpdate backendsendPrimop =( sendName, sendDoc@@ -70,7 +73,7 @@ buildStoragePrimFns backend =(v, _) -> throwErrorHere $ TypeError sendName 0 TString v)- (receiveName, receiveDoc, receive) = storageReceive backend+ (receiveName, receiveDoc, receive) = machineGetLog backendreceivePrimop =( receiveName, receiveDocdiff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex 77176f3..5e47e6a 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -26,7 +26,7 @@ import qualified System.FilePath.Find as FPimport Radicleimport Radicle.Internal.Cryptoimport Radicle.Internal.Effects.Capabilities-import Radicle.Internal.Storage+import Radicle.Internal.MachineBackend.Interfaceimport qualified Radicle.Internal.UUID as UUID@@ -116,14 +116,14 @@ sourceFiles = do-- | Bindings with REPL and client stuff mocked.testBindings :: Bindings (PrimFns (StateT WorldState IO))-testBindings = addPrimFns storagePrimFns (replBindings [])+testBindings = addPrimFns memoryMachineBackendPrimFns (replBindings [])-- | Provides @send!@ and @receive!@ functions that store chains in a-- 'Map' in the 'WorldState'.-storagePrimFns :: PrimFns (StateT WorldState IO)-storagePrimFns =- buildStoragePrimFns StorageBackend- { storageSend =+memoryMachineBackendPrimFns :: PrimFns (StateT WorldState IO)+memoryMachineBackendPrimFns =+ buildMachineBackendPrimFns MachineBackend+ { machineUpdate =( "send!", "Mocked version of `send!` that stores sent values in a map with chains as keys.", \id values -> do@@ -134,7 +134,7 @@ storagePrimFns =s { worldStateRemoteChains = Map.insert id newExprs $ worldStateRemoteChains s }pure $ Right $ Seq.length newExprs)- , storageReceive =+ , machineGetLog =( "receive!", "Mocked version of `receive!` that retrieves values from a map with chains as keys.", \id maybeIndex -> do