aboutsummaryrefslogtreecommitdiff
path: root/Data/ConfigManager.hs
blob: d972cd8451f65feadb5833975c380baf311b2030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module Data.ConfigManager
  ( readConfig
  , lookup
  , lookupDefault
  ) where

import Prelude hiding (lookup)
import Text.Read (readMaybe)
import Control.Monad (join)

import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.HashMap.Strict as M

import Data.ConfigManager.Config
import Data.ConfigManager.Parser (parseConfig)
import Data.ConfigManager.Types

readConfig :: FilePath -> IO (Either Text Config)
readConfig path = parseConfig <$> T.readFile path

lookup :: Read a => Name -> Config -> Maybe a
lookup name config = join . fmap (readMaybe . T.unpack) $ M.lookup name (hashMap config)

lookupDefault :: Value -> Name -> Config -> Value
lookupDefault defaultValue name config = M.lookupDefault defaultValue name (hashMap config)