xc-jp / purescript-protobuf

Google Protocol Buffers for PureScript

Home Page:http://pursuit.purescript.org/packages/purescript-protobuf/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

parseTry

jamesdbrock opened this issue · comments

import Control.Monad.Error.Class(class MonadError, try)

-- | Try a `MonadError` in the context of a `ParserT` monad. Will transform
-- | a `MonadError String` into a `ParseError` in the `ParserT`
-- | monad, with parse failure position. Useful instances of `MonadError String`
-- | include `ExceptT String` and `Either String`.
-- |
-- | The motivation behind this function is
-- | [Parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/).
-- | After parsing a Protobuf message off of the wire, don't immediately return
-- | it out of `runParserT`. Rather, stay in the `ParserT` monad and
-- | build the final data structure that you want from information in the
-- | Protobuf message, while checking to make sure that the Protobuf message
-- | has all of the information that you need. Then return the final data
-- | structure.
parseTry :: forall s t m a. Monad m => MonadError String t => t a -> ParserT s m a
parseTry f = lift (try f) >>= case _ of
  Left err -> fail err
  Right x -> pure x

ParserT is already a MonadError. How useful is this really?