module Data.CQRS.GUID
( GUID
, fromByteString
, newGUID
, nil
, toByteString
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Char8 ()
import Data.Typeable (Typeable)
import Data.Word (Word8)
import System.Random (randomRIO)
newtype GUID a = GUID ByteString
deriving (Show, Typeable, Eq)
nil :: GUID a
nil = GUID $ B.pack $ replicate 32 0
randomWord8 :: IO Word8
randomWord8 = fmap fromInteger $ randomRIO (0,255)
newGUID :: IO (GUID a)
newGUID = do
uuid <- sequence $ replicate 32 randomWord8
return $ GUID $ B.pack uuid
toByteString :: GUID a -> ByteString
toByteString (GUID s) = s
fromByteString :: ByteString -> GUID a
fromByteString s = GUID s