toml-parser-2.0.1.0: TOML 1.0.0 parser

Copyright | (c) Eric Mertens 2024
License | ISC
Maintainer | emertens@gmail.com
Safe Haskell | None
Language | Haskell2010

Toml.Schema.Generic

Contents

- DerivingVia
- FromValue
- ToValue

Description

This module makes it possible to easily derive the TOML classes using the `DerivingVia` extension.

For example:

    data Physical = Physical {
        color :: String,
        shape :: String
        }
        deriving (Eq, Show, Generic)
        deriving (ToTable, ToValue, FromValue) via GenericTomlTable Physical

These derived instances would allow you to match TOML `{color="red", shape="round"}` to value `Physical "red" "round"`.

    data Coord = Coord Int Int
        deriving (Eq, Show, Generic)
        deriving (ToValue, FromValue) via GenericTomlArray Physical

These derived instances would allow you to match TOML `[1,2]` to value `Coord 1 2`.

Synopsis

- newtype GenericTomlTable a = GenericTomlTable a
- newtype GenericTomlArray a = GenericTomlArray a
- genericFromArray :: (Generic a, GFromArray (Rep a)) => Value' l -> Matcher l a
- genericFromTable :: (Generic a, GParseTable (Rep a)) => Value' l -> Matcher l a
- class GFromArray (f :: Type -> Type)
- class GParseTable (f :: Type -> Type)
- genericToArray :: (Generic a, GToArray (Rep a)) => a -> Value
- genericToTable :: (Generic a, GToTable (Rep a)) => a -> Table
- class GToArray (f :: Type -> Type)
- class GToTable (f :: Type -> Type)

# DerivingVia

newtype GenericTomlTable a #

Helper type to use GHC's DerivingVia extension to derive `ToValue`, `ToTable`, `FromValue` for records.

Constructors

GenericTomlTable a |

#### Instances

Instances details

(Generic a, GParseTable (Rep a)) => FromValue (GenericTomlTable a) # | Instance derived using `genericParseTable`
Instance details Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlTable a) # listFromValue :: Value' l -> Matcher l [GenericTomlTable a] #
(Generic a, GToTable (Rep a)) => ToTable (GenericTomlTable a) # | Instance derived using `genericToTable`
Instance details Defined in Toml.Schema.Generic Methods toTable :: GenericTomlTable a -> Table #
(Generic a, GToTable (Rep a)) => ToValue (GenericTomlTable a) # | Instance derived from `ToTable` instance using `defaultTableToValue`
Instance details Defined in Toml.Schema.Generic Methods toValue :: GenericTomlTable a -> Value # toValueList :: [GenericTomlTable a] -> Value #

newtype GenericTomlArray a #

Helper type to use GHC's DerivingVia extension to derive `ToValue`, `ToTable`, `FromValue` for any product type.

Constructors

GenericTomlArray a |

#### Instances

Instances details

(Generic a, GFromArray (Rep a)) => FromValue (GenericTomlArray a) # | Instance derived using `genericFromArray`
Instance details Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlArray a) # listFromValue :: Value' l -> Matcher l [GenericTomlArray a] #
(Generic a, GToArray (Rep a)) => ToValue (GenericTomlArray a) # | Instance derived using `genericToArray`
Instance details Defined in Toml.Schema.Generic Methods toValue :: GenericTomlArray a -> Value # toValueList :: [GenericTomlArray a] -> Value #

# FromValue

genericFromArray :: (Generic a, GFromArray (Rep a)) => Value' l -> Matcher l a #

Match a `Value'` as an array positionally matching field fields of a constructor to the elements of the array.

genericFromTable :: (Generic a, GParseTable (Rep a)) => Value' l -> Matcher l a #

Implementation of `fromValue` using `genericParseTable` to derive a match from the record field names of the target type.

class GFromArray (f :: Type -> Type) #

Supports conversion of TOML arrays into product-type values.

Minimal complete definition

gFromArray

#### Instances

Instances details

