-- |
-- Module      : Data.TypeID.V5
-- License     : MIT
-- Maintainer  : mmzk1526@outlook.com
-- Portability : GHC
--
-- 'Data.TypeID.V7.TypeID' with 'UUID'v5.
--
module Data.TypeID.V5
  (
  -- * Data types
    TypeIDV5
  , getPrefix
  , getUUID
  -- * 'TypeIDV5' generation ('TypeIDV5'-specific)
  , genTypeID
  , decorateTypeID
  -- * 'TypeIDV5' generation (class methods)
  , genID
  , decorate
  -- * Validation ('TypeIDV5'-specific)
  , checkPrefix
  , checkTypeID
  -- * Validation (class methods)
  , checkID
  -- * Encoding & decoding ('TypeIDV5'-specific)
  , toString
  , toText
  , toByteString
  , parseString
  , parseText
  , parseByteString
  , parseStringM
  , parseTextM
  , parseByteStringM
  -- * Encoding & decoding (class methods)
  , id2String
  , id2Text
  , id2ByteString
  , string2ID
  , text2ID
  , byteString2ID
  , string2IDM
  , text2IDM
  , byteString2IDM
  ) where

import           Control.Monad.IO.Class
import           Data.ByteString.Lazy (ByteString)
import           Data.Text (Text)
import           Data.TypeID.Class
import           Data.TypeID.Error
import qualified Data.TypeID.Internal as TID
import           Data.UUID.Types (UUID)
import           Data.UUID.Versions
import           Data.Word

-- | Similar to 'Data.TypeID.V7.TypeID', but uses 'UUID'v5.
type TypeIDV5 = TID.TypeID' 'V5

