Compare commits
No commits in common. "4856ec516f0733c8aef065166a417a358f12fe62" and "4dc9143dbc83e88c708e0642d5b7354d5a10155e" have entirely different histories.
4856ec516f
...
4dc9143dbc
6 changed files with 29 additions and 53 deletions
|
@ -11,7 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
- CHANGELOG.md
|
- CHANGELOG.md
|
||||||
- Stack integration
|
- Stack integration
|
||||||
- Borsh serialization for `HexString`
|
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
haskell-hexstring
|
haskell-hexstring
|
||||||
=================
|
=================
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/solatis/haskell-hexstring.png?branch=master)](https://travis-ci.org/solatis/haskell-hexstring)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/solatis/haskell-hexstring/badge.svg?branch=master)](https://coveralls.io/r/solatis/haskell-hexstring?branch=master)
|
||||||
|
[![MIT](http://b.repl.ca/v1/license-MIT-blue.png)](http://en.wikipedia.org/wiki/MIT_License)
|
||||||
|
[![Haskell](http://b.repl.ca/v1/language-haskell-lightgrey.png)](http://haskell.org)
|
||||||
|
|
||||||
Fast and safe representation of a hex string
|
Fast and safe representation of a hex string
|
||||||
|
|
|
@ -35,10 +35,7 @@ library
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, base16-bytestring
|
, base16-bytestring
|
||||||
, binary
|
, binary
|
||||||
, borsh >=0.2
|
|
||||||
, bytestring
|
, bytestring
|
||||||
, foreign-rust
|
|
||||||
, generics-sop
|
|
||||||
, text
|
, text
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,6 @@ library:
|
||||||
- bytestring
|
- bytestring
|
||||||
- base16-bytestring
|
- base16-bytestring
|
||||||
- aeson
|
- aeson
|
||||||
- generics-sop
|
|
||||||
- borsh >= 0.2
|
|
||||||
- foreign-rust
|
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
hextring-test:
|
hextring-test:
|
||||||
|
|
|
@ -1,21 +1,11 @@
|
||||||
{-# LANGUAGE DeriveGeneric #-}
|
module Data.HexString ( HexString
|
||||||
{-# LANGUAGE DuplicateRecordFields #-}
|
|
||||||
{-# LANGUAGE DeriveAnyClass #-}
|
|
||||||
{-# LANGUAGE DerivingVia #-}
|
|
||||||
{-# LANGUAGE UndecidableInstances #-}
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
|
|
||||||
module Data.HexString
|
|
||||||
( HexString
|
|
||||||
, hexString
|
, hexString
|
||||||
, fromBinary
|
, fromBinary
|
||||||
, toBinary
|
, toBinary
|
||||||
, fromBytes
|
, fromBytes
|
||||||
, toBytes
|
, toBytes
|
||||||
, toText
|
, toText ) where
|
||||||
) where
|
|
||||||
|
|
||||||
import Codec.Borsh
|
|
||||||
import Control.Applicative (pure)
|
import Control.Applicative (pure)
|
||||||
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
@ -25,29 +15,22 @@ import qualified Data.ByteString as BS
|
||||||
import qualified Data.ByteString.Base16 as BS16 (decodeLenient, encode)
|
import qualified Data.ByteString.Base16 as BS16 (decodeLenient, encode)
|
||||||
import qualified Data.ByteString.Lazy as BSL
|
import qualified Data.ByteString.Lazy as BSL
|
||||||
|
|
||||||
import Data.Structured
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.Encoding as TE
|
import qualified Data.Text.Encoding as TE
|
||||||
|
|
||||||
import qualified Data.Binary as B (Binary, decode, encode)
|
import qualified Data.Binary as B (Binary, decode, encode)
|
||||||
|
|
||||||
import qualified GHC.Generics as GHC
|
|
||||||
import qualified Generics.SOP as SOP
|
|
||||||
|
|
||||||
-- | Represents a Hex string. Guarantees that all characters it contains
|
-- | Represents a Hex string. Guarantees that all characters it contains
|
||||||
-- are valid hex characters.
|
-- are valid hex characters.
|
||||||
data HexString =
|
data HexString =
|
||||||
HexString BS.ByteString
|
HexString BS.ByteString
|
||||||
deriving stock (Prelude.Show, GHC.Generic)
|
deriving ( Show, Eq, Ord )
|
||||||
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
|
|
||||||
deriving anyclass (Data.Structured.Show)
|
|
||||||
deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct HexString
|
|
||||||
|
|
||||||
instance FromJSON HexString where
|
instance FromJSON HexString where
|
||||||
parseJSON = withText "HexString" $ pure . hexString . TE.encodeUtf8
|
parseJSON = withText "HexString" $ pure . hexString . TE.encodeUtf8
|
||||||
|
|
||||||
instance ToJSON HexString where
|
instance ToJSON HexString where
|
||||||
toJSON = Data.Aeson.String . toText
|
toJSON = String . toText
|
||||||
|
|
||||||
-- | Smart constructor which validates that all the text are actually
|
-- | Smart constructor which validates that all the text are actually
|
||||||
-- hexadecimal characters.
|
-- hexadecimal characters.
|
||||||
|
@ -58,9 +41,10 @@ hexString bs =
|
||||||
| (48 <= c) && (c < 58) = True
|
| (48 <= c) && (c < 58) = True
|
||||||
| (97 <= c) && (c < 103) = True
|
| (97 <= c) && (c < 103) = True
|
||||||
| otherwise = False
|
| otherwise = False
|
||||||
|
|
||||||
in if BS.all isValidHex bs
|
in if BS.all isValidHex bs
|
||||||
then HexString bs
|
then HexString bs
|
||||||
else error ("Not a valid hex string: " ++ Prelude.show bs)
|
else error ("Not a valid hex string: " ++ show bs)
|
||||||
|
|
||||||
-- | Converts a 'B.Binary' to a 'HexString' value
|
-- | Converts a 'B.Binary' to a 'HexString' value
|
||||||
fromBinary :: B.Binary a => a -> HexString
|
fromBinary :: B.Binary a => a -> HexString
|
||||||
|
|
|
@ -2,9 +2,3 @@ resolver: lts-21.22
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
- .
|
- .
|
||||||
|
|
||||||
extra-deps:
|
|
||||||
- git: https://github.com/well-typed/borsh.git
|
|
||||||
commit: d2fcfa159e0a844b1ec5e8ed3e232d4b380fa831
|
|
||||||
- git: https://git.vergara.tech/Vergara_Tech/haskell-foreign-rust.git
|
|
||||||
commit: 787c2e813eb3a5d16c375d4b37dfefbd2adcdf05
|
|
||||||
|
|
Loading…
Reference in a new issue