GFromArray (U1 :: Type -> Type) # | Uses no array elements
Instance details Defined in Toml.Schema.Generic.FromValue Methods gFromArray :: StateT [Value' l] (Matcher l) (U1 a) #
(GFromArray f, GFromArray g) => GFromArray (f :*: g) # |
Instance details Defined in Toml.Schema.Generic.FromValue Methods gFromArray :: StateT [Value' l] (Matcher l) ((f :*: g) a) #
FromValue a => GFromArray (K1 i a :: Type -> Type) # |
Instance details Defined in Toml.Schema.Generic.FromValue Methods gFromArray :: StateT [Value' l] (Matcher l) (K1 i a a0) #
GFromArray f => GFromArray (M1 i c f) # |
Instance details Defined in Toml.Schema.Generic.FromValue Methods gFromArray :: StateT [Value' l] (Matcher l) (M1 i c f a) #

class GParseTable (f :: Type -> Type) #

Supports conversion of TOML tables into record values using field selector names as TOML keys.

Minimal complete definition

gParseTable

#### Instances

Instances details

GParseTable (U1 :: Type -> Type) # | Emits empty table
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l (U1 a) #
(GParseTable f, GParseTable g) => GParseTable (f :*: g) # | Matches left then right component
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l ((f :*: g) a) #
GParseTable f => GParseTable (C1 ('MetaCons sym fix 'True) f) # | Ignores value constructor name - only supports record constructors
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l (C1 ('MetaCons sym fix 'True) f a) #
GParseTable f => GParseTable (D1 c f) # | Ignores type constructor name
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l (D1 c f a) #
(Selector s, FromValue a) => GParseTable (S1 s (K1 i (Maybe a) :: Type -> Type)) # | Omits the key from the table on nothing, includes it on just
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l (S1 s (K1 i (Maybe a) :: Type -> Type) a0) #
(Selector s, FromValue a) => GParseTable (S1 s (K1 i a :: Type -> Type)) # | Uses record selector name as table key
Instance details Defined in Toml.Schema.Generic.FromValue Methods gParseTable :: ParseTable l (S1 s (K1 i a :: Type -> Type) a0) #

# ToValue

genericToArray :: (Generic a, GToArray (Rep a)) => a -> Value #

Use a record's field names to generate a `Table`

genericToTable :: (Generic a, GToTable (Rep a)) => a -> Table #

Use a record's field names to generate a `Table`

class GToArray (f :: Type -> Type) #

Convert product types to arrays positionally.

Minimal complete definition

gToArray

#### Instances

Instances details

(GToArray f, GToArray g) => GToArray (f :*: g) # | Convert left and then right
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToArray :: (f :*: g) a -> [Value] -> [Value] #
ToValue a => GToArray (K1 i a :: Type -> Type) # | Convert fields using `ToValue` instances
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToArray :: K1 i a a0 -> [Value] -> [Value] #
GToArray f => GToArray (M1 i c f) # | Ignore metadata
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToArray :: M1 i c f a -> [Value] -> [Value] #

class GToTable (f :: Type -> Type) #

Supports conversion of product types with field selector names to TOML values.

Minimal complete definition

gToTable

#### Instances

Instances details

GToTable (U1 :: Type -> Type) # | Emits empty table
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: U1 a -> [(Text, Value)] -> [(Text, Value)] #
GToTable (V1 :: Type -> Type) # |
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: V1 a -> [(Text, Value)] -> [(Text, Value)] #
(GToTable f, GToTable g) => GToTable (f :*: g) # |
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: (f :*: g) a -> [(Text, Value)] -> [(Text, Value)] #
GToTable f => GToTable (C1 c f) # | Ignores value constructor names
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: C1 c f a -> [(Text, Value)] -> [(Text, Value)] #
GToTable f => GToTable (D1 c f) # | Ignores type constructor names
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: D1 c f a -> [(Text, Value)] -> [(Text, Value)] #
(Selector s, ToValue a) => GToTable (S1 s (K1 i (Maybe a) :: Type -> Type)) # | Omits the key from the table on nothing, includes it on just
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: S1 s (K1 i (Maybe a) :: Type -> Type) a0 -> [(Text, Value)] -> [(Text, Value)] #
(Selector s, ToValue a) => GToTable (S1 s (K1 i a :: Type -> Type)) # | Uses record selector name as table key
Instance details Defined in Toml.Schema.Generic.ToValue Methods gToTable :: S1 s (K1 i a :: Type -> Type) a0 -> [(Text, Value)] -> [(Text, Value)] #