module Cometa.TransactionId
( TransactionId,
fromBytes,
toBytes,
fromHex,
toHex,
)
where
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
newtype TransactionId = TransactionId ByteString deriving (TransactionId -> TransactionId -> Bool
(TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool) -> Eq TransactionId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TransactionId -> TransactionId -> Bool
== :: TransactionId -> TransactionId -> Bool
$c/= :: TransactionId -> TransactionId -> Bool
/= :: TransactionId -> TransactionId -> Bool
Eq, Eq TransactionId
Eq TransactionId =>
(TransactionId -> TransactionId -> Ordering)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> TransactionId)
-> (TransactionId -> TransactionId -> TransactionId)
-> Ord TransactionId
TransactionId -> TransactionId -> Bool
TransactionId -> TransactionId -> Ordering
TransactionId -> TransactionId -> TransactionId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TransactionId -> TransactionId -> Ordering
compare :: TransactionId -> TransactionId -> Ordering
$c< :: TransactionId -> TransactionId -> Bool
< :: TransactionId -> TransactionId -> Bool
$c<= :: TransactionId -> TransactionId -> Bool
<= :: TransactionId -> TransactionId -> Bool
$c> :: TransactionId -> TransactionId -> Bool
> :: TransactionId -> TransactionId -> Bool
$c>= :: TransactionId -> TransactionId -> Bool
>= :: TransactionId -> TransactionId -> Bool
$cmax :: TransactionId -> TransactionId -> TransactionId
max :: TransactionId -> TransactionId -> TransactionId
$cmin :: TransactionId -> TransactionId -> TransactionId
min :: TransactionId -> TransactionId -> TransactionId
Ord, Int -> TransactionId -> ShowS
[TransactionId] -> ShowS
TransactionId -> String
(Int -> TransactionId -> ShowS)
-> (TransactionId -> String)
-> ([TransactionId] -> ShowS)
-> Show TransactionId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TransactionId -> ShowS
showsPrec :: Int -> TransactionId -> ShowS
$cshow :: TransactionId -> String
show :: TransactionId -> String
$cshowList :: [TransactionId] -> ShowS
showList :: [TransactionId] -> ShowS
Show)
fromBytes :: ByteString -> Either Error TransactionId
fromBytes :: ByteString -> Either Error TransactionId
fromBytes ByteString
bytes
| ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = TransactionId -> Either Error TransactionId
forall a b. b -> Either a b
Right (ByteString -> TransactionId
TransactionId ByteString
bytes)
| Bool
otherwise = Error -> Either Error TransactionId
forall a b. a -> Either a b
Left (Int -> Error
InvalidTransactionIdLength (ByteString -> Int
ByteString.length ByteString
bytes))
toBytes :: TransactionId -> ByteString
toBytes :: TransactionId -> ByteString
toBytes (TransactionId ByteString
bytes) = ByteString
bytes
fromHex :: Text -> Either Error TransactionId
fromHex :: Text -> Either Error TransactionId
fromHex Text
text = Either Error TransactionId
-> (ByteString -> Either Error TransactionId)
-> Maybe ByteString
-> Either Error TransactionId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error TransactionId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error TransactionId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)
toHex :: TransactionId -> Text
toHex :: TransactionId -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (TransactionId -> ByteString) -> TransactionId -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TransactionId -> ByteString
toBytes