module Data.Settings.Interface
(
queryR
, querySectionR
, queryOptionR
, updateOptionR
, resetOptionR
, query
, querySection
, queryOption
, updateOption
, resetOption
)
where
import Control.Monad (liftM)
import Data.HashMap.Lazy (keys)
import Data.Settings.Route
import Data.Settings.Section
import Data.Settings.Types
import Prelude hiding (lookup)
queryR :: MonadSettings m s
=> OptRoute
-> m (Either SettingsError (Either ([SecName], [OptName]) String))
queryR route = do
t <- getSTree
case lookup route t of
Just (Left sec) ->
return $ Right $ Left (keys $ secSubs sec, keys $ secOpts sec)
Just (Right opt) -> liftM (Right . Right) $ optGet opt
Nothing -> return . Left $ NoSuchNode route
querySectionR :: MonadSettings m s
=> OptRoute
-> m (Either SettingsError ([SecName], [OptName]))
querySectionR route = do
result <- queryR route
return $ case result of
Left err -> Left err
Right (Left secsOpts) -> Right secsOpts
Right (Right _val) -> Left $ NoSuchSection route
queryOptionR :: MonadSettings m s
=> OptRoute
-> m (Either SettingsError String)
queryOptionR route = do
result <- queryR route
return $ case result of
Left err -> Left err
Right (Left _secsOpts) -> Left $ NoSuchOption route
Right (Right val) -> Right val
updateOptionR :: MonadSettings m s
=> OptRoute
-> String
-> m (Maybe SettingsError)
updateOptionR route val = do
t <- getSTree
case lookupOpt route t of
Just opt -> optSet opt val
Nothing -> return $ Just $ NoSuchOption route
resetOptionR :: MonadSettings m s => OptRoute -> m (Maybe SettingsError)
resetOptionR route = do
t <- getSTree
case lookupOpt route t of
Just opt -> optReset opt >> return Nothing
Nothing -> return $ Just $ NoSuchOption route
byPathMaybe :: Monad m
=> (OptRoute -> m (Maybe SettingsError))
-> OptPath
-> m (Maybe SettingsError)
byPathMaybe f path =
case parseRoute path of
Just route -> f route
Nothing -> return $ Just $ InvalidPath path
byPathEither :: Monad m
=> (OptRoute -> m (Either SettingsError a))
-> OptPath
-> m (Either SettingsError a)
byPathEither f path =
case parseRoute path of
Just route -> f route
Nothing -> return $ Left $ InvalidPath path
query :: MonadSettings m s
=> OptPath
-> m (Either SettingsError (Either ([SecName], [OptName]) String))
query = byPathEither queryR
querySection :: MonadSettings m s
=> OptPath
-> m (Either SettingsError ([SecName], [OptName]))
querySection = byPathEither querySectionR
queryOption :: MonadSettings m s => OptPath -> m (Either SettingsError String)
queryOption = byPathEither queryOptionR
updateOption :: MonadSettings m s
=> OptPath
-> String
-> m (Maybe SettingsError)
updateOption path val = byPathMaybe (flip updateOptionR val) path
resetOption :: MonadSettings m s => OptPath -> m (Maybe SettingsError)
resetOption = byPathMaybe resetOptionR