Stability | unstable |
---|---|
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
IMPORTANT NOTE: this module works with the user's home directory, and that is the place where the configuration will be read from and persisted.
- type Key = String
- type Value = String
- data Configuration
- withConfiguration :: String -> (Configuration -> IO b) -> IO b
- loadConfiguration :: String -> IO Configuration
- saveConfiguration :: Configuration -> IO ()
- hasV :: Configuration -> Key -> Bool
- getV :: Configuration -> Key -> Maybe Value
- removeV :: Configuration -> Key -> Configuration
- replaceV :: Configuration -> Key -> Value -> Configuration
- type InteractiveValidator = Value -> IO (Either String Value)
- acceptNonBlank :: InteractiveValidator
- acceptAnything :: InteractiveValidator
- fillInteractively :: Configuration -> [(Key, InteractiveValidator)] -> IO Configuration
- fillInteractivelyWhen :: (Configuration -> Bool) -> Configuration -> [(Key, InteractiveValidator)] -> IO Configuration
- newC :: Configuration -> Bool
- emptyC :: Configuration -> Bool
Basics
Types
data Configuration Source
Managing
:: String | Configuration file name |
-> (Configuration -> IO b) | |
-> IO b |
However if you like to stack software ala withSocketsDo $ withX $ withY
this might not
be your preferred approach. You could go with the following approach, which was excluded for
library portability:
{-# LANGUAGE ImplicitParams, RankNTypes #-} import System.Config.File withConfigurationImplicit :: String -> ((?configuration :: Configuration) => IO b) -> IO b withConfigurationImplicit filename f = withConfiguration filename (\c -> let ?configuration = c in f) main = withConfigurationImplicit ".apprc" $ do print $ hasV "name" ?configuration print $ getV "name" ?configuration
:: String | Configuration file name |
-> IO Configuration |
saveConfiguration :: Configuration -> IO () Source
The configuration will be saved into the same file it was read from, obviously
CRUD
hasV :: Configuration -> Key -> Bool Source
removeV :: Configuration -> Key -> Configuration Source
replaceV :: Configuration -> Key -> Value -> Configuration Source
Data "entry"
It proved useful that for a few small cases to also have a way to "build" the configuration interactively. When you consider easy to validate fields (that don't depend on other fields), it seems to be worth to have this functionality included.
Validation
type InteractiveValidator = Value -> IO (Either String Value) Source
Via the Left
data constructor we are able to pass the message necessary to
notify the user that the inputed data is not valid
Execution
fillInteractively :: Configuration -> [(Key, InteractiveValidator)] -> IO Configuration Source
Request user input for the set of (Key, InteractiveValidator). For keys that are
already set in the Configuration
, values will be overwritten
fillInteractivelyWhen :: (Configuration -> Bool) -> Configuration -> [(Key, InteractiveValidator)] -> IO Configuration Source
Execution dependent on a predicate
Predicates
newC :: Configuration -> Bool Source
Has this configuration just been created?
emptyC :: Configuration -> Bool Source
Configuration doesn't contain any values?