{-# LANGUAGE OverloadedStrings    #-}
{- |
Copyright               : © 2021 Albert Krewinkel
SPDX-License-Identifier : MIT
Maintainer              : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>

Marshaling/unmarshaling functions of 'TableHead' values.
-}
module Text.Pandoc.Lua.Marshal.TableHead
  ( peekTableHead
  , pushTableHead
  , typeTableHead
  , mkTableHead
  ) where

import Control.Applicative (optional)
import Data.Maybe (fromMaybe)
import HsLua
import Text.Pandoc.Lua.Marshal.Attr (peekAttr, pushAttr)
import Text.Pandoc.Lua.Marshal.List (pushPandocList)
import Text.Pandoc.Lua.Marshal.Row (peekRowFuzzy, pushRow)
import Text.Pandoc.Definition

-- | Push a TableHead as a userdata value.
pushTableHead :: LuaError e => TableHead -> LuaE e ()
pushTableHead :: TableHead -> LuaE e ()
pushTableHead = UDTypeWithList e (DocumentedFunction e) TableHead Void
-> TableHead -> LuaE e ()
forall e fn a itemtype.
LuaError e =>
UDTypeWithList e fn a itemtype -> a -> LuaE e ()
pushUD UDTypeWithList e (DocumentedFunction e) TableHead Void
forall e. LuaError e => DocumentedType e TableHead
typeTableHead

-- | Retrieves a 'Cell' from the stack.
peekTableHead :: LuaError e => Peeker e TableHead
peekTableHead :: Peeker e TableHead
peekTableHead = UDTypeWithList e (DocumentedFunction e) TableHead Void
-> Peeker e TableHead
forall e fn a itemtype.
LuaError e =>
UDTypeWithList e fn a itemtype -> Peeker e a
peekUD UDTypeWithList e (DocumentedFunction e) TableHead Void
forall e. LuaError e => DocumentedType e TableHead
typeTableHead

-- | Row object type.
typeTableHead :: LuaError e => DocumentedType e TableHead
typeTableHead :: DocumentedType e TableHead
typeTableHead = Name
-> [(Operation, DocumentedFunction e)]
-> [Member e (DocumentedFunction e) TableHead]
-> DocumentedType e TableHead
forall e a.
LuaError e =>
Name
-> [(Operation, DocumentedFunction e)]
-> [Member e (DocumentedFunction e) a]
-> DocumentedType e a
deftype Name
"pandoc TableHead"
  [ 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
$ Name
-> (Maybe TableHead -> Maybe TableHead -> LuaE e Bool)
-> HsFnPrecursor
     e (Maybe TableHead -> Maybe TableHead -> LuaE e Bool)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"__eq"
     ### liftPure2 (\a b -> fromMaybe False ((==) <$> a <*> b))
     HsFnPrecursor e (Maybe TableHead -> Maybe TableHead -> LuaE e Bool)
-> Parameter e (Maybe TableHead)
-> HsFnPrecursor e (Maybe TableHead -> LuaE e Bool)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e (Maybe TableHead)
-> Text -> Text -> Text -> Parameter e (Maybe TableHead)
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (Peek e TableHead -> Peek e (Maybe TableHead)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peek e TableHead -> Peek e (Maybe TableHead))
-> (StackIndex -> Peek e TableHead) -> Peeker e (Maybe TableHead)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e TableHead
forall e. LuaError e => Peeker e TableHead
peekTableHead) Text
"TableHead" Text
"self" Text
""
     HsFnPrecursor e (Maybe TableHead -> LuaE e Bool)
-> Parameter e (Maybe TableHead) -> HsFnPrecursor e (LuaE e Bool)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e (Maybe TableHead)
-> Text -> Text -> Text -> Parameter e (Maybe TableHead)
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (Peek e TableHead -> Peek e (Maybe TableHead)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peek e TableHead -> Peek e (Maybe TableHead))
-> (StackIndex -> Peek e TableHead) -> Peeker e (Maybe TableHead)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e TableHead
forall e. LuaError e => Peeker e TableHead
peekTableHead) Text
"any" Text
"object" 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 -> Text -> Text -> FunctionResults e Bool
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e Bool
forall e. Pusher e Bool
pushBool Text
"boolean" Text
"true iff the two values are equal"
  , Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall e.
Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
operation Operation
Tostring (DocumentedFunction e -> (Operation, DocumentedFunction e))
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall a b. (a -> b) -> a -> b
$ (TableHead -> LuaE e String)
-> HsFnPrecursor e (TableHead -> LuaE e String)
forall a e. a -> HsFnPrecursor e a
lambda
    ### liftPure show
    HsFnPrecursor e (TableHead -> LuaE e String)
-> Parameter e TableHead -> HsFnPrecursor e (LuaE e String)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> (StackIndex -> Peek e TableHead)
-> Text -> Text -> Text -> Parameter e TableHead
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter StackIndex -> Peek e TableHead
forall e. LuaError e => Peeker e TableHead
peekTableHead Text
"TableHead" Text
"self" Text
""
    HsFnPrecursor e (LuaE e String)
