choirMail/app/Config.hs
2022-11-14 12:13:42 +01:00

32 lines
922 B
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Config where
import qualified Toml
import Toml(TomlCodec, (.=))
import qualified Data.Text as T
import Monad
data Config = Config {
mailDomain :: String
,mailUsername :: String
,mailPassword :: String
,mailTo :: T.Text
,mailFrom :: T.Text
,mailErrorTo :: T.Text
} deriving Show
configCodec :: TomlCodec Config
configCodec = table "mail" $ Config
<$> Toml.string "domain" .= mailDomain
<*> Toml.string "user" .= mailUsername
<*> Toml.string "password" .= mailPassword
<*> Toml.text "to" .= mailTo
<*> Toml.text "from" .= mailFrom
<*> Toml.text "errorTo" .= mailErrorTo
parseConfigFile :: (MonadIO m, MonadFail m) => String -> m Config
parseConfigFile path = do
config <- liftIO $ Toml.decodeFileEither configCodec path
case config of
Left errors -> fail $ unwords $ fmap show errors
Right x -> return x