aboutsummaryrefslogtreecommitdiff
path: root/Data/ConfigManager.hs
diff options
context:
space:
mode:
authorJoris2016-03-20 22:04:11 +0100
committerJoris2016-03-20 22:04:11 +0100
commit5edfb35f6be596e9d91df6690b9977c783bb147b (patch)
tree537c7f62879884a57957d90cf0f284a1bff51745 /Data/ConfigManager.hs
parent28f8f4d52e72953ed8bdf1518a259c8b57ed3596 (diff)
Return Either instead of Maybe for lookup
Diffstat (limited to 'Data/ConfigManager.hs')
-rw-r--r--Data/ConfigManager.hs17
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
--