From 5edfb35f6be596e9d91df6690b9977c783bb147b Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 20 Mar 2016 22:04:11 +0100 Subject: Return Either instead of Maybe for lookup --- Data/ConfigManager.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'Data') 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 -- -- cgit v1.2.3