choirMail/app/Config.hs

33 lines
932 B
Haskell
Raw Permalink 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
2022-11-14 12:25:00 +01:00
configCodec = flip Toml.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
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