module Data.HexString ( HexString
, hexString
, fromBinary
, toBinary
, fromBytes
, toBytes
, toText ) where
import Data.Word (Word8)
import qualified Data.ByteString.Base16 as BS16 (decode, encode)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Binary as B (Binary, decode, encode)
data HexString =
HexString BS.ByteString
deriving ( Show, Eq, Ord )
hexString :: BS.ByteString -> HexString
hexString bs =
let isValidHex :: Word8 -> Bool
isValidHex c
| (48 <= c) && (c < 58) = True
| (97 <= c) && (c < 103) = True
| otherwise = False
in if BS.all isValidHex bs
then HexString bs
else error ("Not a valid hex string: " ++ show bs)
fromBinary :: B.Binary a => a -> HexString
fromBinary = hexString . BS16.encode . BSL.toStrict . B.encode
toBinary :: B.Binary a => HexString -> a
toBinary (HexString bs) = B.decode . BSL.fromStrict . fst . BS16.decode $ bs
fromBytes :: BS.ByteString -> HexString
fromBytes = hexString . BS16.encode
toBytes :: HexString -> BS.ByteString
toBytes (HexString bs) = (fst . BS16.decode) bs
toText :: HexString -> T.Text
toText (HexString bs) = TE.decodeUtf8 bs