Skip to content

Commit

Permalink
Improve error message for retrieval of Inlines, Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Jun 10, 2022
1 parent e7cc2cd commit 970b413
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

`pandoc-lua-marshal` uses [PVP Versioning][].

## 0.1.6.1

Released 2022-06-10.

- Provide better error messages when fuzzy retrieval of Inlines
or Blocks fails.

- Relax upper bound for text, allow text-2.0.

## 0.1.6

Released 2022-06-03.
Expand Down
18 changes: 10 additions & 8 deletions src/Text/Pandoc/Lua/Marshal/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,22 @@ pushBlocks xs = do
-- bare strings as @Str@ values.
peekBlockFuzzy :: LuaError e
=> Peeker e Block
peekBlockFuzzy = choice
[ peekBlock
, \idx -> Plain <$!> peekInlinesFuzzy idx
]
peekBlockFuzzy idx =
peekBlock idx
<|> (Plain <$!> peekInlinesFuzzy idx)
<|> (failPeek =<<
typeMismatchMessage "Block or list of Inlines" idx)
{-# INLINABLE peekBlockFuzzy #-}

-- | Try extra-hard to return the value at the given index as a list of
-- inlines.
peekBlocksFuzzy :: LuaError e
=> Peeker e [Block]
peekBlocksFuzzy = choice
[ peekList peekBlockFuzzy
, (<$!>) pure . peekBlockFuzzy
]
peekBlocksFuzzy idx =
peekList peekBlockFuzzy idx
<|> (pure <$!> peekBlockFuzzy idx)
<|> (failPeek =<<
typeMismatchMessage "Block, list of Blocks, or compatible element" idx)
{-# INLINABLE peekBlocksFuzzy #-}

-- | Block object type.
Expand Down
10 changes: 5 additions & 5 deletions src/Text/Pandoc/Lua/Marshal/Inline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Text.Pandoc.Lua.Marshal.Inline
, walkInlinesStraight
) where

import Control.Applicative (optional)
import Control.Applicative ((<|>), optional)
import Control.Monad.Catch (throwM)
import Control.Monad ((<$!>))
import Data.Data (showConstr, toConstr)
Expand Down Expand Up @@ -93,10 +93,10 @@ peekInlinesFuzzy :: LuaError e
=> Peeker e [Inline]
peekInlinesFuzzy idx = liftLua (ltype idx) >>= \case
TypeString -> B.toList . B.text <$> peekText idx
_ -> choice
[ peekList peekInlineFuzzy
, fmap pure . peekInlineFuzzy
] idx
_ -> peekList peekInlineFuzzy idx
<|> (pure <$> peekInlineFuzzy idx)
<|> (failPeek =<<
typeMismatchMessage "Inline, list of Inlines, or string" idx)
{-# INLINABLE peekInlinesFuzzy #-}

-- | Inline object type.
Expand Down
6 changes: 6 additions & 0 deletions test/test-block.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ return {
{'Header', 'CodeBlock'}
)
end),
test('gives sensible error message', function ()
assert.error_matches(
function() Blocks(nil) end,
'Block, list of Blocks, or compatible element expected'
)
end)
},
group 'walk' {
test('modifies Inline subelements', function ()
Expand Down
6 changes: 6 additions & 0 deletions test/test-inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ return {
{'Str', 'Space', 'Str'}
)
end),
test('gives sensible error message', function ()
assert.error_matches(
function() Inlines(nil) end,
"Inline, list of Inlines, or string"
)
end)
},
group 'walk' {
test('modifies Inline subelements', function ()
Expand Down

0 comments on commit 970b413

Please sign in to comment.