From 4623c271067d63f7545e20d7c7cf3c491ec7cb28 Mon Sep 17 00:00:00 2001
From: Jens Nolte <git@queezle.net>
Date: Mon, 12 Jul 2021 17:59:42 +0200
Subject: [PATCH] Use Quasar.Prelude

---
 src/Quasar/Network/Connection.hs       |  6 ++----
 src/Quasar/Network/Multiplexer.hs      |  7 ++-----
 src/Quasar/Network/Runtime.hs          | 16 +---------------
 src/Quasar/Network/SocketLocation.hs   |  2 +-
 src/Quasar/Network/TH.hs               |  4 +---
 test/Quasar/Network/MultiplexerSpec.hs |  6 +++---
 6 files changed, 10 insertions(+), 31 deletions(-)

diff --git a/src/Quasar/Network/Connection.hs b/src/Quasar/Network/Connection.hs
index e2f747c..5b18ddb 100644
--- a/src/Quasar/Network/Connection.hs
+++ b/src/Quasar/Network/Connection.hs
@@ -7,15 +7,13 @@ module Quasar.Network.Connection (
 import Control.Concurrent (threadDelay)
 import Control.Concurrent.Async (Async, async, cancel, link, waitCatch, withAsync)
 import Control.Concurrent.MVar
-import Control.Exception (Exception(..), SomeException, bracketOnError, catch, interruptible, finally, throwIO, bracketOnError, onException)
-import Control.Monad ((>=>), unless, forM_)
+import Control.Exception (Exception(..), SomeException, bracketOnError, catch, interruptible, finally, bracketOnError, onException)
 import qualified Data.ByteString as BS
 import qualified Data.ByteString.Lazy as BSL
-import Data.List (intercalate)
 import qualified Network.Socket as Socket
 import qualified Network.Socket.ByteString as Socket
 import qualified Network.Socket.ByteString.Lazy as SocketL
-import Prelude
+import Quasar.Prelude
 
 -- | Abstraction over a bidirectional stream connection (e.g. a socket), to be able to switch to different communication channels (e.g. stdin/stdout or a dummy implementation for unit tests).
 data Connection = Connection {
diff --git a/src/Quasar/Network/Multiplexer.hs b/src/Quasar/Network/Multiplexer.hs
index 13ed48e..82b388a 100644
--- a/src/Quasar/Network/Multiplexer.hs
+++ b/src/Quasar/Network/Multiplexer.hs
@@ -24,9 +24,7 @@ module Quasar.Network.Multiplexer (
 
 
 import Control.Concurrent.Async (AsyncCancelled(..), async, link, race_, wait, waitAnyCancel, withAsync, withAsyncWithUnmask)
-import Control.Exception (Exception(..), Handler(..), MaskingState(Unmasked), SomeException(..), catch, catches, handle, interruptible, throwIO, getMaskingState, mask_)
-import Control.Monad (when, unless, void)
-import Control.Monad.IO.Class (liftIO)
+import Control.Exception (Exception(..), Handler(..), MaskingState(Unmasked), SomeException(..), catch, catches, handle, interruptible, getMaskingState, mask_)
 import Control.Monad.State (StateT, execStateT, runStateT, lift)
 import qualified Control.Monad.State as State
 import Control.Concurrent.MVar
@@ -39,8 +37,7 @@ import qualified Data.HashMap.Strict as HM
 import Data.Tuple (swap)
 import Data.Word
 import Quasar.Network.Connection
-import Prelude
-import GHC.Generics
+import Quasar.Prelude
 import System.IO (hPutStrLn, stderr)
 
 -- NOTE this module got more complicated than expected and should be refactored to encode the core protocol interactions in pure code, with an IO wrapper that handles usage, callbacks and sending/receiving.
diff --git a/src/Quasar/Network/Runtime.hs b/src/Quasar/Network/Runtime.hs
index b171ee5..5356d3c 100644
--- a/src/Quasar/Network/Runtime.hs
+++ b/src/Quasar/Network/Runtime.hs
@@ -42,17 +42,14 @@ module Quasar.Network.Runtime (
 import Control.Concurrent (forkFinally)
 import Control.Concurrent.Async (cancel, link, withAsync, mapConcurrently_)
 import Control.Exception (SomeException, bracket, bracketOnError, bracketOnError, interruptible, mask_)
-import Control.Monad (when, unless, forever, void)
-import qualified Control.Monad.State as State
 import Control.Concurrent.MVar
 import Data.Binary (Binary, encode, decodeOrFail)
 import qualified Data.ByteString.Lazy as BSL
-import Data.Hashable (Hashable)
 import qualified Data.HashMap.Strict as HM
 import qualified Network.Socket as Socket
-import Prelude
 import Quasar.Network.Connection
 import Quasar.Network.Multiplexer
+import Quasar.Prelude
 import System.Posix.Files (getFileStatus, isSocket, fileExist, removeLink)
 
 
@@ -305,14 +302,3 @@ newLocalClient server = do
 
 withStandaloneClient :: forall p a. (RpcProtocol p, HasProtocolImpl p) => ProtocolImpl p -> (Client p -> IO a) -> IO a
 withStandaloneClient impl runClientHook = withServer impl [] $ \server -> withLocalClient server runClientHook
-
-
-
--- * Helper functions
-
--- | Lookup and delete a value from a HashMap in one operation
-lookupDelete :: forall k v. (Eq k, Hashable k) => k -> HM.HashMap k v -> (HM.HashMap k v, Maybe v)
-lookupDelete key m = State.runState fn Nothing
-  where
-    fn :: State.State (Maybe v) (HM.HashMap k v)
-    fn = HM.alterF (\c -> State.put c >> pure Nothing) key m
diff --git a/src/Quasar/Network/SocketLocation.hs b/src/Quasar/Network/SocketLocation.hs
index f8ed7f1..afa6ad1 100644
--- a/src/Quasar/Network/SocketLocation.hs
+++ b/src/Quasar/Network/SocketLocation.hs
@@ -1,7 +1,7 @@
 module Quasar.Network.SocketLocation where
 
 import Control.Exception (handle)
-import Prelude
+import Quasar.Prelude
 import System.Environment (getEnv)
 
 systemSocketPath :: String -> IO FilePath
diff --git a/src/Quasar/Network/TH.hs b/src/Quasar/Network/TH.hs
index 7af9bef..a8def53 100644
--- a/src/Quasar/Network/TH.hs
+++ b/src/Quasar/Network/TH.hs
@@ -16,8 +16,6 @@ module Quasar.Network.TH (
   HasProtocolImpl
 ) where
 
-import Control.Applicative (liftA2)
-import Control.Monad (unless)
 import Control.Monad.State (State, execState)
 import qualified Control.Monad.State as State
 import Data.Binary (Binary)
@@ -25,9 +23,9 @@ import Data.Maybe (isNothing)
 import GHC.Generics
 import Language.Haskell.TH hiding (interruptible)
 import Language.Haskell.TH.Syntax
-import Prelude
 import Quasar.Network.Multiplexer
 import Quasar.Network.Runtime
+import Quasar.Prelude
 
 data RpcApi = RpcApi {
   name :: String,
diff --git a/test/Quasar/Network/MultiplexerSpec.hs b/test/Quasar/Network/MultiplexerSpec.hs
index 88998eb..1291288 100644
--- a/test/Quasar/Network/MultiplexerSpec.hs
+++ b/test/Quasar/Network/MultiplexerSpec.hs
@@ -100,7 +100,7 @@ spec = describe "runMultiplexerProtocol" $ parallel $ do
 
 
 withEchoServer :: (Channel -> IO a) -> IO a
-withEchoServer fn = bracket setup close (\(channel, _) -> fn channel)
+withEchoServer fn = bracket setup closePair (\(channel, _) -> fn channel)
   where
     setup :: IO (Channel, Channel)
     setup = do
@@ -109,8 +109,8 @@ withEchoServer fn = bracket setup close (\(channel, _) -> fn channel)
       echoChannel <- newMultiplexer MultiplexerSideB echoSocket
       configureEchoHandler echoChannel
       pure (mainChannel, echoChannel)
-    close :: (Channel, Channel) -> IO ()
-    close (x, y) = channelClose x >> channelClose y
+    closePair :: (Channel, Channel) -> IO ()
+    closePair (x, y) = channelClose x >> channelClose y
     configureEchoHandler :: Channel -> IO ()
     configureEchoHandler channel = channelSetHandler channel (echoHandler channel)
     echoHandler :: Channel -> ReceivedMessageResources -> BSL.ByteString -> IO ()
-- 
GitLab