{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-partial-type-signatures #-}

{-
  TemplateHaskell functions
-}

module Data.Registry.MessagePack.TH where

import Control.Monad.Fail
import Data.List (elemIndex)
import Data.Registry.MessagePack.Options
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import Protolude hiding (Type)

indexConstructorTypes :: [Type] -> [Type] -> Q [(Type, Int)]
indexConstructorTypes :: [Type] -> [Type] -> Q [(Type, Int)]
indexConstructorTypes [Type]
allTypes [Type]
constructorTypes =
  forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Type]
constructorTypes forall a b. (a -> b) -> a -> b
$ \Type
t ->
    case forall a. Eq a => a -> [a] -> Maybe Int
elemIndex Type
t [Type]
allTypes of
      Just Int
n -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type
t, Int
n)
      Maybe Int
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"the type " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Type
t forall a. Semigroup a => a -> a -> a
<> String
" cannot be found in the list of all types " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show [Type]
allTypes

-- | Get the types of all the fields of a constructor
typesOf :: Con -> Q [Type]
typesOf :: Con -> Q [Type]
typesOf (NormalC Name
_ [BangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [BangType]
types)
typesOf (RecC Name
_ [VarBangType]
types) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ (\(Name
_, Bang
_, Type
t) -> Type
t) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [VarBangType]
types
typesOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create encoders for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"encoders creation failed"

nameOf :: Con -> Q Name
nameOf :: Con -> Q Name
nameOf (NormalC Name
n [BangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf (RecC Name
n [VarBangType]
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Name
n
nameOf Con
other = do
  forall (m :: * -> *). Quasi m => Bool -> String -> m ()
qReport Bool
True (String
"we can only create encoders for normal constructors and records, got: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Con
other)
  forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"encoders creation failed"

-- | Remove the module name from a qualified name
makeName :: Options -> Name -> Name
makeName :: Options -> Name -> Name
makeName Options
options = String -> Name
mkName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. ConvertText a b => a -> b
toS forall b c a. (b -> c) -> (a -> b) -> a -> c
. Options -> Text -> Text
modifyTypeName Options
options forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Show a, StringConv String b) => a -> b
show

-- | Return the name of a given type with a modified name based on options
getSimpleTypeName :: Options -> Type -> Name
getSimpleTypeName :: Options -> Type -> Name
getSimpleTypeName Options
options (ForallT [TyVarBndr Specificity]
_ [Type]
_ Type
ty) = Options -> Type -> Name
getSimpleTypeName Options
options Type
ty
getSimpleTypeName Options
options (VarT Name
name) = Options -> Name -> Name
makeName Options
options Name
name
getSimpleTypeName Options
options (ConT Name
name) = Options -> Name -> Name
makeName Options
options Name
name
getSimpleTypeName Options
options (TupleT Int
n) = Options -> Name -> Name
makeName Options
options forall a b. (a -> b) -> a -> b
$ Int -> Name
tupleTypeName Int
n
getSimpleTypeName Options
options Type
ArrowT = Options -> Name -> Name
makeName Options
options ''(->)
getSimpleTypeName Options
options Type
ListT = Options -> Name -> Name
makeName Options
options ''[]
getSimpleTypeName Options
options (AppT Type
t1 Type
t2) = String -> Name
mkName (forall a b. (Show a, StringConv String b) => a -> b
show (Options -> Type -> Name
getSimpleTypeName Options
options Type
t1) forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show (Options -> Type -> Name
getSimpleTypeName Options
options Type
t2))
getSimpleTypeName Options
options (SigT Type
t Type
_) = Options -> Type -> Name
getSimpleTypeName Options
options Type
t
getSimpleTypeName Options
options (UnboxedTupleT Int
n) = Options -> Name -> Name
makeName Options
options forall a b. (a -> b) -> a -> b
$ Int -> Name
unboxedTupleTypeName Int
n
getSimpleTypeName Options
_ Type
t = forall a. HasCallStack => Text -> a
panic forall a b. (a -> b) -> a -> b
$ Text
"getSimpleTypeName: Unknown type: " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show Type
t