Skip to content

Commit 78639b8

Browse files
committed
remote: NixSerializer, rename mapError to mapErrorST, add mapErrorS, mapReaderS
for transforming `NixSerializer`(s)
1 parent e57397b commit 78639b8

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module System.Nix.Store.Remote.Serializer
99
(
1010
-- * NixSerializer
1111
NixSerializer
12+
, mapReaderS
13+
, mapErrorS
1214
-- * Errors
1315
, SError(..)
1416
-- ** Runners
@@ -63,11 +65,11 @@ module System.Nix.Store.Remote.Serializer
6365
, verbosity
6466
) where
6567

66-
import Control.Monad.Except (MonadError, throwError, withExceptT)
68+
import Control.Monad.Except (MonadError, throwError, )
6769
import Control.Monad.Reader (MonadReader)
6870
import Control.Monad.Trans (MonadTrans, lift)
69-
import Control.Monad.Trans.Reader (ReaderT, runReaderT)
70-
import Control.Monad.Trans.Except (ExceptT, runExceptT)
71+
import Control.Monad.Trans.Reader (ReaderT, runReaderT, withReaderT)
72+
import Control.Monad.Trans.Except (ExceptT, mapExceptT, runExceptT, withExceptT)
7173
import Crypto.Hash (Digest, HashAlgorithm, SHA256)
7274
import Data.ByteString (ByteString)
7375
import Data.Dependent.Sum (DSum((:=>)))
@@ -146,16 +148,44 @@ runSerialT r =
146148
. runExceptT
147149
. _unSerialT
148150

149-
mapError
151+
mapErrorST
150152
:: Functor m
151153
=> (e -> e')
152154
-> SerialT r e m a
153155
-> SerialT r e' m a
154-
mapError f =
156+
mapErrorST f =
157+
SerialT
158+
. withExceptT f
159+
. _unSerialT
160+
161+
mapErrorS
162+
:: (e -> e')
163+
-> NixSerializer r e a
164+
-> NixSerializer r e' a
165+
mapErrorS f s = Serializer
166+
{ getS = mapErrorST f $ getS s
167+
, putS = mapErrorST f . putS s
168+
}
169+
170+
mapReaderST
171+
:: Functor m
172+
=> (r' -> r)
173+
-> SerialT r e m a
174+
-> SerialT r' e m a
175+
mapReaderST f =
155176
SerialT
156-
. withExceptT f
177+
. (mapExceptT . withReaderT) f
157178
. _unSerialT
158179

180+
mapReaderS
181+
:: (r' -> r)
182+
-> NixSerializer r e a
183+
-> NixSerializer r' e a
184+
mapReaderS f s = Serializer
185+
{ getS = mapReaderST f $ getS s
186+
, putS = mapReaderST f . putS s
187+
}
188+
159189
-- * NixSerializer
160190

161191
type NixSerializer r e = Serializer (SerialT r e)
@@ -677,7 +707,7 @@ mapPrimE
677707
:: Functor m
678708
=> SerialT r SError m a
679709
-> SerialT r LoggerSError m a
680-
mapPrimE = mapError LoggerSError_Prim
710+
mapPrimE = mapErrorST LoggerSError_Prim
681711

682712
maybeActivity :: NixSerializer r LoggerSError (Maybe Activity)
683713
maybeActivity = Serializer
@@ -822,8 +852,7 @@ logger = Serializer
822852
, putS = \case
823853
Logger_Next s -> do
824854
putS loggerOpCode LoggerOpCode_Next
825-
mapError LoggerSError_Prim $
826-
putS text s
855+
mapPrimE $ putS text s
827856

828857
Logger_Read i -> do
829858
putS loggerOpCode LoggerOpCode_Read

0 commit comments

Comments
 (0)