From fd1ddce73c0ad18a2a4509a299c6e93f8c6c383d Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Fri, 9 Feb 2024 13:18:02 -0600 Subject: [PATCH] Implement `Read` --- CHANGELOG.md | 1 + src/Data/HexString.hs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67385b0..86fae43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - CHANGELOG.md - Stack integration - Borsh serialization for `HexString` +- `Read` implementation for `HexString` ### Changed diff --git a/src/Data/HexString.hs b/src/Data/HexString.hs index 467552f..7d90d24 100644 --- a/src/Data/HexString.hs +++ b/src/Data/HexString.hs @@ -32,6 +32,7 @@ import qualified Data.Binary as B (Binary, decode, encode) import qualified GHC.Generics as GHC import qualified Generics.SOP as SOP +import Text.Read -- | Represents a Hex string. Guarantees that all characters it contains -- are valid hex characters. @@ -45,6 +46,9 @@ newtype HexString = HexString instance Prelude.Show HexString where show = T.unpack . toText +instance Read HexString where + readsPrec _ s = [((fromText . T.pack) s, "")] + instance FromJSON HexString where parseJSON = withText "HexString" $ pure . hexString . TE.encodeUtf8