diff options
Diffstat (limited to 'Data')
-rw-r--r-- | Data/ConfigManager.hs | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/Data/ConfigManager.hs b/Data/ConfigManager.hs index 038276e..aa92554 100644 --- a/Data/ConfigManager.hs +++ b/Data/ConfigManager.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE OverloadedStrings #-} + -- | -- Module: Data.ConfigManager -- License: GPL-3 @@ -30,12 +32,10 @@ module Data.ConfigManager 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.HashMap.Strict as M -import Data.Maybe (fromMaybe) import qualified Data.ConfigManager.Reader as R import Data.ConfigManager.Types @@ -47,14 +47,21 @@ readConfig = R.readConfig Required -- | Lookup for the value associated to a name. -lookup :: Read a => Name -> Config -> Maybe a -lookup name config = join . fmap (readMaybe . T.unpack) $ M.lookup name (hashMap config) +lookup :: Read a => Name -> Config -> Either Text a +lookup name config = + case M.lookup name (hashMap config) of + Nothing -> + Left . T.concat $ ["Value not found for Key ", name] + Just value -> + case readMaybe . T.unpack $ value of + Nothing -> Left . T.concat $ ["Reading error for key ", name] + Just result -> Right result -- | Lookup for the value associated to a name and return the default value if -- no binding exists with the given name. lookupDefault :: Read a => a -> Name -> Config -> a -lookupDefault defaultValue name config = fromMaybe defaultValue $ lookup name config +lookupDefault defaultValue name config = foldl (flip const) defaultValue $ lookup name config -- $format -- |