From 94e007a27aaccc6fd1cc4927de3aba6d0f500daf Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 4 Sep 2024 14:42:08 -0500 Subject: [PATCH] feat: add FromHttpApiData instance --- CHANGELOG.md | 6 ++++++ hexstring.cabal | 1 + package.yaml | 3 ++- src/Data/HexString.hs | 8 ++++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86fae43..3b416d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.12.2.0] + +### Added + +- `FromHttpApiData` instance for Servant integration + ## [0.12.0] ### Added diff --git a/hexstring.cabal b/hexstring.cabal index 23eb6cd..09aa9e0 100644 --- a/hexstring.cabal +++ b/hexstring.cabal @@ -39,6 +39,7 @@ library , bytestring , foreign-rust , generics-sop + , servant , text default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index a1a042d..0087176 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: hexstring -version: 0.12.1.0 +version: 0.12.2.0 git: "https://git.vergara.tech/Vergara_Tech/haskell-hexstring" license: MIT author: @@ -30,6 +30,7 @@ library: - generics-sop - borsh >= 0.2 - foreign-rust + - servant tests: hextring-test: diff --git a/src/Data/HexString.hs b/src/Data/HexString.hs index 10a5200..c7cf493 100644 --- a/src/Data/HexString.hs +++ b/src/Data/HexString.hs @@ -35,6 +35,8 @@ import qualified GHC.Generics as GHC import qualified Generics.SOP as SOP import Text.Read +import Servant.API (FromHttpApiData(..)) + -- | Represents a Hex string. Guarantees that all characters it contains -- are valid hex characters. newtype HexString = HexString @@ -56,6 +58,12 @@ instance FromJSON HexString where instance ToJSON HexString where toJSON = Data.Aeson.String . toText +instance FromHttpApiData HexString where + parseUrlPiece s = + case BS16.decode (TE.encodeUtf8 s) of + Right s -> Right $ HexString s + Left e -> Left $ T.pack e + -- | Smart constructor which validates that all the text are actually -- hexadecimal characters. hexString :: BS.ByteString -> HexString