Skip to content

Commit 5aa852f

Browse files
committed
Refactor: Move ?! and ?!& to Cardano.Api.Monad.Error
1 parent 2729847 commit 5aa852f

File tree

7 files changed

+24
-21
lines changed

7 files changed

+24
-21
lines changed

cardano-api/src/Cardano/Api/Address.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ where
9292
import Cardano.Api.Byron.Internal.Key
9393
import Cardano.Api.Era
9494
import Cardano.Api.HasTypeProxy
95-
import Cardano.Api.Internal.Utils
9695
import Cardano.Api.Key.Internal
96+
import Cardano.Api.Monad.Error
9797
import Cardano.Api.Network.Internal.NetworkId
9898
import Cardano.Api.Parser.Text qualified as P
9999
import Cardano.Api.Plutus.Internal.Script

cardano-api/src/Cardano/Api/Internal/Utils.hs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@
55

66
-- | Internal utils for the other Api modules
77
module Cardano.Api.Internal.Utils
8-
( (?!)
9-
, (?!.)
10-
, (<<$>>)
8+
( (<<$>>)
119
, (<<<$>>>)
1210
, noInlineMaybeToStrictMaybe
1311
)
1412
where
1513

1614
import Cardano.Ledger.BaseTypes
17-
import Cardano.Ledger.Shelley ()
18-
19-
(?!) :: Maybe a -> e -> Either e a
20-
Nothing ?! e = Left e
21-
Just x ?! _ = Right x
22-
23-
(?!.) :: Either e a -> (e -> e') -> Either e' a
24-
Left e ?!. f = Left (f e)
25-
Right x ?!. _ = Right x
2615

2716
infixl 4 <<$>>
2817

cardano-api/src/Cardano/Api/Monad/Error.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Cardano.Api.Monad.Error
1515
, liftMaybe
1616
, failEither
1717
, failEitherWith
18+
, (?!)
19+
, (?!&)
1820
, module Control.Monad.Except
1921
, module Control.Monad.IO.Class
2022
, module Control.Monad.Trans.Class
@@ -118,6 +120,18 @@ liftMaybe
118120
-> m a
119121
liftMaybe e = maybe (throwError e) pure
120122

123+
-- | Infix 'liftMaybe', with arguments flipped
124+
(?!) :: MonadError e m => Maybe a -> e -> m a
125+
(?!) = flip liftMaybe
126+
127+
infixl 8 ?!
128+
129+
-- | Map over the 'Left' value of 'Either e a'. Infix 'first' with its arguments flipped.
130+
(?!&) :: Either e a -> (e -> e') -> Either e' a
131+
(?!&) = flip first
132+
133+
infixl 8 ?!&
134+
121135
-- | Lift 'Either' to `MonadFail`
122136
failEither
123137
:: MonadFail m

cardano-api/src/Cardano/Api/Serialise/Bech32.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
import Cardano.Api.Error
1919
import Cardano.Api.HasTypeProxy
2020
import Cardano.Api.Internal.Orphans.Misc ()
21-
import Cardano.Api.Internal.Utils
21+
import Cardano.Api.Monad.Error
2222
import Cardano.Api.Pretty
2323
import Cardano.Api.Serialise.Raw
2424

@@ -52,7 +52,7 @@ deserialiseFromBech32
5252
deserialiseFromBech32 bech32Str = do
5353
(prefix, dataPart) <-
5454
Bech32.decodeLenient bech32Str
55-
?!. Bech32DecodingError
55+
?!& Bech32DecodingError
5656

5757
let actualPrefix = Bech32.humanReadablePartToText prefix
5858
permittedPrefixes = bech32PrefixesPermitted (asType @a)
@@ -83,7 +83,7 @@ deserialiseAnyOfFromBech32
8383
deserialiseAnyOfFromBech32 types bech32Str = do
8484
(prefix, dataPart) <-
8585
Bech32.decodeLenient bech32Str
86-
?!. Bech32DecodingError
86+
?!& Bech32DecodingError
8787

8888
let actualPrefix = Bech32.humanReadablePartToText prefix
8989

cardano-api/src/Cardano/Api/Serialise/Cip129.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ where
2020
import Cardano.Api.Governance.Internal.Action.ProposalProcedure
2121
import Cardano.Api.HasTypeProxy
2222
import Cardano.Api.Internal.Orphans (AsType (..))
23-
import Cardano.Api.Internal.Utils
23+
import Cardano.Api.Monad.Error
2424
import Cardano.Api.Serialise.Bech32
2525
import Cardano.Api.Serialise.Raw
2626
import Cardano.Api.Tx.Internal.TxIn
@@ -98,7 +98,7 @@ deserialiseFromBech32Cip129
9898
deserialiseFromBech32Cip129 bech32Str = do
9999
(prefix, dataPart) <-
100100
Bech32.decodeLenient bech32Str
101-
?!. Bech32DecodingError
101+
?!& Bech32DecodingError
102102

103103
let actualPrefix = Bech32.humanReadablePartToText prefix
104104
permittedPrefixes = cip129Bech32PrefixesPermitted (asType @a)
@@ -160,7 +160,7 @@ deserialiseGovActionIdFromBech32Cip129 bech32Str = do
160160
let permittedPrefixes = ["gov_action"]
161161
(prefix, dataPart) <-
162162
Bech32.decodeLenient bech32Str
163-
?!. Bech32DecodingError
163+
?!& Bech32DecodingError
164164
let actualPrefix = Bech32.humanReadablePartToText prefix
165165
guard (actualPrefix `elem` permittedPrefixes)
166166
?! Bech32UnexpectedPrefix actualPrefix (fromList permittedPrefixes)

cardano-api/src/Cardano/Api/Tx/Internal/Body.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ import Cardano.Api.Experimental.Plutus.Internal.Shim.LegacyScripts
248248
import Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements
249249
import Cardano.Api.Governance.Internal.Action.ProposalProcedure
250250
import Cardano.Api.Governance.Internal.Action.VotingProcedure
251-
import Cardano.Api.Internal.Utils
252251
import Cardano.Api.Key.Internal
253252
import Cardano.Api.Ledger.Internal.Reexport qualified as Ledger
253+
import Cardano.Api.Monad.Error
254254
import Cardano.Api.Network.Internal.NetworkId
255255
import Cardano.Api.Plutus.Internal.Script
256256
import Cardano.Api.Plutus.Internal.ScriptData

cardano-api/src/Cardano/Api/Tx/Internal/Output.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ import Cardano.Api.Era.Internal.Eon.Convert
6363
import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra
6464
import Cardano.Api.Error (Error (..), displayError)
6565
import Cardano.Api.Hash
66-
import Cardano.Api.Internal.Utils
6766
import Cardano.Api.Ledger.Internal.Reexport qualified as Ledger
67+
import Cardano.Api.Monad.Error
6868
import Cardano.Api.Parser.Text qualified as P
6969
import Cardano.Api.Plutus.Internal.Script
7070
import Cardano.Api.Plutus.Internal.ScriptData

0 commit comments

Comments
 (0)