module Data.Boltzmann.Internal.Annotations
( withDefault
, withString
, withDouble
, withInt
, withBool
) where
import Data.Maybe (fromMaybe)
import Data.Char (toLower)
import Data.Map (Map)
import qualified Data.Map as M
import Text.Read (readMaybe)
withDefault :: Read a
=> Map String String
-> String -> a -> a
withDefault f x d = case x `M.lookup` f of
Nothing -> d
Just x' -> fromMaybe d (readMaybe x')
withString :: Map String String
-> String -> String -> String
withString f x d = fromMaybe d (x `M.lookup` f)
withDouble :: Map String String
-> String -> Double -> Double
withDouble = withDefault
withInt :: Map String String
-> String -> Int -> Int
withInt = withDefault
withBool :: Map String String
-> String -> Bool -> Bool
withBool f x d = case x `M.lookup` f of
Nothing -> d
Just x' -> case map toLower x' of
"yes" -> True
"y" -> True
"1" -> True
"true" -> True
"no" -> False
"n" -> False
"false" -> False
"0" -> False
_ -> d