Skip to content

Commit

Permalink
More useful compile-time errors for invalid KindID prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MMZK1526 committed May 30, 2024
1 parent 4fcc52a commit d12905f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

* Update implementation so that the prefix for `KindID` now conforms with specification v0.3.0.

* More useful compile-time errors for invalid `KindID` prefixes.

* More tests.

* Fix documentation typos.
Expand Down
27 changes: 22 additions & 5 deletions src/Data/KindID/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Data.KindID.Class
ValidPrefix
, ToPrefix(..)
-- * Helpers
, LengthLT64C
, IsLUSymbolC
, LengthSymbol
, IsLowerChar
, IsUnderscore
Expand All @@ -27,6 +29,7 @@ module Data.KindID.Class
, ILSUH
) where

import Data.Kind
import Data.Type.Bool
import Data.Type.Equality
import Data.Type.Ord
Expand Down Expand Up @@ -62,14 +65,28 @@ class ToPrefix a where
type PrefixSymbol a :: Symbol

-- | The 'PrefixSymbol' of a 'Symbol' is the 'Symbol' itself.
instance ToPrefix (a :: Symbol) where
type PrefixSymbol a = a

instance ToPrefix (s :: Symbol) where
type PrefixSymbol s = s

-- | A constraint for valid prefix 'Symbol's.
type ValidPrefix prefix = ( KnownSymbol prefix
, LengthSymbol prefix < 64
, IsLUSymbol prefix ~ 'True )
, LengthLT64C prefix
, IsLUSymbolC 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 "

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 (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 (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’.

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 (LengthSymbol 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 " characters is too long!" ) )

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’.

-- | 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!"))

Check warning on line 89 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 89 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 89 in src/Data/KindID/Class.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.2.5)

Unticked promoted constructor: ‘Text’.

-- | The length of a 'Symbol' as a 'Nat'.
type family LengthSymbol (prefix :: Symbol) :: Nat where
Expand Down

0 comments on commit d12905f

Please sign in to comment.