Skip to content

Commit

Permalink
Have OrEmpty always parse emptyElement as Nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
EggBaconAndSpam committed Apr 26, 2023
1 parent e4fb2e2 commit 56e946d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: structural-xml
version: 0.2.0.1
version: 0.2.0.2
description: Structural XML parsing and unparsing.
license: BSD3
license-file: LICENSE
Expand Down
10 changes: 4 additions & 6 deletions src/Data/XML/Parse/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ readContent text i = case readMaybe $ Text.unpack text of
<> show (typeRep @a)

instance FromElement a => FromElement (OrEmpty a) where
fromElement el = case fromElement el of
Right a -> pure . OrEmpty $ Just a
Left err ->
if isEmptyElement el
then pure $ OrEmpty Nothing
else Left err
fromElement el =
if isEmptyElement el
then pure $ OrEmpty Nothing
else OrEmpty . Just <$> fromElement el

-- | A 'choice' element corresponds to the 'choice' schema element, which encodes sum types:
--
Expand Down
3 changes: 1 addition & 2 deletions src/Data/XML/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ data AnnotatedNode i
newtype ContentElement a = ContentElement {content :: a}
deriving stock (Show, Eq, Ord, Generic)

-- | Encodes `Nothing` as emptyElement. Parses the empty element as `Nothing`,
-- unless the parser for `a` succeeds on the empty element.
-- | Encodes `Nothing` as emptyElement. Parses the empty element as `Nothing`.
newtype OrEmpty a = OrEmpty {unOrEmpty :: Maybe a}

instance Semigroup Element where
Expand Down
2 changes: 1 addition & 1 deletion structural-xml.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: structural-xml
version: 0.2.0.1
version: 0.2.0.2
description: Structural XML parsing and unparsing.
author: Frederik Ramcke <[email protected]>
maintainer: Frederik Ramcke <[email protected]>
Expand Down

0 comments on commit 56e946d

Please sign in to comment.