{-# LANGUAGE FlexibleInstances, UndecidableInstances, ScopedTypeVariables #-}
module Toml.Generic (
GenericTomlTable(GenericTomlTable),
GenericTomlArray(GenericTomlArray),
) where
import Data.Coerce (coerce)
import GHC.Generics (Generic(Rep))
import Toml.FromValue (FromValue(fromValue), parseTableFromValue)
import Toml.FromValue.Generic (GParseTable, GFromArray, genericParseTable, genericFromArray)
import Toml.FromValue.Matcher (Matcher)
import Toml.ToValue (ToTable(toTable), ToValue(toValue), defaultTableToValue)
import Toml.ToValue.Generic (GToTable, GToArray, genericToTable, genericToArray)
import Toml.Value (Value, Table)
newtype GenericTomlTable a = GenericTomlTable a
instance (Generic a, GToTable (Rep a)) => ToValue (GenericTomlTable a) where
toValue :: GenericTomlTable a -> Value
toValue = GenericTomlTable a -> Value
forall a. ToTable a => a -> Value
defaultTableToValue
{-# INLINE toValue #-}
instance (Generic a, GToTable (Rep a)) => ToTable (GenericTomlTable a) where
toTable :: GenericTomlTable a -> Table
toTable = (a -> Table) -> GenericTomlTable a -> Table
forall a b. Coercible a b => a -> b
coerce (a -> Table
forall a. (Generic a, GToTable (Rep a)) => a -> Table
genericToTable :: a -> Table)
{-# INLINE toTable #-}
instance (Generic a, GParseTable (Rep a)) => FromValue (GenericTomlTable a) where
fromValue :: Value -> Matcher (GenericTomlTable a)
fromValue = (Value -> Matcher a) -> Value -> Matcher (GenericTomlTable a)
forall a b. Coercible a b => a -> b
coerce (ParseTable a -> Value -> Matcher a
forall a. ParseTable a -> Value -> Matcher a
parseTableFromValue ParseTable a
forall a. (Generic a, GParseTable (Rep a)) => ParseTable a
genericParseTable :: Value -> Matcher a)
{-# INLINE fromValue #-}
newtype GenericTomlArray a = GenericTomlArray a
instance (Generic a, GToArray (Rep a)) => ToValue (GenericTomlArray a) where
toValue :: GenericTomlArray a -> Value
toValue = (a -> Value) -> GenericTomlArray a -> Value
forall a b. Coercible a b => a -> b
coerce (a -> Value
forall a. (Generic a, GToArray (Rep a)) => a -> Value
genericToArray :: a -> Value)
{-# INLINE toValue #-}
instance (Generic a, GFromArray (Rep a)) => FromValue (GenericTomlArray a) where
fromValue :: Value -> Matcher (GenericTomlArray a)
fromValue = (Value -> Matcher a) -> Value -> Matcher (GenericTomlArray a)
forall a b. Coercible a b => a -> b
coerce (Value -> Matcher a
forall a. (Generic a, GFromArray (Rep a)) => Value -> Matcher a
genericFromArray :: Value -> Matcher a)
{-# INLINE fromValue #-}