Copyright | © Mike Meyer, 2015 |
---|---|
License | BSD3 |
Maintainer | mwm@mired.org |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Most ini config files turn into 1-1 maps quite nicely. However, some encode lists of objects, which require having more than one section of a given name, or more than one value in a section with a given name - and order matters.
This package parsers Ini files, but instead of creating maps, it
creates a list of sections, each section of which is a list of
values. As a result, the get
function can now return a list of
values for options that can occur multiple times, and there are plural
versions of the Option and Section fetchers.
- type Section = AList OptionName Option
- data Config
- type OptionName = String
- type SectionName = String
- type Option = String
- type SectionItem = (OptionName, Option)
- type ConfigItem = (SectionName, Section)
- type UpdateValue = SectionName -> OptionName -> Option -> Maybe Option
- type UpdateOption = SectionName -> OptionName -> Option -> Maybe SectionItem
- class Value a where
- config :: Section -> [ConfigItem] -> Config
- setDefault :: Config -> Section -> Config
- (<+) :: Cons container item => container -> item -> container
- (+>) :: Cons container item => item -> container -> container
- toList :: Cons container item => container -> [item]
- fromList :: Cons container item => [item] -> container
- get :: Value a => Config -> Maybe SectionName -> OptionName -> Maybe a
- getDefault :: Config -> Section
- getSection :: Config -> SectionName -> Maybe Section
- getSections :: Config -> SectionName -> [Section]
- getSectionsBy :: Config -> (SectionName -> Bool) -> [ConfigItem]
- updateValues :: UpdateValue -> Config -> Config
- updateDefaultValues :: (OptionName -> Option -> Maybe Option) -> Config -> Config
- updateSectionValues :: (OptionName -> Option -> Maybe Option) -> Section -> Section
- updateOptions :: UpdateOption -> Config -> Config
- updateDefaultOptions :: (OptionName -> Option -> Maybe SectionItem) -> Config -> Config
- updateSectionOptions :: (OptionName -> Option -> Maybe SectionItem) -> Section -> Section
- formatConfig :: Config -> String
- writeConfig :: Config -> IO ()
- writeConfigFile :: FilePath -> Config -> IO ()
- hWriteConfig :: Handle -> Config -> IO ()
- parseConfig :: String -> Result Config
- parseFile :: FilePath -> IO (Maybe Config)
- parseFileEx :: FilePath -> IO (Result Config)
Types
A config is an unmaned Section
and an AList
of SectionItem
s.
type OptionName = String Source
type SectionName = String Source
Names are all String
s.
type SectionItem = (OptionName, Option) Source
type ConfigItem = (SectionName, Section) Source
type UpdateValue = SectionName -> OptionName -> Option -> Maybe Option Source
An UpdateValue
is a function that takes a SectionName
,
OptionName
and Option
and returns a Nothing if it doesn't want
to change the given SectionItem
, or Just
Option
if it does.
type UpdateOption = SectionName -> OptionName -> Option -> Maybe SectionItem Source
An UpdateOption
is like an UpdateValue
, except it returns a
Maybe
SectionItem
, allowing it to change the key as well as the
value of the SectionItem
in question.
The Value
class is one that the get functions can
return. Most notably, names that occur multiple times in a section
can become a List
, returning a singleton or empty List
for
single or missing names in a context where a List
is needed.
0/1
, yes/no
, on/off
, true/false
and enabled/disabled
values
will be returned as Bool
in the appropriate contexts.
Finally, any value that has a Read
instance will be converted
if possible, so that integer and floating point values can be
used immediately.
getValue :: OptionName -> Section -> Maybe a Source
gets the value for the
named getValue
Section
OptionName
Option
from the Section
.
Build
config :: Section -> [ConfigItem] -> Config Source
setDefault :: Config -> Section -> Config Source
setDefault
sets the default Section
for the Config
.
Convert
Query
get :: Value a => Config -> Maybe SectionName -> OptionName -> Maybe a Source
get
a value from a Config
selected by Maybe
SectionName
and OptionName
. Nothing
as the SectionName
gets Option
values
from the default Section
.
getDefault :: Config -> Section Source
getDefault
returns the default section from a Config
.
getSection :: Config -> SectionName -> Maybe Section Source
getSection
returns the first Section
name from Config
if one
exists.
getSections :: Config -> SectionName -> [Section] Source
getSections
returns the List
of Section
s with name in the Config
.
getSectionsBy :: Config -> (SectionName -> Bool) -> [ConfigItem] Source
getSectionsBy
returns a list of ConfigItem'
s from a Config
chosen by the provided function.
Update
updateValues :: UpdateValue -> Config -> Config Source
updateOptions
uses an UpdateValue
to update all the values in
the Config
.
updateDefaultValues :: (OptionName -> Option -> Maybe Option) -> Config -> Config Source
updateDefaultOptions
updates the values in the default
Section
of the Config
with the given function, which is similar
to an UpdateValue
without the SectionName
argument.
updateSectionValues :: (OptionName -> Option -> Maybe Option) -> Section -> Section Source
updateSectionValues
updates the values in the named Section
of the Config
with the given function, which is similar to an
UpdateValue
without the SectionName
argument.
updateOptions :: UpdateOption -> Config -> Config Source
updateOptions
uses an UpdateOption
to update all the options
in the Config
.
updateDefaultOptions :: (OptionName -> Option -> Maybe SectionItem) -> Config -> Config Source
updateDefaultOptions
updates the options in the default
Section
of the Config
with the given function, which is similar
to an UpdateOption
without the SectionName
argument.
updateSectionOptions :: (OptionName -> Option -> Maybe SectionItem) -> Section -> Section Source
updateSectionOptions
updates the options in the named Section
Section
of the Config
with the given function, which is similar
to an UpdateOption
without the SectionName
argument.
Format
formatConfig :: Config -> String Source
formatConfig
converts a Config
to a String
representation
for use in a .ini
file.
writeConfig :: Config -> IO () Source
writeConfig
formats a Config
and writes it to stdout
.
writeConfigFile :: FilePath -> Config -> IO () Source
writeConfigFile
formats a Config
and writes it to FilePath
.
hWriteConfig :: Handle -> Config -> IO () Source
Parse
parseConfig :: String -> Result Config Source
parseConfig
parses a String
into a Config
, returning the
Config
wrapped in a a Result
parseFile :: FilePath -> IO (Maybe Config) Source
parseFile
parses the named .ini
file, sending diagnostic messages to
the console.
parseFileEx :: FilePath -> IO (Result Config) Source
parseFileEx
parses the named .ini
file, returning either the
Config
or diagnostic messages in the Result
.