diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2025-04-05 19:52:39 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2025-04-05 21:47:56 +0200 |
commit | 768d33667faca870f10ac6e39f3400ea9810fb7f (patch) | |
tree | 0af9d37fdff5410c9089f5fd397f991e3e701e8b /src | |
parent | 752e28e05ecc6968a66be67819ba76a72aa53724 (diff) |
Use UUID type from uuid-types package
Diffstat (limited to 'src')
-rw-r--r-- | src/Erebos/UUID.hs | 62 |
1 files changed, 4 insertions, 58 deletions
diff --git a/src/Erebos/UUID.hs b/src/Erebos/UUID.hs index 353cc0e..128d450 100644 --- a/src/Erebos/UUID.hs +++ b/src/Erebos/UUID.hs @@ -6,67 +6,13 @@ module Erebos.UUID ( nextRandom, ) where -import Control.Monad - import Crypto.Random.Entropy import Data.Bits -import Data.ByteString (ByteString) import Data.ByteString qualified as BS -import Data.ByteString.Char8 qualified as BSC -import Data.Text (Text) -import Data.Text.Encoding - -import Erebos.Util - - -newtype UUID = UUID ByteString - deriving (Eq, Ord) - -instance Show UUID where - show = toString - - -toString :: UUID -> String -toString = BSC.unpack . toASCIIBytes - -fromString :: String -> Maybe UUID -fromString = fromASCIIBytes . BSC.pack - -toText :: UUID -> Text -toText = decodeUtf8 . toASCIIBytes - -fromText :: Text -> Maybe UUID -fromText = fromASCIIBytes . encodeUtf8 - -toASCIIBytes :: UUID -> ByteString -toASCIIBytes (UUID uuid) = BS.concat - [ showHex $ BS.take 4 $ uuid - , BSC.singleton '-' - , showHex $ BS.take 2 $ BS.drop 4 uuid - , BSC.singleton '-' - , showHex $ BS.take 2 $ BS.drop 6 uuid - , BSC.singleton '-' - , showHex $ BS.take 2 $ BS.drop 8 uuid - , BSC.singleton '-' - , showHex $ BS.drop 10 uuid - ] - -fromASCIIBytes :: ByteString -> Maybe UUID -fromASCIIBytes bs = do - guard $ BS.length bs == 36 - guard $ BSC.index bs 8 == '-' - guard $ BSC.index bs 13 == '-' - guard $ BSC.index bs 18 == '-' - guard $ BSC.index bs 23 == '-' - UUID . BS.concat <$> sequence - [ readHex $ BS.take 8 $ bs - , readHex $ BS.take 4 $ BS.drop 9 bs - , readHex $ BS.take 4 $ BS.drop 14 bs - , readHex $ BS.take 4 $ BS.drop 19 bs - , readHex $ BS.drop 24 bs - ] - +import Data.ByteString.Lazy qualified as BSL +import Data.Maybe +import Data.UUID.Types nextRandom :: IO UUID nextRandom = do @@ -75,4 +21,4 @@ nextRandom = do let version = 4 b6' = b6 .&. 0x0f .|. (version `shiftL` 4) b8' = b8 .&. 0x3f .|. 0x80 - return $ UUID $ BS.pack [ b0, b1, b2, b3, b4, b5, b6', b7, b8', b9, ba, bb, bc, bd, be, bf ] + return $ fromJust $ fromByteString $ BSL.pack [ b0, b1, b2, b3, b4, b5, b6', b7, b8', b9, ba, bb, bc, bd, be, bf ] |