generic-data-asserts 0.1.1 → 0.2.0
raw patch · 4 files changed
+46/−19 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Generic.Data.Rep.Assert: type GAssertError a msg = TypeError ('Text "Assertion on generic representation failed for type: " :<>: 'ShowType a :$$: 'Text "Message: " :<>: 'Text msg)
+ Generic.Data.Rep.Error: type GAssertError a msg = TypeError ('Text "Assertion on generic representation failed for type: " :<>: 'ShowType a :$$: 'Text "Message: " :<>: 'Text msg)
+ Generic.Data.Rep.Error: type GAssertErrorNotEnum a = GAssertError a "not enum type (all empty constructors)"
+ Generic.Data.Rep.Error: type GAssertErrorNotSum a = GAssertError a "not sum type (>=2 constructors)"
+ Generic.Data.Rep.Error: type GAssertErrorSum a = GAssertError a "not non-sum type (1 constructor)"
+ Generic.Data.Rep.Error: type GAssertErrorVoid a = GAssertError a "not non-void type (>=1 constructor)"
- Generic.Data.Rep.Assert: type GAssertEnum a = Assert (IsEnum (StripD1 (Rep a))) (GAssertError a "not enum type (all empty constructors)")
+ Generic.Data.Rep.Assert: type GAssertEnum a = Assert (IsEnum (StripD1 (Rep a))) (GAssertErrorNotEnum a)
- Generic.Data.Rep.Assert: type GAssertNotSum a = Assert (IsNotSum (StripD1 (Rep a))) (GAssertError a "not non-sum type (1 constructor)")
+ Generic.Data.Rep.Assert: type GAssertNotSum a = Assert (IsNotSum (StripD1 (Rep a))) (GAssertErrorSum a)
- Generic.Data.Rep.Assert: type GAssertNotVoid a = Assert (IsNotVoid (StripD1 (Rep a))) (GAssertError a "not non-void type (>=1 constructor)")
+ Generic.Data.Rep.Assert: type GAssertNotVoid a = Assert (IsNotVoid (StripD1 (Rep a))) (GAssertErrorVoid a)
- Generic.Data.Rep.Assert: type GAssertSum a = Assert (IsSum (StripD1 (Rep a))) (GAssertError a "not sum type (>=2 constructors)")
+ Generic.Data.Rep.Assert: type GAssertSum a = Assert (IsSum (StripD1 (Rep a))) (GAssertErrorNotSum a)
Files
- CHANGELOG.md +3/−0
- generic-data-asserts.cabal +3/−2
- src/Generic/Data/Rep/Assert.hs +10/−17
- src/Generic/Data/Rep/Error.hs +30/−0
CHANGELOG.md view
@@ -1,3 +1,6 @@+## 0.2.0 (2024-04-05)+* split errors out into own module+ ## 0.1.1 (2024-04-04) Initial release.
generic-data-asserts.cabal view
@@ -5,10 +5,10 @@ -- see: https://github.com/sol/hpack name: generic-data-asserts-version: 0.1.1+version: 0.2.0 synopsis: Structural assertions on generic data representations. description: Please see README.md.-category: Data, Serialization+category: Data, Generics homepage: https://github.com/raehik/generic-rep-asserts#readme bug-reports: https://github.com/raehik/generic-rep-asserts/issues author: Ben Orchard@@ -27,6 +27,7 @@ library exposed-modules: Generic.Data.Rep.Assert+ Generic.Data.Rep.Error other-modules: Paths_generic_data_asserts hs-source-dirs:
src/Generic/Data/Rep/Assert.hs view
@@ -5,21 +5,14 @@ module Generic.Data.Rep.Assert where import GHC.Generics-import GHC.TypeError-import GHC.TypeLits ( type Symbol )-import Data.Kind ( type Constraint )--type GAssertError :: k -> Symbol -> Constraint-type GAssertError a msg = TypeError- ('Text "Assertion on generic representation failed for type: "- :<>: 'ShowType a- :$$: 'Text "Message: " :<>: 'Text msg)+import Generic.Data.Rep.Error+import GHC.TypeError ( Assert ) type family StripD1 a where StripD1 (D1 _ a) = a -- | Type is not void i.e. has at least one constructor.-type GAssertNotVoid a = Assert (IsNotVoid (StripD1 (Rep a)))- (GAssertError a "not non-void type (>=1 constructor)")+type GAssertNotVoid a =+ Assert (IsNotVoid (StripD1 (Rep a))) (GAssertErrorVoid a) type family IsNotVoid a where IsNotVoid V1 = False IsNotVoid _ = True@@ -27,8 +20,8 @@ -- | Type is not a sum type i.e. has at most one constructor. -- -- Permits void types.-type GAssertNotSum a = Assert (IsNotSum (StripD1 (Rep a)))- (GAssertError a "not non-sum type (1 constructor)")+type GAssertNotSum a =+ Assert (IsNotSum (StripD1 (Rep a))) (GAssertErrorSum a) type family IsNotSum a where IsNotSum (_ :+: _) = False IsNotSum _ = True@@ -36,8 +29,8 @@ -- | Type is a sum type i.e. has >=2 constructors. -- -- Permits void types.-type GAssertSum a = Assert (IsSum (StripD1 (Rep a)))- (GAssertError a "not sum type (>=2 constructors)")+type GAssertSum a =+ Assert (IsSum (StripD1 (Rep a))) (GAssertErrorNotSum a) type family IsSum a where IsSum (C1 _ _) = False IsSum _ = True@@ -45,8 +38,8 @@ -- | Type has only empty constructors. -- -- Permits void types.-type GAssertEnum a = Assert (IsEnum (StripD1 (Rep a)))- (GAssertError a "not enum type (all empty constructors)")+type GAssertEnum a =+ Assert (IsEnum (StripD1 (Rep a))) (GAssertErrorNotEnum a) type family IsEnum a where IsEnum V1 = True IsEnum (C1 _ a) = ConsIsEmpty a
+ src/Generic/Data/Rep/Error.hs view
@@ -0,0 +1,30 @@+-- | Descriptive type errors for generic representation assertion failures.+--+-- These are potentially useful separately from the asserts, in places where+-- we're unable to perform asserts which evaluate to 'Constraint's e.g. type+-- families, so we expose them neatly here.++module Generic.Data.Rep.Error where++import GHC.TypeError+import GHC.TypeLits ( type Symbol )++-- polymorphic kind keeps us extra useful -- we only use as 'Constraint', but+-- library users may want to use this in type families+type GAssertError :: ka -> Symbol -> k+type GAssertError a msg = TypeError+ ('Text "Assertion on generic representation failed for type: "+ :<>: 'ShowType a+ :$$: 'Text "Message: " :<>: 'Text msg)++type GAssertErrorVoid a =+ GAssertError a "not non-void type (>=1 constructor)"++type GAssertErrorSum a =+ GAssertError a "not non-sum type (1 constructor)"++type GAssertErrorNotSum a =+ GAssertError a "not sum type (>=2 constructors)"++type GAssertErrorNotEnum a =+ GAssertError a "not enum type (all empty constructors)"