-- | Generate a new 'TypeIDV5' from a prefix.
--
-- It throws a 'TypeIDError' if the prefix does not match the specification,
-- namely if it's longer than 63 characters or if it contains characters other
-- than lowercase latin letters.
genTypeID :: MonadIO m => Text -> UUID -> [Word8] -> m TypeIDV5
genTypeID :: forall (m :: * -> *).
MonadIO m =>
Text -> UUID -> [Word8] -> m TypeIDV5
genTypeID = Text -> UUID -> [Word8] -> m TypeIDV5
forall (m :: * -> *).
MonadIO m =>
Text -> UUID -> [Word8] -> m TypeIDV5
TID.genTypeIDV5
{-# INLINE genTypeID #-}

-- | Obtain a 'TypeIDV5' from a prefix and a 'UUID'.
decorateTypeID :: Text -> UUID -> Either TypeIDError TypeIDV5
decorateTypeID :: Text -> UUID -> Either TypeIDError TypeIDV5
decorateTypeID = Text -> UUID -> Either TypeIDError TypeIDV5
forall (version :: UUIDVersion).
Text -> UUID -> Either TypeIDError (TypeID' version)
TID.decorateTypeID
{-# INLINE decorateTypeID #-}

-- | Check if the given prefix is a valid 'TypeIDV5' prefix.
checkPrefix :: Text -> Maybe TypeIDError
checkPrefix :: Text -> Maybe TypeIDError
checkPrefix = Text -> Maybe TypeIDError
TID.checkPrefix
{-# INLINE checkPrefix #-}

-- | Check if the prefix is valid and the suffix 'UUID' has the correct v5
-- version and variant.
checkTypeID :: TypeIDV5 -> Maybe TypeIDError
checkTypeID :: TypeIDV5 -> Maybe TypeIDError
checkTypeID = TypeIDV5 -> Maybe TypeIDError
TID.checkTypeIDV5
{-# INLINE checkTypeID #-}

-- | Pretty-print a 'TypeIDV5'. It is 'id2String' with concrete type.
toString :: TypeIDV5 -> String
toString :: TypeIDV5 -> String
toString = TypeIDV5 -> String
forall (version :: UUIDVersion). TypeID' version -> String
TID.toString
{-# INLINE toString #-}

-- | Pretty-print a 'TypeIDV5' to strict 'Text'. It is 'id2Text' with concrete
-- type.
toText :: TypeIDV5 -> Text
toText :: TypeIDV5 -> Text
toText = TypeIDV5 -> Text
forall (version :: UUIDVersion). TypeID' version -> Text
TID.toText
{-# INLINE toText #-}

-- | Pretty-print a 'TypeIDV5' to lazy 'ByteString'. It is 'id2ByteString'
-- with concrete type.
toByteString :: TypeIDV5 -> ByteString
toByteString :: TypeIDV5 -> ByteString
toByteString = TypeIDV5 -> ByteString
forall (version :: UUIDVersion). TypeID' version -> ByteString
TID.toByteString
{-# INLINE toByteString #-}

-- | Parse a 'TypeIDV5' from its 'String' representation. It is 'string2ID' with
-- concrete type.
parseString :: String -> Either TypeIDError TypeIDV5
parseString :: String -> Either TypeIDError TypeIDV5
parseString = String -> Either TypeIDError TypeIDV5
forall (version :: UUIDVersion).
String -> Either TypeIDError (TypeID' version)
TID.parseString
{-# INLINE parseString #-}

-- | Parse a 'TypeIDV5' from its string representation as a strict 'Text'. It
-- is 'text2ID' with concrete type.
parseText :: Text -> Either TypeIDError TypeIDV5
parseText :: Text -> Either TypeIDError TypeIDV5
parseText = Text -> Either TypeIDError TypeIDV5
forall (version :: UUIDVersion).
Text -> Either TypeIDError (TypeID' version)
TID.parseText
{-# INLINE parseText #-}

-- | Parse a 'TypeIDV5' from its string representation as a lazy 'ByteString'.
-- It is 'byteString2ID' with concrete type.
parseByteString :: ByteString -> Either TypeIDError TypeIDV5
parseByteString :: ByteString -> Either TypeIDError TypeIDV5
parseByteString = ByteString -> Either TypeIDError TypeIDV5
forall (version :: UUIDVersion).
ByteString -> Either TypeIDError (TypeID' version)
TID.parseByteString
{-# INLINE parseByteString #-}

-- | Parse a 'TypeIDV5' from its 'String' representation, throwing an error when
-- the parsing fails. It is 'string2IDM' with concrete type.
parseStringM :: MonadIO m => String -> m TypeIDV5
parseStringM :: forall (m :: * -> *). MonadIO m => String -> m TypeIDV5
parseStringM = String -> m TypeIDV5
forall (m :: * -> *) (version :: UUIDVersion).
MonadIO m =>
String -> m (TypeID' version)
TID.parseStringM
{-# INLINE parseStringM #-}

-- | Parse a 'TypeIDV5' from its string representation as a strict 'Text',
-- throwing an error when the parsing fails. It is 'text2IDM' with concrete
-- type.
parseTextM :: MonadIO m => Text -> m TypeIDV5
parseTextM :: forall (m :: * -> *). MonadIO m => Text -> m TypeIDV5
parseTextM = Text -> m TypeIDV5
forall (m :: * -> *) (version :: UUIDVersion).
MonadIO m =>
Text -> m (TypeID' version)
TID.parseTextM
{-# INLINE parseTextM #-}

-- | Parse a 'TypeIDV5' from its string representation as a lazy 'ByteString',
-- throwing an error when the parsing fails. It is 'byteString2IDM' with
-- concrete type.
parseByteStringM :: MonadIO m => ByteString -> m TypeIDV5
parseByteStringM :: forall (m :: * -> *). MonadIO m => ByteString -> m TypeIDV5
parseByteStringM = ByteString -> m TypeIDV5
forall (m :: * -> *) (version :: UUIDVersion).
MonadIO m =>
ByteString -> m (TypeID' version)
TID.parseByteStringM
{-# INLINE parseByteStringM #-}