some error handling and cleanup that comes with it

This commit is contained in:
nerf van nerfingen 2022-11-12 16:18:11 +01:00
parent 99ab66b419
commit 616fc48990
7 changed files with 91 additions and 31 deletions

View file

@ -4,6 +4,7 @@ module Config where
import qualified Toml
import Toml(TomlCodec, (.=))
import qualified Data.Text as T
import Monad
data Config = Config {
mailDomain :: String
@ -11,6 +12,7 @@ data Config = Config {
,mailPassword :: String
,mailTo :: T.Text
,mailFrom :: T.Text
,mailErrorTo :: T.Text
} deriving Show
configCodec :: TomlCodec Config
@ -20,10 +22,11 @@ configCodec = Config
<*> Toml.string "mailPassword" .= mailPassword
<*> Toml.text "mailTo" .= mailTo
<*> Toml.text "mailFrom" .= mailFrom
<*> Toml.text "mailErrorTo" .= mailErrorTo
parseFile :: String -> IO (Either String Config)
parseFile path = do
config <- Toml.decodeFileEither configCodec path
parseConfigFile :: (MonadIO m, MonadFail m) => String -> m Config
parseConfigFile path = do
config <- liftIO $ Toml.decodeFileEither configCodec path
case config of
Left errors -> return $ Left $ unwords $ fmap show errors
Right x -> return $ Right $ x
Left errors -> fail $ unwords $ fmap show errors
Right x -> return x