module Game.LambdaHack.Config
( CP(..), toCP, forceEither, getOption, get, getItems
) where
import qualified Data.ConfigFile as CF
import qualified Data.Binary as Binary
import Game.LambdaHack.Utils.Assert
newtype CP = CP CF.ConfigParser
instance Binary.Binary CP where
put (CP conf) = Binary.put $ CF.to_string conf
get = do
string <- Binary.get
let c = CF.readstring CF.emptyCP string
return $ toCP $ forceEither c
instance Show CP where
show (CP conf) = show $ CF.to_string conf
toCP :: CF.ConfigParser -> CP
toCP cf = CP $ cf {CF.optionxform = id}
forceEither :: Show a => Either a b -> b
forceEither (Left a) = assert `failure` a
forceEither (Right b) = b
getOption :: CF.Get_C a => CP -> CF.SectionSpec -> CF.OptionSpec -> Maybe a
getOption (CP conf) s o =
if CF.has_option conf s o
then Just $ forceEither $ CF.get conf s o
else Nothing
get :: CF.Get_C a => CP -> CF.SectionSpec -> CF.OptionSpec -> a
get (CP conf) s o =
if CF.has_option conf s o
then forceEither $ CF.get conf s o
else assert `failure` "Unknown config option: " ++ s ++ "." ++ o
getItems :: CP -> CF.SectionSpec -> [(String, String)]
getItems (CP conf) s =
if CF.has_section conf s
then forceEither $ CF.items conf s
else assert `failure` "Unknown config section: " ++ s