Skip to content

Commit 75648be

Browse files
f-flehins
authored andcommitted
Add custom Show instance for the Mismatch type, to show the Relation between the values
1 parent 512559b commit 75648be

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ import NoThunks.Class (NoThunks (..))
185185
import Numeric.Natural (Natural)
186186
import Quiet (Quiet (Quiet))
187187
import System.Random.Stateful (Random, Uniform (..), UniformRange (..))
188+
import Type.Reflection (typeRep)
188189
#if MIN_VERSION_random(1,3,0)
189190
import System.Random.Stateful (isInRangeOrd)
190191
#endif
@@ -343,11 +344,21 @@ fromRatioBoundedRatio ratio
343344
lowerBound = minBound :: BoundedRatio b a
344345
upperBound = maxBound :: BoundedRatio b a
345346

346-
instance (ToCBOR a, Integral a, Bounded a, Typeable b) => ToCBOR (BoundedRatio b a) where
347+
instance
348+
(ToCBOR a, Integral a, Bounded a, Typeable b, Typeable (BoundedRatio b a)) =>
349+
ToCBOR (BoundedRatio b a)
350+
where
347351
toCBOR (BoundedRatio u) = Plain.encodeRatioWithTag toCBOR u
348352

349353
instance
350-
(FromCBOR a, Bounded (BoundedRatio b a), Bounded a, Integral a, Typeable b, Show a) =>
354+
( FromCBOR a
355+
, Bounded (BoundedRatio b a)
356+
, Bounded a
357+
, Integral a
358+
, Typeable b
359+
, Show a
360+
, Typeable (BoundedRatio b a)
361+
) =>
351362
FromCBOR (BoundedRatio b a)
352363
where
353364
fromCBOR = do
@@ -359,7 +370,9 @@ instance
359370

360371
instance (ToCBOR (BoundedRatio b a), Typeable b, Typeable a) => EncCBOR (BoundedRatio b a)
361372

362-
instance (FromCBOR (BoundedRatio b a), Typeable b, Typeable a) => DecCBOR (BoundedRatio b a)
373+
instance
374+
(FromCBOR (BoundedRatio b a), Typeable b, Typeable a, Typeable (BoundedRatio b a)) =>
375+
DecCBOR (BoundedRatio b a)
363376

364377
instance Bounded (BoundedRatio b Word64) => ToJSON (BoundedRatio b Word64) where
365378
toJSON :: BoundedRatio b Word64 -> Value
@@ -747,7 +760,14 @@ data Mismatch (r :: Relation) a = Mismatch
747760
{ mismatchSupplied :: !a
748761
, mismatchExpected :: !a
749762
}
750-
deriving (Eq, Ord, Show, Generic, NFData, ToJSON, FromJSON, NoThunks)
763+
deriving (Eq, Ord, Generic, NFData, ToJSON, FromJSON, NoThunks)
764+
765+
instance (Typeable r, Show a) => Show (Mismatch (r :: Relation) a) where
766+
show (Mismatch {mismatchSupplied, mismatchExpected}) =
767+
let headerLine = "Mismatch (" <> show (typeRep @r) <> ")"
768+
suppliedLine = "supplied: " <> show mismatchSupplied
769+
expectedLine = "expected: " <> show mismatchExpected
770+
in headerLine <> " {" <> suppliedLine <> ", " <> expectedLine <> "}"
751771

752772
-- | Convert a `Mismatch` to a tuple that has "supplied" and "expected" swapped places
753773
swapMismatch :: Mismatch r a -> (a, a)

0 commit comments

Comments
 (0)