Skip to content

Commit

Permalink
Upd: use Name instead of Text for names
Browse files Browse the repository at this point in the history
  • Loading branch information
s9gf4ult committed Jan 3, 2017
1 parent 6849254 commit 7aa37c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/Text/XML/DOM/Parser/Class.hs
Expand Up @@ -165,7 +165,11 @@ voidFromDom :: (Monad m) => DomParserT Identity m Void
voidFromDom = empty

class FromAttribute a where
fromAttribute :: Text -> Either Text a
fromAttribute
:: Text
-- ^ Attribute contents to parse
-> Either Text a
-- ^ Either error message or result

instance FromAttribute () where
fromAttribute _ = Right ()
Expand Down
32 changes: 15 additions & 17 deletions src/Text/XML/DOM/Parser/Combinators.hs
Expand Up @@ -86,38 +86,38 @@ inFilteredTrav deeper = traverseElems trav

inElemTrav
:: (Monad m, Foldable g, DomTraversable f)
=> Text
=> Name -- ^ Name of tag to traverse in
-> DomParserT Identity m a
-> DomParserT g m (f a)
inElemTrav n = inFilteredTrav deeper
where
deeper = (DomPath [n],) . toListOf (folded . nodes . folded . _Element . ell n)
deeper = (DomPath [n],) . toListOf (folded . nodes . folded . _Element . el n)

-- | Runs parser inside first children element with given name
inElem
:: (Monad m, Foldable g)
=> Text
=> Name
-> DomParserT Identity m a
-> DomParserT g m a
inElem n = fmap runIdentity . inElemTrav n

inElemAll
:: (Monad m, Foldable g)
=> Text
=> Name
-> DomParserT Identity m a
-> DomParserT g m [a]
inElemAll = inElemTrav

inElemMay
:: (Monad m, Foldable g)
=> Text
=> Name
-> DomParserT Identity m a
-> DomParserT g m (Maybe a)
inElemMay = inElemTrav

inElemNe
:: (Monad m, Foldable g)
=> Text
=> Name
-> DomParserT Identity m a
-> DomParserT g m (NonEmpty a)
inElemNe = inElemTrav
Expand Down Expand Up @@ -159,11 +159,11 @@ divePath path = magnify $ to modElems
. over pdPath (<> path)
diver :: Fold Element Element
diver = foldr (.) id $ map toDive $ unDomPath path
toDive n = nodes . folded . _Element . ell n
toDive n = nodes . folded . _Element . el n

diveElem
:: (Monad m, Foldable g)
=> Text
=> Name
-> DomParserT [] m a
-> DomParserT g m a
diveElem p = divePath $ DomPath [p]
Expand Down Expand Up @@ -211,15 +211,15 @@ ignoreBlank = ignoreElem test
-- | Returns name of current element.
--
-- @since 1.0.0
getCurrentName :: (Monad m) => DomParserT Identity m Text
getCurrentName = view $ pdElements . to runIdentity . localName
getCurrentName :: (Monad m) => DomParserT Identity m Name
getCurrentName = view $ pdElements . to runIdentity . name

-- | If name of current tag differs from first argument throws 'PENotFound' with
-- tag name replaced in last path's segment. Useful for checking root
-- document's element name.
checkCurrentName
:: (Monad m)
=> Text
=> Name
-> DomParserT Identity m ()
checkCurrentName n = do
cn <- getCurrentName
Expand Down Expand Up @@ -294,18 +294,16 @@ getCurrentAttributes = view $ pdElements . to runIdentity . attrs
-- | Returns element with given name or 'Nothing'
--
-- @since 1.0.0
getCurrentAttribute :: (Monad m) => Text -> DomParserT Identity m (Maybe Text)
getCurrentAttribute attrName'
getCurrentAttribute :: (Monad m) => Name -> DomParserT Identity m (Maybe Text)
getCurrentAttribute attrName
= preview $ pdElements . to runIdentity . attr attrName
where
attrName = Name attrName' Nothing Nothing

-- | Parses attribute with given name, throws error if attribute is not found.
--
-- @since 1.0.0
parseAttribute
:: (Monad m)
=> Text
=> Name
-- ^ Attribute name
-> (Text -> Either Text a)
-- ^ Attribute content parser
Expand All @@ -320,7 +318,7 @@ parseAttribute attrName parser =
-- @since 1.0.0
parseAttributeMaybe
:: (Monad m)
=> Text
=> Name
-- ^ Attribute name
-> (Text -> Either Text a)
-- ^ Attribute content parser
Expand Down
10 changes: 5 additions & 5 deletions src/Text/XML/DOM/Parser/Types.hs
Expand Up @@ -33,8 +33,8 @@ import Text.XML
import Text.XML.Lens

newtype DomPath = DomPath
{ unDomPath :: [Text]
} deriving (Eq, Ord, Show, Read, Monoid)
{ unDomPath :: [Name]
} deriving (Eq, Ord, Show, Monoid)

-- | DOM parser error description.
data ParserError
Expand All @@ -51,7 +51,7 @@ data ParserError

-- | Could not parse attribute
| PEAttributeWrongFormat
{ _peAttributeName :: Text
{ _peAttributeName :: Name
, _peDetails :: Text
, _pePath :: DomPath
}
Expand All @@ -63,7 +63,7 @@ data ParserError

-- | Expected attribute but not found
| PEAttributeNotFound
{ _peAttributeName :: Text
{ _peAttributeName :: Name
, _pePath :: DomPath
}

Expand Down Expand Up @@ -122,7 +122,7 @@ runDomParserT
runDomParserT doc par =
let pd = ParserData
{ _pdElements = doc ^. root . to pure
, _pdPath = DomPath [doc ^. root . localName]
, _pdPath = DomPath [doc ^. root . name]
}
in runExceptT $ runReaderT par pd

Expand Down
2 changes: 1 addition & 1 deletion test/Main.hs
Expand Up @@ -33,7 +33,7 @@ testStructureAttributes = do
int <- parseAttribute "int" readContent
bool <- parseAttribute "bool" readBool
return $ TestStructure
{ tsName = name
{ tsName = nameLocalName name
, tsInts = [int]
, tsBools = pure bool }

Expand Down

0 comments on commit 7aa37c9

Please sign in to comment.