Skip to content

Commit

Permalink
Change TypeIDError to use String instead of Text in its fields
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MMZK1526 committed Jun 23, 2024
1 parent 299b129 commit 4eb692b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/Data/KindID/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 #-}

Expand All @@ -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 #-}

Expand All @@ -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 #-}

Expand Down
14 changes: 6 additions & 8 deletions src/Data/TypeID/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,25 @@ module Data.TypeID.Error
) where

import Control.Exception
import Data.Text (Text)
import qualified Data.Text as T

-- | Errors from parsing TypeIDs.
--
-- Should NOT rely on \"grepping\" the output produced by 'show' since the
-- 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)
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/Data/TypeID/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4eb692b

Please sign in to comment.