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

Conversation

ddssff
Copy link

@ddssff ddssff commented Nov 14, 2019

I'm not sure whether these are generally useful, or they just fit into a design pattern I've been using.

@jgm
Copy link
Owner

jgm commented Nov 14, 2019

Could you give a little explanation of how you use these (and maybe an example)?

@ddssff
Copy link
Author

ddssff commented Nov 14, 2019

Generally speaking, if you are building a system of any size you start to accumulate a lot of error types, and these might turn into a heirarchy, where a FileError type might have one constructor to hold an IOException, another to hold a PandocError, and so on. To keep the code in different parts of the system uncoupled, you give each subsystem's error type a class such as

class HasPandocError e where fromPandocError :: PandocError -> e

Now your pandoc code can throw an error of some type it doesn't know about, as long as that error type has a HasPandocError instance.

The idea of liftPandocPure and liftPandocIO is to use the fromPandocError function to transform the error type, which in the pandoc library is always PandocError. So if I want to call pandoc from inside a LaTeXT monad I can say

latexify :: AppAbbrevs s e m => Markup -> LaTeXT m ()
latexify x = latexFromMarkup (liftPandocPure . pandocFromMarkdown) x

pandocFromMarkdown :: PandocMonad m => Text -> m Pandoc
pandocFromMarkdown t = softBreakToLineBreak <$> P.readMarkdown readOptsForMarkdown (removeCarriageReturns t)

latexFromMarkup :: AppAbbrevs s e m => (Text -> LaTeXT m Pandoc) -> Markup -> LaTeXT m ()
...

So pandocFromMarkdown gets lifted into LaTeXT m.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants