module Importify.ParseException
( ModuleParseException (..)
, eitherParseResult
, reportErrorsIfAny
, setMpeFile
) where
import Universum
import Fmt (Builder, blockListF, build, indentF, (+|), (|+))
import Language.Haskell.Exts (ParseResult (..), SrcLoc (srcFilename),
prettyPrint)
import Extended.Data.Str (charWrap, wordWrap)
import Extended.System.Wlog (printWarning)
data ModuleParseException = MPE !SrcLoc !String
deriving (Show)
instance Exception ModuleParseException
instance Buildable ModuleParseException where
build (MPE loc reason) = "Location:\n"
+| indentF 4 (build $ charWrap 80 $ prettyPrint loc)
|+ "Reason:\n"
+| indentF 4 (build $ wordWrap 80 reason)
|+ ""
setMpeFile :: FilePath -> ModuleParseException -> ModuleParseException
setMpeFile modulePath (MPE loc msg) = MPE (loc {srcFilename = modulePath}) msg
eitherParseResult :: ParseResult res
-> Either ModuleParseException res
eitherParseResult (ParseOk res) = Right res
eitherParseResult (ParseFailed loc reason) = Left $ MPE loc reason
prettyParseErrors :: Text -> NonEmpty ModuleParseException -> Text
prettyParseErrors libName exceptions =
"Next errors occured during caching of package: "+|libName|+"\n"
+| indentF 4 (blockListF exceptions) |+ ""
reportErrorsIfAny :: [ModuleParseException] -> Text -> IO ()
reportErrorsIfAny exceptions libName = whenNotNull exceptions $
printWarning . prettyParseErrors libName