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
|
2022-11-12 16:18:11 +01:00
|
|
|
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
|
2022-11-12 16:18:11 +01:00
|
|
|
,mailErrorTo :: T.Text
|
2022-11-08 20:16:34 +01:00
|
|
|
} deriving Show
|
|
|
|
|
|
|
|
configCodec :: TomlCodec Config
|
2022-11-14 12:25:00 +01:00
|
|
|
configCodec = flip Toml.table "mail" $ Config
|
2022-11-14 12:13:42 +01:00
|
|
|
<$> Toml.string "domain" .= mailDomain
|
|
|
|
<*> Toml.string "user" .= mailUsername
|
|
|
|
<*> Toml.string "password" .= mailPassword
|
|
|
|
<*> Toml.text "to" .= mailTo
|
|
|
|
<*> Toml.text "from" .= mailFrom
|
|
|
|
<*> Toml.text "errorTo" .= mailErrorTo
|
2022-11-08 20:16:34 +01:00
|
|
|
|
2022-11-12 16:18:11 +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
|
2022-11-12 16:18:11 +01:00
|
|
|
Left errors -> fail $ unwords $ fmap show errors
|
|
|
|
Right x -> return x
|