Skip to content

Commit e84d098

Browse files
authored
Merge pull request #842 from IntersectMBO/mgalazyn/refactor/remove-partial-instances
Remove partial instances
2 parents 3b17c77 + 68505ed commit e84d098

33 files changed

Lines changed: 532 additions & 552 deletions

File tree

cardano-api/cardano-api.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ library
100100
Cardano.Api.Ledger
101101
Cardano.Api.Ledger.Lens
102102
Cardano.Api.Network
103+
Cardano.Api.Parser.Text
103104
Cardano.Api.Shelley
104105
Cardano.Api.Tx.UTxO
105106

@@ -322,6 +323,7 @@ test-suite cardano-api-test
322323
main-is: cardano-api-test.hs
323324
type: exitcode-stdio-1.0
324325
build-depends:
326+
FailT,
325327
QuickCheck,
326328
aeson >=1.5.6.0,
327329
base16-bytestring,
@@ -420,7 +422,6 @@ test-suite cardano-api-golden
420422
hedgehog >=1.1,
421423
hedgehog-extras ^>=0.7,
422424
microlens,
423-
parsec,
424425
plutus-core ^>=1.45,
425426
plutus-ledger-api,
426427
tasty,

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ import Cardano.Api.Internal.Error
156156
import Cardano.Api.Internal.Script (scriptInEraToRefScript)
157157
import Cardano.Api.Ledger qualified as L
158158
import Cardano.Api.Ledger.Lens qualified as A
159+
import Cardano.Api.Parser.Text qualified as P
159160
import Cardano.Api.Shelley
160161
import Cardano.Api.Shelley qualified as ShelleyApi
161162

