From 4eb692bb8c5abdbdea233cd8ca5f2734c6955d7b Mon Sep 17 00:00:00 2001 From: mmzk1526 Date: Sun, 23 Jun 2024 23:36:34 +0100 Subject: [PATCH] Change TypeIDError to use String instead of Text in its fields We will use the promoted constructors in the type level, and I do not think it is possible to convert a type level Text into a Symbol --- src/Data/KindID/Internal.hs | 12 ++++++------ src/Data/TypeID/Error.hs | 14 ++++++-------- src/Data/TypeID/Internal.hs | 10 ++++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Data/KindID/Internal.hs b/src/Data/KindID/Internal.hs index dfdbeba..78ded16 100644 --- a/src/Data/KindID/Internal.hs +++ b/src/Data/KindID/Internal.hs @@ -458,8 +458,8 @@ parseString str = do tid <- TID.parseString str case fromTypeID tid of Nothing -> Left $ TypeIDErrorPrefixMismatch - (T.pack (symbolVal (Proxy @(PrefixSymbol prefix)))) - (getPrefix tid) + (symbolVal (Proxy @(PrefixSymbol prefix))) + (T.unpack $ getPrefix tid) Just kid -> pure kid {-# INLINE parseString #-} @@ -472,8 +472,8 @@ parseText str = do tid <- TID.parseText str case fromTypeID tid of Nothing -> Left $ TypeIDErrorPrefixMismatch - (T.pack (symbolVal (Proxy @(PrefixSymbol prefix)))) - (getPrefix tid) + (symbolVal (Proxy @(PrefixSymbol prefix))) + (T.unpack $ getPrefix tid) Just kid -> pure kid {-# INLINE parseText #-} @@ -486,8 +486,8 @@ parseByteString str = do tid <- TID.parseByteString str case fromTypeID tid of Nothing -> Left $ TypeIDErrorPrefixMismatch - (T.pack (symbolVal (Proxy @(PrefixSymbol prefix)))) - (getPrefix tid) + (symbolVal (Proxy @(PrefixSymbol prefix))) + (T.unpack $ getPrefix tid) Just kid -> pure kid {-# INLINE parseByteString #-} diff --git a/src/Data/TypeID/Error.hs b/src/Data/TypeID/Error.hs index 4718aea..f2af828 100644 --- a/src/Data/TypeID/Error.hs +++ b/src/Data/TypeID/Error.hs @@ -13,8 +13,6 @@ module Data.TypeID.Error ) where import Control.Exception -import Data.Text (Text) -import qualified Data.Text as T -- | Errors from parsing TypeIDs. -- @@ -22,18 +20,18 @@ import qualified Data.Text as T -- exact output format may differ across library versions. data TypeIDError = -- | The prefix is longer than 63 characters. - TypeIDErrorPrefixTooLong Text + TypeIDErrorPrefixTooLong String -- | The ID contains an extra underscore separator. | TypeIDExtraSeparator -- | The ID starts with an underscore separator. - | TypeIDStartWithUnderscore Text + | TypeIDStartWithUnderscore String -- | The ID ends with an underscore separator. - | TypeIDEndWithUnderscore Text + | TypeIDEndWithUnderscore String -- | The prefix contains an invalid character, namely not lowercase Latin. - | TypeIDErrorPrefixInvalidChar Text Char + | TypeIDErrorPrefixInvalidChar String Char -- | From a 'Data.KindID.V7KindID' conversion. The prefix doesn't match with -- the expected. - | TypeIDErrorPrefixMismatch Text Text + | TypeIDErrorPrefixMismatch String String -- | The 'Data.UUID.Types.Internal.UUID' suffix has errors. | TypeIDErrorUUIDError deriving (Eq, Ord) @@ -42,7 +40,7 @@ instance Show TypeIDError where show :: TypeIDError -> String show (TypeIDErrorPrefixTooLong txt) = concat [ "The prefix ", show txt - , " with ", show (T.length txt), " characters is too long!" ] + , " with ", show (length txt), " characters is too long!" ] show TypeIDExtraSeparator = "The underscore separator should not be present if the prefix is empty!" show (TypeIDStartWithUnderscore txt) diff --git a/src/Data/TypeID/Internal.hs b/src/Data/TypeID/Internal.hs index e472a3f..57594bb 100644 --- a/src/Data/TypeID/Internal.hs +++ b/src/Data/TypeID/Internal.hs @@ -543,16 +543,18 @@ parseByteStringM = byteString2IDM -- | Check if the given prefix is a valid 'TypeID'' prefix. checkPrefix :: Text -> Maybe TypeIDError checkPrefix prefix - | T.length prefix > 63 = Just $ TypeIDErrorPrefixTooLong prefix + | T.length prefix > 63 = Just $ TypeIDErrorPrefixTooLong prefixStr | T.null prefix = Nothing - | T.head prefix == '_' = Just $ TypeIDStartWithUnderscore prefix - | T.last prefix == '_' = Just $ TypeIDEndWithUnderscore prefix + | T.head prefix == '_' = Just $ TypeIDStartWithUnderscore prefixStr + | T.last prefix == '_' = Just $ TypeIDEndWithUnderscore prefixStr | otherwise = case T.uncons ( T.dropWhile ( liftM2 (||) (== '_') $ liftM2 (&&) isLower isAscii) prefix) of Nothing -> Nothing - Just (c, _) -> Just $ TypeIDErrorPrefixInvalidChar prefix c + Just (c, _) -> Just $ TypeIDErrorPrefixInvalidChar prefixStr c + where + prefixStr = T.unpack prefix {-# INLINE checkPrefix #-} -- | Check if the prefix is valid and the suffix 'UUID' has the correct v7