choirMail/app/Config.hs

33 lines
931 B
Haskell
Raw Normal View History

2022-11-08 20:16:34 +01:00
{-# LANGUAGE OverloadedStrings #-}
module Config where
import qualified Toml
import Toml(TomlCodec, (.=))
import qualified Data.Text as T
import Monad
2022-11-08 20:16:34 +01:00
data Config = Config {
mailDomain :: String
,mailUsername :: String
,mailPassword :: String
,mailTo :: T.Text
,mailFrom :: T.Text
,mailErrorTo :: T.Text
2022-11-08 20:16:34 +01:00
} deriving Show
configCodec :: TomlCodec Config
configCodec = Config
<$> Toml.string "mailDomain" .= mailDomain
<*> Toml.string "mailUser" .= mailUsername
<*> Toml.string "mailPassword" .= mailPassword
<*> Toml.text "mailTo" .= mailTo
<*> Toml.text "mailFrom" .= mailFrom
<*> Toml.text "mailErrorTo" .= mailErrorTo
2022-11-08 20:16:34 +01:00
parseConfigFile :: (MonadIO m, MonadFail m) => String -> m Config
parseConfigFile path = do
config <- liftIO $ Toml.decodeFileEither configCodec path
2022-11-08 20:16:34 +01:00
case config of
Left errors -> fail $ unwords $ fmap show errors
Right x -> return x