{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE LambdaCase #-}
{- |
   Module      : Text.Pandoc.Lua.ErrorConversion
   Copyright   : © 2020-2021 Albert Krewinkel
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
   Stability   : alpha

Define how Lua errors are converted into @'PandocError'@ Haskell
exceptions, and /vice versa/.
-}
module Text.Pandoc.Lua.ErrorConversion
  ( addContextToException
  ) where

import HsLua (LuaError, LuaE, top)
import HsLua.Marshalling (resultToEither, runPeek)
import HsLua.Class.Peekable (PeekError (..))
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Lua.Marshaling.PandocError (pushPandocError, peekPandocError)

import qualified Data.Text as T
import qualified HsLua as Lua

addContextToException :: ()
addContextToException :: ()
addContextToException = ()
forall a. HasCallStack => a
undefined

-- | Retrieve a @'PandocError'@ from the Lua stack.
popPandocError :: LuaE PandocError PandocError
popPandocError :: LuaE PandocError PandocError
popPandocError = do
  Result PandocError
errResult <- Peek PandocError PandocError
-> LuaE PandocError (Result PandocError)
forall e a. Peek e a -> LuaE e (Result a)
runPeek (Peek PandocError PandocError
 -> LuaE PandocError (Result PandocError))
-> Peek PandocError PandocError
-> LuaE PandocError (Result PandocError)
forall a b. (a -> b) -> a -> b
$ Peeker PandocError PandocError
forall e. LuaError e => Peeker e PandocError
peekPandocError StackIndex
top
  case Result PandocError -> Either String PandocError
forall a. Result a -> Either String a
resultToEither Result PandocError
errResult of
    Right PandocError
x -> PandocError -> LuaE PandocError PandocError
forall (m :: * -> *) a. Monad m => a -> m a
return PandocError
x
    Left String
err -> PandocError -> LuaE PandocError PandocError
forall (m :: * -> *) a. Monad m => a -> m a
return (PandocError -> LuaE PandocError PandocError)
-> PandocError -> LuaE PandocError PandocError
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocLuaError (String -> Text
T.pack String
err)

-- Ensure conversions between Lua errors and 'PandocError' exceptions
-- are possible.
instance LuaError PandocError where
  popException :: LuaE PandocError PandocError
popException = LuaE PandocError PandocError
popPandocError
  pushException :: PandocError -> LuaE PandocError ()
pushException = PandocError -> LuaE PandocError ()
forall e. LuaError e => Pusher e PandocError
pushPandocError
  luaException :: String -> PandocError
luaException = Text -> PandocError
PandocLuaError (Text -> PandocError) -> (String -> Text) -> String -> PandocError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

instance PeekError PandocError where
  messageFromException :: PandocError -> String
messageFromException = \case
    PandocLuaError Text
m -> Text -> String
T.unpack Text
m
    PandocError
err -> PandocError -> String
forall a. Show a => a -> String
show PandocError
err