{-# LANGUAGE FlexibleInstances, Trustworthy #-}
module Text.Chatty.Templates where
import Language.Haskell.TH
strToType :: Name -> Q Type
strToType :: Name -> Q Type
strToType Name
s = do
TyConI Dec
d <- Name -> Q Info
reify Name
s
case Dec
d of
DataD Cxt
_ Name
n [TyVarBndr]
_ Maybe Type
_ [Con]
_ [DerivClause]
_ -> Name -> Q Type
conT (Name -> Q Type) -> Name -> Q Type
forall a b. (a -> b) -> a -> b
$ Name -> Name
simpleName Name
n
NewtypeD Cxt
_ Name
n [TyVarBndr]
_ Maybe Type
_ Con
_ [DerivClause]
_ -> Name -> Q Type
conT (Name -> Q Type) -> Name -> Q Type
forall a b. (a -> b) -> a -> b
$ Name -> Name
simpleName Name
n
TySynD Name
n [TyVarBndr]
_ Type
_ -> Name -> Q Type
conT (Name -> Q Type) -> Name -> Q Type
forall a b. (a -> b) -> a -> b
$ Name -> Name
simpleName Name
n
where
simpleName :: Name -> Name
simpleName :: Name -> Name
simpleName Name
nm =
let s :: String
s = Name -> String
nameBase Name
nm
in case (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
':') String
s of
[] -> String -> Name
mkName String
s
Char
_:[] -> String -> Name
mkName String
s
Char
_:String
t -> String -> Name
mkName String
t