    {-# LANGUAGE OverloadedStrings    #-}
    {- |
       Module      : Text.Pandoc.Lua.Marshal.LogMessage
       Copyright   : © 2017-2024 Albert Krewinkel
       License     : GPL-2.0-or-later
       Maintainer  : Albert Krewinkel <albert+pandoc@tarleb.com>

    Pushing and retrieving of pandoc log messages.
    -}
    module Text.Pandoc.Lua.Marshal.LogMessage
      ( peekLogMessage
      , pushLogMessage
      , typeLogMessage
      ) where

    import Control.Applicative (optional)
    import Data.Maybe (fromMaybe)
    import HsLua
    import Text.Pandoc.Logging (LogMessage, showLogMessage)
    import qualified Data.Aeson as Aeson

    -- | Type definition for pandoc log messages.
    typeLogMessage :: LuaError e => DocumentedType e LogMessage
    typeLogMessage :: forall e. LuaError e => DocumentedType e LogMessage
    typeLogMessage = Name
    -> [(Operation, DocumentedFunction e)]
    -> [Member e (DocumentedFunction e) LogMessage]
    -> DocumentedType e LogMessage
    forall e a.
    LuaError e =>
    Name
    -> [(Operation, DocumentedFunction e)]
    -> [Member e (DocumentedFunction e) a]
    -> DocumentedType e a
    deftype Name
    "LogMessage"
      [ Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation Operation
    Index (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ Name
    -> (LogMessage -> LuaE e Text)
    -> HsFnPrecursor e (LogMessage -> LuaE e Text)
    forall a e. Name -> a -> HsFnPrecursor e a
    defun Name
    "__tostring"
          ### liftPure showLogMessage
          HsFnPrecursor e (LogMessage -> LuaE e Text)
    -> Parameter e LogMessage -> HsFnPrecursor e (LuaE e Text)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> DocumentedType e LogMessage
    -> Text -> Text -> Parameter e LogMessage
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype
    -> Text -> Text -> Parameter e a
    udparam DocumentedType e LogMessage
    forall e. LuaError e => DocumentedType e LogMessage
    typeLogMessage Text
    "msg" Text
    "object"
          HsFnPrecursor e (LuaE e Text)
    -> FunctionResults e Text -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e Text -> TypeSpec -> Text -> FunctionResults e Text
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e Text
    forall e. Pusher e Text
    pushText TypeSpec
    "string" Text
    "stringified log message"
      , Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation Operation
    Eq (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ (Maybe LogMessage -> Maybe LogMessage -> LuaE e Bool)
    -> HsFnPrecursor
         e (Maybe LogMessage -> Maybe LogMessage -> LuaE e Bool)
    forall a e. a -> HsFnPrecursor e a
    lambda
          ### liftPure2 (\a b -> fromMaybe False ((==) <$> a <*> b))
          HsFnPrecursor
      e (Maybe LogMessage -> Maybe LogMessage -> LuaE e Bool)
    -> Parameter e (Maybe LogMessage)
    -> HsFnPrecursor e (Maybe LogMessage -> LuaE e Bool)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Peeker e (Maybe LogMessage)
    -> TypeSpec -> Text -> Text -> Parameter e (Maybe LogMessage)
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter (Peek e LogMessage -> Peek e (Maybe LogMessage)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (Peek e LogMessage -> Peek e (Maybe LogMessage))
    -> (StackIndex -> Peek e LogMessage) -> Peeker e (Maybe LogMessage)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StackIndex -> Peek e LogMessage
    forall e. LuaError e => Peeker e LogMessage
    peekLogMessage) TypeSpec
    "a" Text
    "LogMessage" Text
    ""
          HsFnPrecursor e (Maybe LogMessage -> LuaE e Bool)
    -> Parameter e (Maybe LogMessage) -> HsFnPrecursor e (LuaE e Bool)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> Peeker e (Maybe LogMessage)
    -> TypeSpec -> Text -> Text -> Parameter e (Maybe LogMessage)
    forall e a. Peeker e a -> TypeSpec -> Text -> Text -> Parameter e a
    parameter (Peek e LogMessage -> Peek e (Maybe LogMessage)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (Peek e LogMessage -> Peek e (Maybe LogMessage))
    -> (StackIndex -> Peek e LogMessage) -> Peeker e (Maybe LogMessage)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StackIndex -> Peek e LogMessage
    forall e. LuaError e => Peeker e LogMessage
    peekLogMessage) TypeSpec
    "b" Text
    "LogMessage" Text
    ""
          HsFnPrecursor e (LuaE e Bool)
    -> FunctionResults e Bool -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e Bool -> TypeSpec -> Text -> FunctionResults e Bool
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e Bool
    forall e. Pusher e Bool
    pushBool TypeSpec
    "boolean" Text
    "whether the two are equal"
      , Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall e.
    Operation
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    operation (Name -> Operation
    CustomOperation Name
    "__tojson") (DocumentedFunction e -> (Operation, DocumentedFunction e))
    -> DocumentedFunction e -> (Operation, DocumentedFunction e)
    forall a b. (a -> b) -> a -> b
    $ (LogMessage -> LuaE e ByteString)
    -> HsFnPrecursor e (LogMessage -> LuaE e ByteString)
    forall a e. a -> HsFnPrecursor e a
    lambda
          ### liftPure Aeson.encode
          HsFnPrecursor e (LogMessage -> LuaE e ByteString)
    -> Parameter e LogMessage -> HsFnPrecursor e (LuaE e ByteString)
    forall e a b.
    HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
    <#> DocumentedType e LogMessage
    -> Text -> Text -> Parameter e LogMessage
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype
    -> Text -> Text -> Parameter e a
    udparam DocumentedType e LogMessage
    forall e. LuaError e => DocumentedType e LogMessage
    typeLogMessage Text
    "msg" Text
    "object"
          HsFnPrecursor e (LuaE e ByteString)
    -> FunctionResults e ByteString -> DocumentedFunction e
    forall e a.
    HsFnPrecursor e (LuaE e a)
    -> FunctionResults e a -> DocumentedFunction e
    =#> Pusher e ByteString
    -> TypeSpec -> Text -> FunctionResults e ByteString
    forall e a. Pusher e a -> TypeSpec -> Text -> FunctionResults e a
    functionResult Pusher e ByteString
    forall e. Pusher e ByteString
    pushLazyByteString TypeSpec
    "string" Text
    "JSON encoded object"
      ]
      [Member e (DocumentedFunction e) LogMessage]
    forall a. Monoid a => a
    mempty -- no members

    -- | Pushes a LogMessage to the stack.
    pushLogMessage :: LuaError e => Pusher e LogMessage
    pushLogMessage :: forall e. LuaError e => Pusher e LogMessage
    pushLogMessage = DocumentedTypeWithList e LogMessage (ZonkAny 3)
    -> LogMessage -> LuaE e ()
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype -> a -> LuaE e ()
    pushUD DocumentedTypeWithList e LogMessage (ZonkAny 3)
    forall e. LuaError e => DocumentedType e LogMessage
    typeLogMessage

    peekLogMessage :: LuaError e => Peeker e LogMessage
    peekLogMessage :: forall e. LuaError e => Peeker e LogMessage
    peekLogMessage  = DocumentedTypeWithList e LogMessage (ZonkAny 0)
    -> Peeker e LogMessage
    forall e a itemtype.
    LuaError e =>
    DocumentedTypeWithList e a itemtype -> Peeker e a
    peekUD DocumentedTypeWithList e LogMessage (ZonkAny 0)
    forall e. LuaError e => DocumentedType e LogMessage
    typeLogMessage