@@ -22,13 +22,16 @@ module Data.API.Tools.Combinators
2222 , warnOnOmittedInstance
2323 , newtypeSmartConstructors
2424 , defaultToolSettings
25+ , defaultDerivedClasses
2526 ) where
2627
2728import Data.API.Types
2829
2930import Control.Applicative
3031import Data.Monoid
3132import Data.Semigroup as Sem
33+ import Data.String
34+ import Data.Typeable
3235import Language.Haskell.TH
3336import Prelude
3437
@@ -43,15 +46,41 @@ data ToolSettings = ToolSettings
4346 , newtypeSmartConstructors :: Bool
4447 -- ^ Rename the constructors of filtered newtypes and generate
4548 -- smart constructors that enforce the invariants
49+ , defaultDerivedClasses :: APINode -> [Name ]
50+ -- ^ The classes which are derived automatically by api-tools.
4651 }
4752
4853-- | Default settings designed to be overridden.
4954defaultToolSettings :: ToolSettings
5055defaultToolSettings = ToolSettings
5156 { warnOnOmittedInstance = False
5257 , newtypeSmartConstructors = False
58+ , defaultDerivedClasses = default_derived_classes
5359 }
5460
61+ -- | Default names of classes for which to derive instances, depending
62+ -- on the type of API node.
63+ default_derived_classes :: APINode -> [Name ]
64+ default_derived_classes an = case anSpec an of
65+ SpNewtype sn -> case snType sn of
66+ BTstring -> ''IsString : derive_leaf_nms
67+ BTbinary -> derive_leaf_nms
68+ BTbool -> derive_leaf_nms
69+ BTint -> derive_leaf_nms
70+ BTutc -> derive_leaf_nms
71+ SpRecord _ -> derive_node_nms
72+ SpUnion _ -> derive_node_nms
73+ SpEnum _ -> derive_leaf_nms ++ [''Bounded, ''Enum]
74+ SpSynonym _ -> []
75+
76+ derive_leaf_nms :: [Name ]
77+ derive_leaf_nms = [''Show,''Eq,''Ord,''Typeable]
78+
79+ derive_node_nms :: [Name ]
80+ derive_node_nms = [''Show,''Eq,''Typeable]
81+
82+
83+
5584-- | A @'Tool' a@ is something that can generate TH declarations from
5685-- a value of type @a@. Tools can be combined using the 'Monoid'
5786-- instance.
0 commit comments