module Data.CQRS.GUID
( GUID
, fromByteString
, base64Decode
, base64Encode
, hexDecode
, hexEncode
, newGUID
, toByteString
) where
import Control.DeepSeq (NFData(..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Base64 as B64
import Data.ByteString.Char8 ()
import Data.Data (Data)
import Data.DeriveTH (derive, makeNFData)
import Data.Typeable (Typeable)
import Data.Word (Word8)
import System.Random (randomRIO)
import Data.CQRS.Serializable
newtype GUID = GUID ByteString
deriving (Typeable, Eq, Ord, Data)
randomWord8 :: IO Word8
randomWord8 = fmap fromInteger $ randomRIO (0,255)
newGUID :: IO GUID
newGUID = do
uuid <- sequence $ replicate 16 randomWord8
return $ GUID $ B.pack uuid
instance Serializable GUID where
serialize = toByteString
deserialize = Just . GUID
hexEncode :: GUID -> ByteString
hexEncode (GUID s) = B16.encode s
base64Encode :: GUID -> ByteString
base64Encode (GUID s) = B64.encode s
instance Show GUID where
show = show . base64Encode
hexDecode :: ByteString -> Maybe GUID
hexDecode s =
case B16.decode s of
(a,b) | B.length b == 0 -> Just $ GUID a
_ -> Nothing
base64Decode :: ByteString -> Maybe GUID
base64Decode s =
case B64.decode s of
Right a -> Just $ GUID a
Left _ -> Nothing
fromByteString :: ByteString -> GUID
fromByteString = GUID
toByteString :: GUID -> ByteString
toByteString (GUID g) = g
$(derive makeNFData ''GUID)