{-# LANGUAGE FlexibleInstances #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

module Text.Chatty.Templates where

import Language.Haskell.TH

-- | Returns a TypeQ from a name
strToType :: Name -> Q Type
strToType s = do
  TyConI d <- reify s
  case d of
    DataD _ n _ _ _ -> conT $ simpleName n
    NewtypeD _ n _ _ _ -> conT $ simpleName n
    TySynD n _ _ -> conT $ simpleName n
  where
    simpleName :: Name -> Name
    simpleName nm =
      let s = nameBase nm
      in case dropWhile (/=':') s of
        []          -> mkName s
        _:[]        -> mkName s
        _:t         -> mkName t