Skip to content

Commit ed41825

Browse files
committed
Add safe conversion to HexString
1 parent 5c065f6 commit ed41825

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/Data/HexString.hs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,51 @@ data HexString =
2727
deriving ( Show, Eq, Ord )
2828

2929
instance FromJSON HexString where
30-
parseJSON = withText "HexString" $ pure . hexString . TE.encodeUtf8
30+
parseJSON = withText "HexString" $ toParser . TE.encodeUtf8
31+
where
32+
toParser input = case hexString' input of
33+
(Just value) -> pure value
34+
Nothing -> fail ("Not a valid hex string: " ++ show input)
3135

3236
instance ToJSON HexString where
3337
toJSON = String . toText
3438

3539
-- | Smart constructor which validates that all the text are actually
3640
-- hexadecimal characters.
37-
hexString :: BS.ByteString -> HexString
38-
hexString bs =
41+
hexString' :: BS.ByteString -> Maybe HexString
42+
hexString' bs =
3943
let isValidHex :: Word8 -> Bool
4044
isValidHex c
4145
| (48 <= c) && (c < 58) = True
4246
| (97 <= c) && (c < 103) = True
4347
| otherwise = False
44-
4548
in if BS.all isValidHex bs
46-
then HexString bs
47-
else error ("Not a valid hex string: " ++ show bs)
49+
then Just (HexString bs)
50+
else Nothing
51+
52+
hexString :: BS.ByteString -> HexString
53+
hexString bs = case hexString' bs of
54+
Just hex -> hex
55+
Nothing -> error ("Not a valid hex string: " ++ show bs)
56+
57+
-- | Converts a 'B.Binary' to a 'Maybe HexString' value
58+
fromBinary' :: B.Binary a => a -> Maybe HexString
59+
fromBinary' = hexString' . BS16.encode . BSL.toStrict . B.encode
4860

4961
-- | Converts a 'B.Binary' to a 'HexString' value
50-
fromBinary :: B.Binary a => a -> HexString
62+
fromBinary :: B.Binary a => a -> HexString
5163
fromBinary = hexString . BS16.encode . BSL.toStrict . B.encode
5264

5365
-- | Converts a 'HexString' to a 'B.Binary' value
5466
toBinary :: B.Binary a => HexString -> a
5567
toBinary (HexString bs) = B.decode . BSL.fromStrict . fst . BS16.decode $ bs
5668

69+
-- | Reads a 'BS.ByteString' as raw bytes and converts to hex representation. We
70+
-- cannot use the instance Binary of 'BS.ByteString' because it provides
71+
-- a leading length, which is not what we want when dealing with raw bytes.
72+
fromBytes' :: BS.ByteString -> Maybe HexString
73+
fromBytes' = hexString' . BS16.encode
74+
5775
-- | Reads a 'BS.ByteString' as raw bytes and converts to hex representation. We
5876
-- cannot use the instance Binary of 'BS.ByteString' because it provides
5977
-- a leading length, which is not what we want when dealing with raw bytes.

0 commit comments

Comments
 (0)