Safe Haskell | None |
---|---|
Language | Haskell2010 |
Scaffolding for building, reading, updating and serializing configuration data types. To create an INI-configurable data type:
{-# LANGUAGE DeriveGeneric #-} import Data.Tini.Configurable data MyConfig = MyConfig { someValue :: Int, ... } deriving Generic instance Configurable MyConfig where defaultConfig = MyConfig { someValue = 42, ... }
For more advanced usage, see https://github.com/valderman/tini.
Synopsis
- class ConfigInvariants a => Configurable a where
- sectionName :: SectionName
- defaultConfig :: a
- fromIni :: Configurable a => Ini -> a
- toIni :: Configurable a => a -> Ini
- readConfigFile :: Configurable a => FilePath -> IO a
- readConfigFileWith :: Configurable a => a -> FilePath -> IO a
- writeConfigFile :: Configurable a => FilePath -> a -> IO ()
- updateConfigFile :: Configurable a => FilePath -> a -> IO ()
- class ConfigInvariants a => Configurable a where
- type ExcludedFields a :: [Symbol]
- updateConfig :: Ini -> a -> a
- updateIni :: a -> Ini -> Ini
- sectionName :: SectionName
- defaultConfig :: a
- type ConfigInvariants a = (FieldsOf a (ExcludedFields a), SymList (ExcludedFields a))
- class Generic a
Basic configuration
class ConfigInvariants a => Configurable a where Source #
Any type which can be stored and loaded as an INI configuration file.
Using the default implementation, this includes all record types with a
single data constructor, and where all fields implement IniValue
.
sectionName :: SectionName Source #
The section of an INI under which to place settings from this configuration. The default is no section.
defaultConfig :: a Source #
The default values for this configuration.
fromIni :: Configurable a => Ini -> a Source #
Create a configuration from the given INI.
Missing values will be filled in from defaultConfig
.
toIni :: Configurable a => a -> Ini Source #
Create a new INI configuration from the given config.
Working with config files
readConfigFile :: Configurable a => FilePath -> IO a Source #
Read the given INI file into a configuration, using defaultConfig
to
fill in any values missing from the config file.
readConfigFileWith :: Configurable a => a -> FilePath -> IO a Source #
Update the given configuration with the settings read from the given INI file.
writeConfigFile :: Configurable a => FilePath -> a -> IO () Source #
Write the given configuration to the given file. If the file already exists, it will be overwritten.
updateConfigFile :: Configurable a => FilePath -> a -> IO () Source #
Update the given configuration file with the given config.
This function first reads the given file as an INI, overwrites all its values with values from from the given config, then writes the result back to disk atomically. This ensures that no data is lost in the event of a crash, and that the resulting file preserves both comments and keys which are not recognized by the config are not clobbered.
Advanced configuration
class ConfigInvariants a => Configurable a where Source #
Any type which can be stored and loaded as an INI configuration file.
Using the default implementation, this includes all record types with a
single data constructor, and where all fields implement IniValue
.
type ExcludedFields a :: [Symbol] Source #
Fields of the configuration type which should not be read from or written to an INI. The default is to allow configuration of all fields.
updateConfig :: Ini -> a -> a Source #
Update the given configuration using settings from the given INI.
In the default implementation, settings are looked up by record selector.
A record called playerName
will be overwritten with the value at key
playerName
in the given INI, if such a key is present and
of the correct type.
updateConfig :: (Generic a, GConfigurable (Rep a)) => Ini -> a -> a Source #
Update the given configuration using settings from the given INI.
In the default implementation, settings are looked up by record selector.
A record called playerName
will be overwritten with the value at key
playerName
in the given INI, if such a key is present and
of the correct type.
updateIni :: a -> Ini -> Ini Source #
Update the given INI with the values from the given config.
Implementations should preserve comments in the given INI (i.e. only
update it using set
).
updateIni :: (Generic a, GConfigurable (Rep a)) => a -> Ini -> Ini Source #
Update the given INI with the values from the given config.
Implementations should preserve comments in the given INI (i.e. only
update it using set
).
sectionName :: SectionName Source #
The section of an INI under which to place settings from this configuration. The default is no section.
defaultConfig :: a Source #
The default values for this configuration.
type ConfigInvariants a = (FieldsOf a (ExcludedFields a), SymList (ExcludedFields a)) Source #
Invariants on ExcludedFields
:
all names appearing in ExcludedFields
must be names of fields of a
.
Re-exports
Representable types of kind *
.
This class is derivable in GHC with the DeriveGeneric
flag on.
A Generic
instance must satisfy the following laws:
from
.to
≡id
to
.from
≡id