module Data.ConfigFile.Monadic (
  -- * Overview
  -- $overview
  
  module Reexporting,
  simpleAccess,
  interpolatingAccess,
  readfile, readhandle, readstring,
  has_section, options, has_option, items,
  set, setshow, remove_option, add_section, remove_section
) where

import Control.Monad.Error
import System.IO(Handle)
import Data.ConfigFile as Reexporting (SectionSpec, OptionSpec, ConfigParser(..),
                                  CPErrorData, CPError, emptyCP, Get_C(..), sections, merge, to_string)
import qualified Data.ConfigFile as C

{- $overview
This module reexports a slightly different version of the standard API which makes it more convenient for chaining monadically.  Everywhere a 'ConfigParser' was the first argument in a function in the standard API, it is now the last.  This lets you rewrite 

> do let cp = emptyCP
>    cp <- add_section cp "sect1"
>    cp <- set cp "sect1" "opt1" "foo"
>    cp <- set cp "sect1" "opt2" "bar"
>    options cp "sect1"

as

> return emptyCP >>=
>  add_section "sect1" >>=
>  set "sect1" "opt1" "foo" >>=
>  set "sect1" "opt2" "bar" >>=
>  options "sect1"

which may be more elegant in some cases.  A future development might be to chain the 'ConfigParser' implicitly with a state monad, which would be yet more elegant.

-}

simpleAccess ::  MonadError CPError m =>
                 SectionSpec -> OptionSpec -> ConfigParser -> m String
simpleAccess :: SectionSpec -> SectionSpec -> ConfigParser -> m SectionSpec
simpleAccess SectionSpec
s SectionSpec
o ConfigParser
cp = ConfigParser -> SectionSpec -> SectionSpec -> m SectionSpec
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> SectionSpec -> m SectionSpec
C.simpleAccess ConfigParser
cp SectionSpec
s SectionSpec
o

interpolatingAccess :: MonadError CPError m =>
                       Int ->
                       SectionSpec -> OptionSpec -> ConfigParser
                       -> m String
interpolatingAccess :: Int -> SectionSpec -> SectionSpec -> ConfigParser -> m SectionSpec
interpolatingAccess Int
maxdepth SectionSpec
s SectionSpec
o ConfigParser
cp = Int -> ConfigParser -> SectionSpec -> SectionSpec -> m SectionSpec
forall (m :: * -> *).
MonadError CPError m =>
Int -> ConfigParser -> SectionSpec -> SectionSpec -> m SectionSpec
C.interpolatingAccess Int
maxdepth ConfigParser
cp SectionSpec
s SectionSpec
o

readfile :: MonadError CPError m => FilePath -> ConfigParser -> IO (m ConfigParser)
readfile :: SectionSpec -> ConfigParser -> IO (m ConfigParser)
readfile SectionSpec
fp ConfigParser
cp = ConfigParser -> SectionSpec -> IO (m ConfigParser)
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> IO (m ConfigParser)
C.readfile ConfigParser
cp SectionSpec
fp

readhandle :: MonadError CPError m => Handle -> ConfigParser -> IO (m ConfigParser)
readhandle :: Handle -> ConfigParser -> IO (m ConfigParser)
readhandle Handle
h ConfigParser
cp = ConfigParser -> Handle -> IO (m ConfigParser)
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> Handle -> IO (m ConfigParser)
C.readhandle ConfigParser
cp Handle
h

readstring ::  MonadError CPError m =>
               String -> ConfigParser -> m ConfigParser
readstring :: SectionSpec -> ConfigParser -> m ConfigParser
readstring SectionSpec
cp ConfigParser
s = ConfigParser -> SectionSpec -> m ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> m ConfigParser
C.readstring ConfigParser
s SectionSpec
cp

has_section :: SectionSpec -> ConfigParser -> Bool
has_section :: SectionSpec -> ConfigParser -> Bool
has_section SectionSpec
x ConfigParser
cp = ConfigParser -> SectionSpec -> Bool
C.has_section ConfigParser
cp SectionSpec
x

