Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions liftPandocPure and liftPandocIO, with a HasPandocError class #5908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Text/Pandoc/Class.hs
Expand Up @@ -73,6 +73,9 @@ module Text.Pandoc.Class ( PandocMonad(..)
, setTranslations
, translateTerm
, Translations
, HasPandocError(fromPandocError)
, liftPandocPure
, liftPandocIO
) where

import Prelude
Expand Down Expand Up @@ -1079,3 +1082,25 @@ instance {-# OVERLAPS #-} PandocMonad m => PandocMonad (ParsecT s st m) where
else "")
(return ())
logOutput = lift . logOutput

-- | Class for lifting 'PandocError' into some wrapper error type e.
class HasPandocError e where fromPandocError :: PandocError -> e
instance HasPandocError PandocError where fromPandocError = id

-- | Run a 'PandocPure' instance and lift the result into some monad
-- whose error type is an instance of 'HasPandocError'.
liftPandocPure ::
forall e m a. (MonadError e m, HasPandocError e)
=> PandocPure a
-> m a
liftPandocPure =
either (throwError . fromPandocError) return . runPure

-- | Run a 'PandocIO' instance and lift the result into some monad
-- whose error type is an instance of 'HasPandocError'.
liftPandocIO ::
forall e m a. (MonadIO m, MonadError e m, HasPandocError e)
=> PandocIO a
-> m a
liftPandocIO io =
liftIO (runIO io) >>= either (throwError . fromPandocError) return