@@ -446,16 +447,23 @@ genAssetName :: Gen AssetName
446447
genAssetName =
447448
Gen.frequency
448449
-- mostly from a small number of choices, so we get plenty of repetition
449-
[ (9, Gen.element ["", "a", "b", "c"])
450-
, (1, AssetName <$> Gen.bytes (Range.singleton 32))
451-
, (1, AssetName <$> Gen.bytes (Range.constant 1 31))
450+
[ (9, UnsafeAssetName <$> Gen.element ["", "a", "b", "c"])
451+
, (1, UnsafeAssetName <$> Gen.bytes (Range.singleton 32))
452+
, (1, UnsafeAssetName <$> Gen.bytes (Range.constant 1 31))
452453
]
453454

454455
genPolicyId :: Gen PolicyId
455456
genPolicyId =
456457
Gen.frequency
457458
-- mostly from a small number of choices, so we get plenty of repetition
458-
[ (9, Gen.element [fromString (x : replicate 55 '0') | x <- ['a' .. 'c']])
459+
[
460+
( 9
461+
, Gen.element
462+
[ pid
463+
| x <- ['a' .. 'c']
464+
, pid <- P.runParserFail parsePolicyId (fromString $ x : replicate 55 '0')
465+
]
466+
)
459467
, -- and some from the full range of the type
460468
(1, PolicyId <$> genScriptHash)
461469
]

cardano-api/src/Cardano/Api.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ module Cardano.Api
216216
, StakeAddressReference (..)
217217
, PaymentKey
218218
, PaymentExtendedKey
219+
, parseHexHash
219220

220221
-- ** Addresses in any era
221222
, AddressAny (..)
222-
, lexPlausibleAddressString
223223
, parseAddressAny
224224

225225
-- ** Addresses in specific eras
@@ -376,13 +376,16 @@ module Cardano.Api
376376

377377
-- ** Transaction Ids
378378
, TxId (..)
379+
, parseTxId
379380
, getTxId
380381
, getTxIdByron
381382

382383
-- ** Transaction inputs
383384
, TxIn (TxIn)
385+
, parseTxIn
384386
, TxIns
385387
, TxIx (TxIx)
388+
, parseTxIx
386389
, renderTxIn
387390
, getReferenceInputsSizeForTxIds
388391

@@ -397,7 +400,6 @@ module Cardano.Api
397400
, txOutValueToValue
398401
, lovelaceToTxOutValue
399402
, TxOutDatum (..)
400-
, parseHash
401403

402404
-- ** Other transaction body types
403405
, TxInsCollateral (..)
@@ -643,6 +645,7 @@ module Cardano.Api
643645
, getScriptData
644646
, unsafeHashableScriptData
645647
, ScriptData (..)
648+
, parseScriptDataHash
646649

647650
-- ** Validation
648651
, ScriptDataRangeError (..)
@@ -669,6 +672,7 @@ module Cardano.Api
669672

670673
-- | Making addresses from scripts.
671674
, ScriptHash (..)
675+
, parseScriptHash
672676
, hashScript
673677

674678
-- * Serialisation
@@ -735,6 +739,7 @@ module Cardano.Api
735739
, serialiseToRawBytesHex
736740
, deserialiseFromRawBytesHex
737741
, serialiseToRawBytesHexText
742+
, parseRawBytesHex
738743
, RawBytesHexError (..)
739744
, UsingRawBytes (..)
740745
, UsingRawBytesHex (..)
@@ -995,7 +1000,6 @@ module Cardano.Api
9951000
, fromLedgerTxOuts
9961001
, toLedgerUTxO
9971002
, fromLedgerUTxO
998-
, runParsecParser
9991003
, SlotsInEpoch (..)
10001004
, SlotsToEpochEnd (..)
10011005
, slotToEpoch

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ module Cardano.Api.Internal.Address
3131

3232
-- ** Addresses in any era
3333
, AddressAny (..)
34-
, lexPlausibleAddressString
3534
, parseAddressAny
3635

3736
-- ** Addresses in specific eras
@@ -101,6 +100,7 @@ import Cardano.Api.Internal.Script
101100
import Cardano.Api.Internal.SerialiseBech32
102101
import Cardano.Api.Internal.SerialiseRaw
103102
import Cardano.Api.Internal.Utils
103+
import Cardano.Api.Parser.Text qualified as P
104104

105105
import Cardano.Chain.Common qualified as Byron
106106
import Cardano.Ledger.Address qualified as Shelley
@@ -120,8 +120,6 @@ import Data.Either.Combinators (rightToMaybe)
120120
import Data.Text (Text)
121121
import Data.Text qualified as Text
122122
import Data.Text.Encoding qualified as Text
123-
import Text.Parsec qualified as Parsec
124-
import Text.Parsec.String qualified as Parsec
125123

126124
-- ----------------------------------------------------------------------------
127125
-- Address Serialisation
@@ -248,10 +246,10 @@ instance SerialiseAsRawBytes (Address ShelleyAddr) where
248246
Just (Shelley.Addr nw pc scr) -> Right (ShelleyAddress nw pc scr)
249247

250248
instance SerialiseAsBech32 (Address ShelleyAddr) where
251-
bech32PrefixFor (ShelleyAddress Shelley.Mainnet _ _) = "addr"
252-
bech32PrefixFor (ShelleyAddress Shelley.Testnet _ _) = "addr_test"
249+
bech32PrefixFor (ShelleyAddress Shelley.Mainnet _ _) = unsafeHumanReadablePartFromText "addr"
250+
bech32PrefixFor (ShelleyAddress Shelley.Testnet _ _) = unsafeHumanReadablePartFromText "addr_test"
253251

254-
bech32PrefixesPermitted (AsAddress AsShelleyAddr) = ["addr", "addr_test"]
252+
bech32PrefixesPermitted (AsAddress AsShelleyAddr) = unsafeHumanReadablePartFromText <$> ["addr", "addr_test"]
255253

256254
instance SerialiseAddress (Address ByronAddr) where
257255
serialiseAddress addr@ByronAddress{} =
@@ -381,20 +379,20 @@ instance IsShelleyBasedEra era => FromJSON (AddressInEra era) where
381379
parseJSON =
382380
let sbe = shelleyBasedEra @era
383381
in withText "AddressInEra" $ \txt -> do
384-
addressAny <- runParsecParser parseAddressAny txt
382+
addressAny <- P.runParserFail parseAddressAny txt
385383
pure $ anyAddressInShelleyBasedEra sbe addressAny
386384

387-
parseAddressAny :: SerialiseAddress addr => Parsec.Parser addr
385+
-- | Parser for any address, supports both bech32 and base58 encodings
386+
parseAddressAny :: SerialiseAddress addr => P.Parser addr
388387
parseAddressAny = do
389388
str <- lexPlausibleAddressString
390389
case deserialiseAddress asType str of
391390
Nothing -> fail $ "invalid address: " <> Text.unpack str
392391
Just addr -> pure addr
393-
394-
lexPlausibleAddressString :: Parsec.Parser Text
395-
lexPlausibleAddressString =
396-
Text.pack <$> Parsec.many1 (Parsec.satisfy isPlausibleAddressChar)
397392
where
393+
lexPlausibleAddressString :: P.Parser Text
394+
lexPlausibleAddressString =
395+
Text.pack <$> P.many1 (P.satisfy isPlausibleAddressChar)
398396
-- Covers both base58 and bech32 (with constrained prefixes)
399397
isPlausibleAddressChar c =
400398
isAsciiLower c
@@ -579,10 +577,10 @@ instance SerialiseAsRawBytes StakeAddress where
579577
Just (Shelley.RewardAccount nw sc) -> Right (StakeAddress nw sc)
580578

581579
instance SerialiseAsBech32 StakeAddress where
582-
bech32PrefixFor (StakeAddress Shelley.Mainnet _) = "stake"
583-
bech32PrefixFor (StakeAddress Shelley.Testnet _) = "stake_test"
580+
bech32PrefixFor (StakeAddress Shelley.Mainnet _) = unsafeHumanReadablePartFromText "stake"
581+
bech32PrefixFor (StakeAddress Shelley.Testnet _) = unsafeHumanReadablePartFromText "stake_test"
584582

585-
bech32PrefixesPermitted AsStakeAddress = ["stake", "stake_test"]
583+
bech32PrefixesPermitted AsStakeAddress = unsafeHumanReadablePartFromText <$> ["stake", "stake_test"]
586584

587585
instance SerialiseAddress StakeAddress where
588586
serialiseAddress addr@StakeAddress{} =

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ import Data.Aeson qualified as Aeson
8181
import Data.ByteString qualified as BS
8282
import Data.ByteString.Short qualified as SBS
8383
import Data.Foldable (Foldable (toList))
84-
import Data.String (IsString)
8584
import Data.Text (Text)
8685

8786
{- HLINT ignore "Use lambda" -}
@@ -236,7 +235,6 @@ data BlockHeader
236235
newtype instance Hash BlockHeader = HeaderHash SBS.ShortByteString
237236
deriving (Eq, Ord, Show)
238237
deriving (ToJSON, FromJSON) via UsingRawBytesHex (Hash BlockHeader)
239-
deriving IsString via UsingRawBytesHex (Hash BlockHeader)
240238

241239
instance SerialiseAsRawBytes (Hash BlockHeader) where
242240
serialiseToRawBytes (HeaderHash bs) = SBS.fromShort bs

cardano-api/src/Cardano/Api/Internal/CIP/Cip129.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ class (SerialiseAsRawBytes a, HasTypeProxy a) => Cip129 a where
5454
default cip129Bech32PrefixesPermitted :: AsType a -> [Text]
5555
cip129Bech32PrefixesPermitted = return . Bech32.humanReadablePartToText . cip129Bech32PrefixFor
5656

57-
-- | The human readable part of the Bech32 encoding for the credential. This will
58-
-- error if the prefix is not valid.
59-
unsafeHumanReadablePartFromText :: Text -> Bech32.HumanReadablePart
60-
unsafeHumanReadablePartFromText =
61-
either (error . ("Error while parsing Bech32: " <>) . show) id
62-
. Bech32.humanReadablePartFromText
63-
6457
instance Cip129 (Credential L.ColdCommitteeRole) where
6558
cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText "cc_cold"
6659
cip129Bech32PrefixesPermitted AsColdCommitteeCredential = ["cc_cold"]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
module Cardano.Api.Internal.Error
99
( Error (..)
1010
, throwErrorAsException
11+
, failEitherError
1112
, ErrorAsException (..)
1213
, FileError (..)
1314
, fileIOExceptT
@@ -16,6 +17,7 @@ module Cardano.Api.Internal.Error
1617
where
1718

1819
import Cardano.Api.Internal.Pretty
20+
import Cardano.Api.Internal.Utils
1921

2022
import Control.Exception (Exception (..), IOException, throwIO)
2123
import Control.Monad.Except (throwError)
@@ -36,6 +38,9 @@ instance Error () where
3638
throwErrorAsException :: Error e => e -> IO a
3739
throwErrorAsException e = throwIO (ErrorAsException e)
3840

41+
failEitherError :: MonadFail m => Error e => Either e a -> m a
42+
failEitherError = failEitherWith displayError
43+
3944
data ErrorAsException where
4045
ErrorAsException :: Error e => e -> ErrorAsException
4146

cardano-api/src/Cardano/Api/Internal/Governance/Poll.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Cardano.Api.Internal.Eras
4141
import Cardano.Api.Internal.HasTypeProxy
4242
import Cardano.Api.Internal.Hash
4343
import Cardano.Api.Internal.Keys.Shelley
44+
import Cardano.Api.Internal.Pretty
4445
import Cardano.Api.Internal.Serialise.Cbor
4546
import Cardano.Api.Internal.SerialiseRaw
4647
import Cardano.Api.Internal.SerialiseTextEnvelope
@@ -60,7 +61,6 @@ import Control.Monad (foldM, when)
6061
import Data.Either.Combinators (maybeToRight)
6162
import Data.Function ((&))
6263
import Data.Map.Strict qualified as Map
63-
import Data.String (IsString (..))
6464
import Data.Text (Text)
6565
import Data.Text qualified as Text
6666
import Data.Text.Lazy qualified as Text.Lazy
@@ -185,7 +185,7 @@ instance SerialiseAsCBOR GovernancePoll where
185185
newtype instance Hash GovernancePoll
186186
= GovernancePollHash {unGovernancePollHash :: Hash.Hash HASH GovernancePoll}
187187
deriving stock (Eq, Ord)
188-
deriving (Show, IsString) via UsingRawBytesHex (Hash GovernancePoll)
188+
deriving (Show, Pretty) via UsingRawBytesHex (Hash GovernancePoll)
189189

190190
instance SerialiseAsRawBytes (Hash GovernancePoll) where
191191
serialiseToRawBytes =

cardano-api/src/Cardano/Api/Internal/Keys/Byron.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Cardano.Api.Internal.HasTypeProxy
3232
import Cardano.Api.Internal.Hash
3333
import Cardano.Api.Internal.Keys.Class
3434
import Cardano.Api.Internal.Keys.Shelley
35+
import Cardano.Api.Internal.Pretty
3536
import Cardano.Api.Internal.Serialise.Cbor
3637
import Cardano.Api.Internal.SerialiseRaw
3738
import Cardano.Api.Internal.SerialiseTextEnvelope
@@ -52,7 +53,6 @@ import Control.Monad
5253
import Data.Bifunctor
5354
import Data.ByteString.Lazy qualified as LB
5455
import Data.Either.Combinators
55-
import Data.String (IsString)
5656
import Data.Text (Text)
5757
import Data.Text qualified as Text
5858
import Formatting (build, formatToString)
@@ -95,13 +95,13 @@ instance Key ByronKey where
9595
newtype VerificationKey ByronKey
9696
= ByronVerificationKey Crypto.VerificationKey
9797
deriving stock Eq
98-
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey ByronKey)
98+
deriving (Show, Pretty) via UsingRawBytesHex (VerificationKey ByronKey)
9999
deriving newtype (ToCBOR, FromCBOR)
100100
deriving anyclass SerialiseAsCBOR
101101

102102
newtype SigningKey ByronKey
103103
= ByronSigningKey Crypto.SigningKey
104-
deriving (Show, IsString) via UsingRawBytesHex (SigningKey ByronKey)
104+
deriving (Show, Pretty) via UsingRawBytesHex (SigningKey ByronKey)
105105
deriving newtype (ToCBOR, FromCBOR)
106106
deriving anyclass SerialiseAsCBOR
107107

@@ -147,7 +147,7 @@ instance SerialiseAsRawBytes (SigningKey ByronKey) where
147147

148148
newtype instance Hash ByronKey = ByronKeyHash Crypto.KeyHash
149149
deriving (Eq, Ord)
150-
deriving (Show, IsString) via UsingRawBytesHex (Hash ByronKey)
150+
deriving (Show, Pretty) via UsingRawBytesHex (Hash ByronKey)
151151
deriving (ToCBOR, FromCBOR) via UsingRawBytes (Hash ByronKey)
152152
deriving anyclass SerialiseAsCBOR
153153

@@ -186,13 +186,13 @@ instance Key ByronKeyLegacy where
186186
newtype VerificationKey ByronKeyLegacy
187187
= ByronVerificationKeyLegacy Crypto.VerificationKey
188188
deriving stock Eq
189-
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey ByronKeyLegacy)
189+
deriving (Show, Pretty) via UsingRawBytesHex (VerificationKey ByronKeyLegacy)
190190
deriving newtype (ToCBOR, FromCBOR)
191191
deriving anyclass SerialiseAsCBOR
192192

193193
newtype SigningKey ByronKeyLegacy
194194
= ByronSigningKeyLegacy Crypto.SigningKey
195-
deriving (Show, IsString) via UsingRawBytesHex (SigningKey ByronKeyLegacy)
195+
deriving (Show, Pretty) via UsingRawBytesHex (SigningKey ByronKeyLegacy)
196196
deriving newtype (ToCBOR, FromCBOR)
197197
deriving anyclass SerialiseAsCBOR
198198

@@ -222,7 +222,7 @@ instance HasTextEnvelope (SigningKey ByronKeyLegacy) where
222222

223223
newtype instance Hash ByronKeyLegacy = ByronKeyHashLegacy Crypto.KeyHash
224224
deriving (Eq, Ord)
225-
deriving (Show, IsString) via UsingRawBytesHex (Hash ByronKeyLegacy)
225+
deriving (Show, Pretty) via UsingRawBytesHex (Hash ByronKeyLegacy)
226226
deriving (ToCBOR, FromCBOR) via UsingRawBytes (Hash ByronKeyLegacy)
227227
deriving anyclass SerialiseAsCBOR
228228

0 commit comments

Comments
 (0)