git.iko.soy/typeable/dom-parser
on master help sign in
Make DomPath newtype
commit6849254fe7689091bd9b11383af5ade3c79769cd authorAlexey Uimanov <s9gf4ult@gmail.com> dateTue 3 Jan 2017, 15:59 (10 years ago) parent65a7be4
3 files changed+29−28
src/Text/XML/DOM/Parser/Combinators.hs+6−6 src/Text/XML/DOM/Parser/Types.hs+18−17 test/Main.hs+5−5
src/Text/XML/DOM/Parser/Combinators.hs+6−6
@@ -91,7 +91,7 @@ inElemTrav
9191 -> DomParserT g m (f a)
9292inElemTrav n = inFilteredTrav deeper
9393 where
94− deeper = ([n],) . toListOf (folded . nodes . folded . _Element . ell n)
94+ deeper = (DomPath [n],) . toListOf (folded . nodes . folded . _Element . ell n)
9595
9696-- | Runs parser inside first children element with given name
9797inElem
@@ -149,7 +149,7 @@ in given path.
149149divePath
150150 :: forall m g a
151151 . (Monad m, Foldable g)
152− => [Text]
152+ => DomPath
153153 -> DomParserT [] m a
154154 -> DomParserT g m a
155155divePath path = magnify $ to modElems
@@ -158,7 +158,7 @@ divePath path = magnify $ to modElems
158158 = over pdElements (toListOf $ folded . diver)
159159 . over pdPath (<> path)
160160 diver :: Fold Element Element
161− diver = foldr (.) id $ map toDive path
161+ diver = foldr (.) id $ map toDive $ unDomPath path
162162 toDive n = nodes . folded . _Element . ell n
163163
164164diveElem
@@ -166,7 +166,7 @@ diveElem
166166 => Text
167167 -> DomParserT [] m a
168168 -> DomParserT g m a
169−diveElem p = divePath [p]
169+diveElem p = divePath $ DomPath [p]
170170
171171-- | Ignore arbitrary current element if it conforms to predicate.
172172ignoreElem
@@ -225,8 +225,8 @@ checkCurrentName n = do
225225 cn <- getCurrentName
226226 unless (cn == n) $ do
227227 p <- view pdPath
228− let pinit = if null p then [] else init p
229− throwError $ ParserErrors [PENotFound $ pinit ++ [n]]
228+ let pinit = if null (unDomPath p) then [] else init $ unDomPath p
229+ throwError $ ParserErrors [PENotFound $ DomPath $ pinit ++ [n]]
230230 return ()
231231
232232-- | Get current content. If current element contains no content or
src/Text/XML/DOM/Parser/Types.hs+18−17
@@ -1,6 +1,6 @@
11module Text.XML.DOM.Parser.Types
22 ( -- * Parser internals
3− DomPath
3+ DomPath(..)
44 , ParserError(..)
55 , pePath
66 , peDetails
@@ -21,19 +21,20 @@ module Text.XML.DOM.Parser.Types
2121 , throwWrongFormat
2222 ) where
2323
24−import Control.Exception
25−import Control.Lens
26−import Control.Monad.Except
27−import Control.Monad.Reader
28−import Data.List.NonEmpty (NonEmpty(..))
29−import qualified Data.List.NonEmpty as NE
30−import Data.Maybe
31−import Data.Text (Text)
32−import GHC.Generics (Generic)
33−import Text.XML
34−import Text.XML.Lens
35−
36−type DomPath = [Text]
24+import Control.Exception
25+import Control.Lens
26+import Control.Monad.Except
27+import Control.Monad.Reader
28+import Data.List.NonEmpty as NE
29+import Data.Maybe
30+import Data.Text (Text)
31+import GHC.Generics (Generic)
32+import Text.XML
33+import Text.XML.Lens
34+
35+newtype DomPath = DomPath
36+ { unDomPath :: [Text]
37+ } deriving (Eq, Ord, Show, Read, Monoid)
3738
3839-- | DOM parser error description.
3940data ParserError
@@ -121,7 +122,7 @@ runDomParserT
121122runDomParserT doc par =
122123 let pd = ParserData
123124 { _pdElements = doc ^. root . to pure
124− , _pdPath = [doc ^. root . localName]
125+ , _pdPath = DomPath [doc ^. root . localName]
125126 }
126127 in runExceptT $ runReaderT par pd
127128
@@ -152,11 +153,11 @@ instance DomTraversable NonEmpty where
152153
153154throwParserError
154155 :: (MonadError ParserErrors m, MonadReader (ParserData f) m)
155− => ([Text] -> ParserError)
156+ => (DomPath -> ParserError)
156157 -> m a
157158throwParserError mkerr = do
158159 path <- view pdPath
159− throwError $ ParserErrors [mkerr path]
160+ throwError $ ParserErrors $ [mkerr path]
160161
161162-- | Throw 'PEWrongFormat' as very common case
162163throwWrongFormat
test/Main.hs+5−5
@@ -193,7 +193,7 @@ specParserFailedPath
193193 :: (Show a)
194194 => String -- ^ Name of spec
195195 -> Document
196− -> [Text] -- ^ Expteced path in error
196+ -> DomPath -- ^ Expteced path in error
197197 -> DomParser Identity a
198198 -> Spec
199199specParserFailedPath name doc path parser = it name $ example $ do
@@ -226,7 +226,7 @@ combinationsSpec = do
226226 describe "deep" $ do
227227 let parser = inElem "a" $ inElem "b" textFromDom
228228 specParserFailedPath "docDeepMultiple2" docDeepMultiple2
229− ["root", "a", "b"] parser
229+ (DomPath ["root", "a", "b"]) parser
230230 -- should fail here because first tag "a" does not contains
231231 -- tag "b" which is expected by parser @inElem "b"@
232232
@@ -276,7 +276,7 @@ combinationsSpec = do
276276 specParserEq "docDeepMultiple1" docDeepMultiple1 "content1" parser
277277 specParserEq "docDeepMultiple2" docDeepMultiple2 "content1" parser
278278 describe "fails" $ do
279− specParserFailedPath "docSimple" docSimple ["root", "a", "b"] parser
279+ specParserFailedPath "docSimple" docSimple (DomPath ["root", "a", "b"]) parser
280280 describe "multiple elements" $ do
281281 let
282282 parser = diveElem "a" $ inElemAll "b" textFromDom
@@ -311,9 +311,9 @@ combinationsSpec = do
311311 inElem "a" $ do
312312 checkCurrentName "a"
313313 describe "fails" $ do
314− specParserFailedPath "root element" docSimple ["toor"] $ do
314+ specParserFailedPath "root element" docSimple (DomPath ["toor"]) $ do
315315 checkCurrentName "toor"
316− specParserFailedPath "inner element" docSimple ["root", "b"] $ do
316+ specParserFailedPath "inner element" docSimple (DomPath ["root", "b"]) $ do
317317 inElem "a" $ checkCurrentName "b"
318318
319319contentSpec :: Spec