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

Marshaling/unmarshaling functions of types that are used exclusively
with tables.
-}
module Text.Pandoc.Lua.Marshal.TableParts
  ( peekColSpec
  , pushColSpec
  , peekRow
  , peekRowFuzzy
  , pushRow
  , peekTableBody
  , peekTableBodyFuzzy
  , pushTableBody
  , peekTableFoot
  , pushTableFoot
  , peekTableHead
  , pushTableHead
    -- * Constructors
  , mkRow
  , mkTableBody
  , mkTableFoot
  , mkTableHead
  ) where

import Control.Applicative (optional)
import Control.Monad ((<$!>))
import HsLua
import Text.Pandoc.Lua.Marshal.Alignment (peekAlignment, pushAlignment)
import Text.Pandoc.Lua.Marshal.Row
import Text.Pandoc.Lua.Marshal.TableBody
import Text.Pandoc.Lua.Marshal.TableFoot
import Text.Pandoc.Lua.Marshal.TableHead
import Text.Pandoc.Definition

-- | Push a ColSpec value as a pair of Alignment and ColWidth.
pushColSpec :: LuaError e => Pusher e ColSpec
pushColSpec :: forall e. LuaError e => Pusher e ColSpec
pushColSpec = Pusher e Alignment -> Pusher e ColWidth -> ColSpec -> LuaE e ()
forall e a b.
LuaError e =>
Pusher e a -> Pusher e b -> (a, b) -> LuaE e ()
pushPair Pusher e Alignment
forall e. Pusher e Alignment
pushAlignment Pusher e ColWidth
forall e. LuaError e => Pusher e ColWidth
pushColWidth

-- | Peek a ColSpec value as a pair of Alignment and ColWidth.
peekColSpec :: LuaError e => Peeker e ColSpec
peekColSpec :: forall e. LuaError e => Peeker e ColSpec
peekColSpec = Peeker e Alignment -> Peeker e ColWidth -> Peeker e ColSpec
forall e a b.
LuaError e =>
Peeker e a -> Peeker e b -> Peeker e (a, b)
peekPair Peeker e Alignment
forall e. Peeker e Alignment
peekAlignment Peeker e ColWidth
forall e. Peeker e ColWidth
peekColWidth

peekColWidth :: Peeker e ColWidth
peekColWidth :: forall e. Peeker e ColWidth
peekColWidth = Name -> Peek e ColWidth -> Peek e ColWidth
forall e a. Name -> Peek e a -> Peek e a
retrieving Name
"ColWidth" (Peek e ColWidth -> Peek e ColWidth)
-> (StackIndex -> Peek e ColWidth) -> StackIndex -> Peek e ColWidth
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \StackIndex
idx -> do
  ColWidth -> (Double -> ColWidth) -> Maybe Double -> ColWidth
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ColWidth
ColWidthDefault Double -> ColWidth
ColWidth (Maybe Double -> ColWidth)
-> Peek e (Maybe Double) -> Peek e ColWidth
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Peek e Double -> Peek e (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peeker e Double
forall a e. (RealFloat a, Read a) => Peeker e a
peekRealFloat StackIndex
idx)

-- | Push a ColWidth value by pushing the width as a plain number, or
-- @nil@ for ColWidthDefault.
pushColWidth :: LuaError e => Pusher e ColWidth
pushColWidth :: forall e. LuaError e => Pusher e ColWidth
pushColWidth = \case
  (ColWidth Double
w)    -> Double -> LuaE e ()
forall a e. (Pushable a, LuaError e) => a -> LuaE e ()
forall e. LuaError e => Double -> LuaE e ()
push Double
w
  ColWidth
ColWidthDefault -> LuaE e ()
forall e. LuaE e ()
pushnil