-- |
-- Module      : Data.TypeID.V7
-- License     : MIT
-- Maintainer  : mmzk1526@outlook.com
-- Portability : GHC
--
-- An implementation of the 'TypeID' specification:
-- https://github.com/jetpack-io/typeid.
--
module Data.TypeID.V7
  (
  -- * Data types
    TypeID
  , TypeIDV7
  , getPrefix
  , getUUID
  , getTime
  -- * 'TypeID' generation ('TypeID'-specific)
  , genTypeID
  , genTypeID'
  , genTypeIDs
  , decorateTypeID
  -- * 'TypeID' generation (class methods)
  , genID
  , genID'
  , genIDs
  , decorate
  -- * Validation ('TypeID'-specific)
  , checkPrefix
  , checkTypeID
  , checkTypeIDWithEnv
  -- * Validation (class methods)
  , checkID
  , checkIDWithEnv
  -- * Encoding & decoding ('TypeID'-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

-- | A type alias for the default 'TypeID' implementation with 'UUID'v7.
type TypeID = TID.TypeID' 'V7

-- | A type alias for 'TypeID'.
type TypeIDV7 = TypeID

-- | Generate a new 'TypeID' 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 -> m TypeID
genTypeID :: forall (m :: * -> *). MonadIO m => Text -> m TypeID
genTypeID = Text -> m TypeID
forall (m :: * -> *). MonadIO m => Text -> m TypeID
TID.genTypeID
{-# INLINE genTypeID #-}

-- | Generate a new 'TypeID' from a prefix based on stateless 'UUID'v7.
--
-- See the documentation of 'V7.genUUID'' for more information.
genTypeID' :: MonadIO m => Text -> m TypeID
genTypeID' :: forall (m :: * -> *). MonadIO m => Text -> m TypeID
genTypeID' = Text -> m TypeID
forall (m :: * -> *). MonadIO m => Text -> m TypeID
TID.genTypeID'
{-# INLINE genTypeID' #-}

-- | Generate a list of 'TypeID's from a prefix.
--
-- It tries its best to generate 'TypeID's at the same timestamp, but it may not
-- be possible if we are asking too many 'UUID's at the same time.
--
-- It is guaranteed that the first 32768 'TypeID's are generated at the same
-- timestamp.
genTypeIDs :: MonadIO m => Text -> Word16 -> m [TypeID]
genTypeIDs :: forall (m :: * -> *). MonadIO m => Text -> Word16 -> m [TypeID]
genTypeIDs = Text -> Word16 -> m [TypeID]
forall (m :: * -> *). MonadIO m => Text -> Word16 -> m [TypeID]
TID.genTypeIDs
{-# INLINE genTypeIDs #-}

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

-- | Check if the given prefix is a valid 'TypeID' 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 v7
-- version and variant.
checkTypeID :: TypeID -> Maybe TypeIDError
checkTypeID :: TypeID -> Maybe TypeIDError
checkTypeID = TypeID -> Maybe TypeIDError
TID.checkTypeID
{-# INLINE checkTypeID #-}

-- | Similar to 'checkTypeID', but also checks if the suffix 'UUID' is
-- generated in the past.
checkTypeIDWithEnv :: MonadIO m => TypeID -> m (Maybe TypeIDError)
checkTypeIDWithEnv :: forall (m :: * -> *). MonadIO m => TypeID -> m (Maybe TypeIDError)
checkTypeIDWithEnv = TypeID -> m (Maybe TypeIDError)
forall (m :: * -> *). MonadIO m => TypeID -> m (Maybe TypeIDError)
TID.checkTypeIDWithEnv
{-# INLINE checkTypeIDWithEnv #-}

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

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

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

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

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

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

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

-- | Parse a 'TypeID' 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 TypeID
parseTextM :: forall (m :: * -> *). MonadIO m => Text -> m TypeID
parseTextM = Text -> m TypeID
forall (m :: * -> *) (version :: UUIDVersion).
MonadIO m =>
Text -> m (TypeID' version)
TID.parseTextM
{-# INLINE parseTextM #-}

-- | Parse a 'TypeID' 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 TypeID
parseByteStringM :: forall (m :: * -> *). MonadIO m => ByteString -> m TypeID
parseByteStringM = ByteString -> m TypeID
forall (m :: * -> *) (version :: UUIDVersion).
MonadIO m =>
ByteString -> m (TypeID' version)
TID.parseByteStringM
{-# INLINE parseByteStringM #-}