Pure parsing of Dhall file without IO

Dhall’s input function requires IO so as to be able to resolve imports. However, it is not uncommon when using Dhall for simple application configuration to not want to support imports in application configuration1.

Here’s2 the pure parser for Dhall files, that works similar to Dhall.input Dhall.auto but without imports:

-- | Pure version of `Dhall.input Dhall.auto`
--
-- The config file cannot have imports, as that requires IO.
parsePure :: forall a. FromDhall a => FilePath -> Text -> Either String a
parsePure fn s = do
  expr0 <- first show $ Dhall.Parser.exprFromText fn s
  expr <- maybeToRight "Cannot have imports" $ traverse (const Nothing) expr0
  void $ first show $ Dhall.TypeCheck.typeOf expr
  first show
    $ validationToEither
    $ Dhall.extract @a Dhall.auto
    $ Dhall.Core.normalize expr
  1. It is especially important, for security reasons, to prevent IO when parsing user-submitted Dhall text in the backend.

    ↩︎
  2. See this forum post for background discussion.

    ↩︎