module Toml.ToValue.Generic (
GToTable(..),
genericToTable,
) where
import Data.Map qualified as Map
import GHC.Generics
import Toml.Value (Table)
import Toml.ToValue (ToValue(..))
genericToTable :: (Generic a, GToTable (Rep a)) => a -> Table
genericToTable :: forall a. (Generic a, GToTable (Rep a)) => a -> Table
genericToTable = Rep a Any -> Table
forall a. Rep a a -> Table
forall (f :: * -> *) a. GToTable f => f a -> Table
gToTable (Rep a Any -> Table) -> (a -> Rep a Any) -> a -> Table
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from
{-# INLINE genericToTable #-}
class GToTable f where
gToTable :: f a -> Table
instance GToTable f => GToTable (D1 c f) where
gToTable :: forall a. D1 c f a -> Table
gToTable (M1 f a
x) = f a -> Table
forall a. f a -> Table
forall (f :: * -> *) a. GToTable f => f a -> Table
gToTable f a
x
{-# INLINE gToTable #-}
instance GToTable f => GToTable (C1 c f) where
gToTable :: forall a. C1 c f a -> Table
gToTable (M1 f a
x) = f a -> Table
forall a. f a -> Table
forall (f :: * -> *) a. GToTable f => f a -> Table
gToTable f a
x
{-# INLINE gToTable #-}
instance (GToTable f, GToTable g) => GToTable (f :*: g) where
gToTable :: forall a. (:*:) f g a -> Table
gToTable (f a
x :*: g a
y) = f a -> Table
forall a. f a -> Table
forall (f :: * -> *) a. GToTable f => f a -> Table
gToTable f a
x Table -> Table -> Table
forall a. Semigroup a => a -> a -> a
<> g a -> Table
forall a. g a -> Table
forall (f :: * -> *) a. GToTable f => f a -> Table
gToTable g a
y
{-# INLINE gToTable #-}
instance {-# OVERLAPS #-} (Selector s, ToValue a) => GToTable (S1 s (K1 i (Maybe a))) where
gToTable :: forall a. S1 s (K1 i (Maybe a)) a -> Table
gToTable (M1 (K1 Maybe a
Nothing)) = Table
forall k a. Map k a
Map.empty
gToTable s :: M1 S s (K1 i (Maybe a)) a
s@(M1 (K1 (Just a
x))) = [Char] -> Value -> Table
forall k a. k -> a -> Map k a
Map.singleton (M1 S s (K1 i (Maybe a)) a -> [Char]
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
(f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> [Char]
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
(a :: k1).
t s f a -> [Char]
selName M1 S s (K1 i (Maybe a)) a
s) (a -> Value
forall a. ToValue a => a -> Value
toValue a
x)
{-# INLINE gToTable #-}
instance (Selector s, ToValue a) => GToTable (S1 s (K1 i a)) where
gToTable :: forall a. S1 s (K1 i a) a -> Table
gToTable s :: S1 s (K1 i a) a
s@(M1 (K1 a
x)) = [Char] -> Value -> Table
forall k a. k -> a -> Map k a
Map.singleton (S1 s (K1 i a) a -> [Char]
forall {k} (s :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
(f :: k1 -> *) (a :: k1).
Selector s =>
t s f a -> [Char]
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
(a :: k1).
t s f a -> [Char]
selName S1 s (K1 i a) a
s) (a -> Value
forall a. ToValue a => a -> Value
toValue a
x)
{-# INLINE gToTable #-}
instance GToTable U1 where
gToTable :: forall a. U1 a -> Table
gToTable U1 a
_ = Table
forall k a. Map k a
Map.empty
{-# INLINE gToTable #-}
instance GToTable V1 where
gToTable :: forall a. V1 a -> Table
gToTable V1 a
v = case V1 a
v of {}
{-# INLINE gToTable #-}