-> FunctionResults e String -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e String -> Text -> Text -> FunctionResults e String
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e String
forall e. String -> LuaE e ()
pushString Text
"string" Text
"native Haskell representation"
  ]
  [ Name
-> Text
-> (Pusher e Attr, TableHead -> Attr)
-> (Peeker e Attr, TableHead -> Attr -> TableHead)
-> Member e (DocumentedFunction e) TableHead
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property Name
"attr" Text
"table head attributes"
      (Pusher e Attr
forall e. LuaError e => Pusher e Attr
pushAttr, \(TableHead Attr
attr [Row]
_) -> Attr
attr)
      (Peeker e Attr
forall e. LuaError e => Peeker e Attr
peekAttr, \(TableHead Attr
_ [Row]
cells) Attr
attr ->
                   Attr -> [Row] -> TableHead
TableHead Attr
attr [Row]
cells)
  , Name
-> Text
-> (Pusher e [Row], TableHead -> [Row])
-> (Peeker e [Row], TableHead -> [Row] -> TableHead)
-> Member e (DocumentedFunction e) TableHead
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property Name
"rows" Text
"header rows"
      (Pusher e Row -> Pusher e [Row]
forall e a. LuaError e => Pusher e a -> Pusher e [a]
pushPandocList Pusher e Row
forall e. LuaError e => Row -> LuaE e ()
pushRow, \(TableHead Attr
_ [Row]
rows) -> [Row]
rows)
      (Peeker e Row -> Peeker e [Row]
forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList Peeker e Row
forall e. LuaError e => Peeker e Row
peekRowFuzzy, \(TableHead Attr
attr [Row]
_) [Row]
rows ->
                                Attr -> [Row] -> TableHead
TableHead Attr
attr [Row]
rows)

  , Name
-> Text
-> [AliasIndex]
-> Member e (DocumentedFunction e) TableHead
forall e fn a. Name -> Text -> [AliasIndex] -> Member e fn a
alias Name
"identifier" Text
"cell ID"         [AliasIndex
"attr", AliasIndex
"identifier"]
  , Name
-> Text
-> [AliasIndex]
-> Member e (DocumentedFunction e) TableHead
forall e fn a. Name -> Text -> [AliasIndex] -> Member e fn a
alias Name
"classes"    Text
"cell classes"    [AliasIndex
"attr", AliasIndex
"classes"]
  , Name
-> Text
-> [AliasIndex]
-> Member e (DocumentedFunction e) TableHead
forall e fn a. Name -> Text -> [AliasIndex] -> Member e fn a
alias Name
"attributes" Text
"cell attributes" [AliasIndex
"attr", AliasIndex
"attributes"]

  , DocumentedFunction e -> Member e (DocumentedFunction e) TableHead
forall e a.
DocumentedFunction e -> Member e (DocumentedFunction e) a
method (DocumentedFunction e -> Member e (DocumentedFunction e) TableHead)
-> DocumentedFunction e
-> Member e (DocumentedFunction e) TableHead
forall a b. (a -> b) -> a -> b
$ Name
-> (TableHead -> LuaE e TableHead)
-> HsFnPrecursor e (TableHead -> LuaE e TableHead)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"clone"
    ### return
    HsFnPrecursor e (TableHead -> LuaE e TableHead)
-> Parameter e TableHead -> HsFnPrecursor e (LuaE e TableHead)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> (StackIndex -> Peek e TableHead)
-> Text -> Text -> Text -> Parameter e TableHead
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter StackIndex -> Peek e TableHead
forall e. LuaError e => Peeker e TableHead
peekTableHead Text
"TableHead" Text
"self" Text
""
    HsFnPrecursor e (LuaE e TableHead)
-> FunctionResults e TableHead -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e TableHead -> Text -> Text -> FunctionResults e TableHead
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e TableHead
forall e. LuaError e => TableHead -> LuaE e ()
pushTableHead Text
"TableHead" Text
"cloned object"
  ]

-- | Constructor function for 'Row' values.
mkTableHead :: LuaError e => DocumentedFunction e
mkTableHead :: DocumentedFunction e
mkTableHead = Name
-> (Maybe [Row] -> Maybe Attr -> LuaE e TableHead)
-> HsFnPrecursor e (Maybe [Row] -> Maybe Attr -> LuaE e TableHead)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"TableHead"
  ### liftPure2 (\mRows mAttr -> TableHead
                  (fromMaybe nullAttr mAttr)
                  (fromMaybe [] mRows))
  HsFnPrecursor e (Maybe [Row] -> Maybe Attr -> LuaE e TableHead)
-> Parameter e (Maybe [Row])
-> HsFnPrecursor e (Maybe Attr -> LuaE e TableHead)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e [Row] -> Text -> Text -> Text -> Parameter e (Maybe [Row])
forall e a.
Peeker e a -> Text -> Text -> Text -> Parameter e (Maybe a)
optionalParameter (Peeker e Row -> Peeker e [Row]
forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList Peeker e Row
forall e. LuaError e => Peeker e Row
peekRowFuzzy) Text
"{Row,...}" Text
"rows"
        Text
"header rows"
  HsFnPrecursor e (Maybe Attr -> LuaE e TableHead)
-> Parameter e (Maybe Attr) -> HsFnPrecursor e (LuaE e TableHead)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e Attr -> Text -> Text -> Text -> Parameter e (Maybe Attr)
forall e a.
Peeker e a -> Text -> Text -> Text -> Parameter e (Maybe a)
optionalParameter Peeker e Attr
forall e. LuaError e => Peeker e Attr
peekAttr Text
"Attr" Text
"attr" Text
"table head attributes"
  HsFnPrecursor e (LuaE e TableHead)
-> FunctionResults e TableHead -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e TableHead -> Text -> Text -> FunctionResults e TableHead
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e TableHead
forall e. LuaError e => TableHead -> LuaE e ()
pushTableHead Text
"TableHead" Text
"new TableHead object"