add_section :: MonadError CPError m =>
               SectionSpec -> ConfigParser -> m ConfigParser
add_section :: SectionSpec -> ConfigParser -> m ConfigParser
add_section SectionSpec
s ConfigParser
cp = ConfigParser -> SectionSpec -> m ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> m ConfigParser
C.add_section ConfigParser
cp SectionSpec
s

options ::  MonadError CPError m =>
            SectionSpec -> ConfigParser -> m [OptionSpec]
options :: SectionSpec -> ConfigParser -> m [SectionSpec]
options SectionSpec
x ConfigParser
cp = ConfigParser -> SectionSpec -> m [SectionSpec]
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> m [SectionSpec]
C.options ConfigParser
cp SectionSpec
x

has_option :: SectionSpec -> OptionSpec -> ConfigParser -> Bool
has_option :: SectionSpec -> SectionSpec -> ConfigParser -> Bool
has_option SectionSpec
s SectionSpec
o ConfigParser
cp = ConfigParser -> SectionSpec -> SectionSpec -> Bool
C.has_option ConfigParser
cp SectionSpec
s SectionSpec
o

items ::  MonadError CPError m =>
          SectionSpec -> ConfigParser -> m [(OptionSpec, String)]
items :: SectionSpec -> ConfigParser -> m [(SectionSpec, SectionSpec)]
items SectionSpec
s ConfigParser
cp = ConfigParser -> SectionSpec -> m [(SectionSpec, SectionSpec)]
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> m [(SectionSpec, SectionSpec)]
C.items ConfigParser
cp SectionSpec
s

set ::  MonadError CPError m =>
        SectionSpec -> OptionSpec -> String -> ConfigParser -> m ConfigParser
set :: SectionSpec
-> SectionSpec -> SectionSpec -> ConfigParser -> m ConfigParser
set SectionSpec
s SectionSpec
passedo SectionSpec
val ConfigParser
cp = ConfigParser
-> SectionSpec -> SectionSpec -> SectionSpec -> m ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser
-> SectionSpec -> SectionSpec -> SectionSpec -> m ConfigParser
C.set ConfigParser
cp SectionSpec
s SectionSpec
passedo SectionSpec
val

setshow :: (Show a, MonadError CPError m) =>
           SectionSpec -> OptionSpec -> a -> ConfigParser -> m ConfigParser
setshow :: SectionSpec -> SectionSpec -> a -> ConfigParser -> m ConfigParser
setshow SectionSpec
s SectionSpec
o a
val ConfigParser
cp = ConfigParser -> SectionSpec -> SectionSpec -> a -> m ConfigParser
forall a (m :: * -> *).
(Show a, MonadError CPError m) =>
ConfigParser -> SectionSpec -> SectionSpec -> a -> m ConfigParser
C.setshow ConfigParser
cp SectionSpec
s SectionSpec
o a
val

remove_option ::  MonadError CPError m =>
                  SectionSpec -> OptionSpec -> ConfigParser -> m ConfigParser
remove_option :: SectionSpec -> SectionSpec -> ConfigParser -> m ConfigParser
remove_option SectionSpec
s SectionSpec
passedo ConfigParser
cp = ConfigParser -> SectionSpec -> SectionSpec -> m ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> SectionSpec -> m ConfigParser
C.remove_option ConfigParser
cp SectionSpec
s SectionSpec
passedo


remove_section ::  MonadError CPError m =>
                   SectionSpec -> ConfigParser -> m ConfigParser
remove_section :: SectionSpec -> ConfigParser -> m ConfigParser
remove_section SectionSpec
s ConfigParser
cp = ConfigParser -> SectionSpec -> m ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> SectionSpec -> m ConfigParser
C.remove_section ConfigParser
cp SectionSpec
s