-- | Conversions from type-level values
--
module Data.Conversions.FromType where

import           Data.Conversions
import           Data.Kind                      ( Type )
import           Data.Proxy                     ( Proxy(..) )
import           Data.Text                      ( Text )
import           GHC.TypeLits            hiding ( natVal )
import           GHC.TypeNats                   ( natVal )
import           Numeric.Natural                ( Natural )
import           Prelude

class FromType a (b :: Type) where
  fromType :: b

-- These instances are not polymorphic in the last parameter so to
-- avoid over-lapping instances
--
instance (KnownNat nat) => FromType nat Natural where
  fromType :: Natural
fromType = Proxy nat -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natVal (Proxy nat
forall k (t :: k). Proxy t
Proxy @nat)

instance (KnownSymbol symbol) => FromType symbol String where
  fromType :: String
fromType = Proxy symbol -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (Proxy symbol
forall k (t :: k). Proxy t
Proxy @symbol)

instance (KnownSymbol symbol) => FromType symbol Text where
  fromType :: Text
fromType = Conversion Text String => String -> Text
forall b a. Conversion b a => a -> b
convert @Text @String (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ forall b. FromType symbol b => b
forall k (a :: k) b. FromType a b => b
fromType @symbol