Skip to content

Commit

Permalink
Show the prefix itself inside error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MMZK1526 committed May 30, 2024
1 parent d12905f commit eb11ff6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
17 changes: 11 additions & 6 deletions src/Data/KindID/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ type ValidPrefix prefix = ( KnownSymbol prefix

-- | Contains a custom error message if the prefix 'Symbol' is too long.
type family LengthLT64C (prefix :: Symbol) :: Constraint where
LengthLT64C s =
If (Compare (LengthSymbol s) 64 == 'LT) (() :: Constraint)
( TypeError ( Text "Prefix with "
LengthLT64C s
= If (Compare (LengthSymbol s) 64 == 'LT) (() :: Constraint)
( TypeError ( Text "The prefix "

Check warning on line 80 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 80 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 80 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.
:<>: ShowType s

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 81 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.
:<>: Text " with "

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 82 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.
:<>: ShowType (LengthSymbol s)

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 83 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘ShowType’.
:<>: Text " characters is too long!" ) )

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘:<>:’.

Check warning on line 84 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

-- | Contains a custom error message if the prefix 'Symbol' is not lowercase +
-- underscore or it starts or ends with underscores.
type family IsLUSymbolC (prefix :: Symbol) :: Constraint where
IsLUSymbolC s =
If (IsLUSymbol s) (() :: Constraint)
(TypeError (Text "Prefix is not valid!"))
IsLUSymbolC s
= If (IsLUSymbol s) (() :: Constraint)
( TypeError ( Text "The prefix "

Check warning on line 91 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 91 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

Check warning on line 91 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.
:<>: ShowType s
:<>: Text " is not valid!" ) )

-- | The length of a 'Symbol' as a 'Nat'.
type family LengthSymbol (prefix :: Symbol) :: Nat where
Expand Down Expand Up @@ -123,6 +127,7 @@ type family ILUSUH2 (uncons :: Maybe (Char, Symbol)) :: Bool where
ILUSUH2 ('Just '( c, s )) = (IsLowerChar c || IsUnderscore c)
&& ILUSUH2 (UnconsSymbol s)


--------------------------------------------------------------------------------
-- Deprecated
--------------------------------------------------------------------------------
Expand Down
29 changes: 16 additions & 13 deletions src/Data/TypeID/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ module Data.TypeID.Error

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

-- | Errors from parsing TypeIDs.
data TypeIDError
= -- | The prefix longer than 63 characters.
TypeIDErrorPrefixTooLong Int
= -- | The prefix is longer than 63 characters.
TypeIDErrorPrefixTooLong Text
-- | The ID contains an extra underscore separator.
| TypeIDExtraSeparator
-- | The ID starts with an underscore separator.
| TypeIDStartWithUnderscore
| TypeIDStartWithUnderscore Text
-- | The ID ends with an underscore separator.
| TypeIDEndWithUnderscore
| TypeIDEndWithUnderscore Text
-- | The prefix contains an invalid character, namely not lowercase Latin.
| TypeIDErrorPrefixInvalidChar Char
| TypeIDErrorPrefixInvalidChar Text Char
-- | From a `Data.KindID.KindID` conversion. The prefix doesn't match with
-- the expected.
| TypeIDErrorPrefixMismatch Text Text
Expand All @@ -36,16 +37,18 @@ data TypeIDError

instance Show TypeIDError where
show :: TypeIDError -> String
show (TypeIDErrorPrefixTooLong n)
= concat ["Prefix with ", show n, " characters is too long!"]
show (TypeIDErrorPrefixTooLong txt)
= concat [ "The prefix ", show txt
, " with ", show (T.length txt), " characters is too long!" ]
show TypeIDExtraSeparator
= "The underscore separator should not be present if the prefix is empty!"
show TypeIDStartWithUnderscore
= "The prefix should not start with an underscore!"
show TypeIDEndWithUnderscore
= "The prefix should not end with an underscore!"
show (TypeIDErrorPrefixInvalidChar c)
= concat ["Prefix contains invalid character ", show c, "!"]
show (TypeIDStartWithUnderscore txt)
= concat ["The prefix ", show txt, " should not start with an underscore!"]
show (TypeIDEndWithUnderscore txt)
= concat ["The prefix ", show txt, " should not end with an underscore!"]
show (TypeIDErrorPrefixInvalidChar txt c)
= concat [ "The prefix ", show txt
, " contains invalid character ", show c, "!"]
show (TypeIDErrorPrefixMismatch expPrefix actPrefix)
= concat [ "Expected prefix ", show expPrefix, " but got "
, show actPrefix, "!" ]
Expand Down
8 changes: 4 additions & 4 deletions src/Data/TypeID/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,16 @@ parseByteStringM = byteString2IDM
-- | Check if the given prefix is a valid 'TypeID'' prefix.
checkPrefix :: Text -> Maybe TypeIDError
checkPrefix prefix
| T.length prefix > 63 = Just $ TypeIDErrorPrefixTooLong (T.length prefix)
| T.length prefix > 63 = Just $ TypeIDErrorPrefixTooLong prefix
| T.null prefix = Nothing
| T.head prefix == '_' = Just TypeIDStartWithUnderscore
| T.last prefix == '_' = Just TypeIDEndWithUnderscore
| T.head prefix == '_' = Just $ TypeIDStartWithUnderscore prefix
| T.last prefix == '_' = Just $ TypeIDEndWithUnderscore prefix
| otherwise
= case T.uncons ( T.dropWhile ( liftM2 (||) (== '_')
$ liftM2 (&&) isLower isAscii)
prefix) of
Nothing -> Nothing
Just (c, _) -> Just $ TypeIDErrorPrefixInvalidChar c
Just (c, _) -> Just $ TypeIDErrorPrefixInvalidChar prefix c
{-# INLINE checkPrefix #-}

-- | Check if the prefix is valid and the suffix 'UUID' has the correct v7
Expand Down

0 comments on commit eb11ff6

Please sign in to comment.