git.iko.soy/typeable/dom-parser
on master help sign in
Attribute parsing class
commit8d2bb395227fd2f4a58c5ac6adca967b8eff78bc authorVladislav Zavialov <vlad.z.4096@gmail.com> dateThu 22 Dec 2016, 16:56 (10 years ago) parent4ee841f
2 files changed+61−19
src/Text/XML/DOM/Parser/Class.hs+39−14 src/Text/XML/DOM/Parser/Combinators.hs+22−5
src/Text/XML/DOM/Parser/Class.hs+39−14
@@ -18,22 +18,23 @@ module Text.XML.DOM.Parser.Class
1818 , unitFromDom
1919 , voidFromDom
2020 , scientificFromDom
21+ -- * FromAttribute
22+ , FromAttribute(..)
2123 ) where
2224
23−import Control.Applicative
24−import Control.Lens
25−import Data.Fixed
26−import Data.Monoid
27−import Data.OpenUnion
28−import Data.Scientific
29−import Data.Text (Text)
30−import qualified Data.Text as T
31−import Data.Typeable
32−import Data.Void
33−import Text.XML
34−import Text.XML.DOM.Parser.Combinators
35−import Text.XML.DOM.Parser.Types
36−import TypeFun.Data.List hiding (Union)
25+import Control.Applicative
26+import Control.Lens
27+import Data.Fixed
28+import Data.Monoid
29+import Data.OpenUnion
30+import Data.Scientific
31+import Data.Text as T hiding (empty)
32+import Data.Typeable
33+import Data.Void
34+import Text.XML
35+import Text.XML.DOM.Parser.Combinators
36+import Text.XML.DOM.Parser.Types
37+import TypeFun.Data.List hiding (Union)
3738
3839proxyFromDom
3940 :: forall proxy m a
@@ -162,3 +163,27 @@ unitFromDom = pure ()
162163-- | Never parses successfully. It is just 'mzero'
163164voidFromDom :: (Monad m) => DomParserT Identity m Void
164165voidFromDom = empty
166+
167+class FromAttribute a where
168+ fromAttribute :: Text -> Either Text a
169+
170+instance FromAttribute () where
171+ fromAttribute _ = Right ()
172+
173+instance FromAttribute Text where
174+ fromAttribute = Right
175+
176+instance FromAttribute Char where
177+ fromAttribute = readChar
178+
179+instance FromAttribute Int where
180+ fromAttribute = readContent
181+
182+instance FromAttribute Integer where
183+ fromAttribute = readContent
184+
185+instance (Typeable a, HasResolution a) => FromAttribute (Fixed a) where
186+ fromAttribute = readContent
187+
188+instance FromAttribute Scientific where
189+ fromAttribute = readContent
src/Text/XML/DOM/Parser/Combinators.hs+22−5
@@ -28,6 +28,7 @@ module Text.XML.DOM.Parser.Combinators
2828 , maybeReadContent
2929 -- * Parsing attributes
3030 , parseAttribute
31+ , parseAttributeMaybe
3132 ) where
3233
3334import Control.Lens
@@ -309,8 +310,24 @@ parseAttribute
309310 -> (Text -> Either Text a)
310311 -- ^ Attribute content parser
311312 -> DomParserT Identity m a
312−parseAttribute attrName parser = getCurrentAttribute attrName >>= \case
313− Nothing -> throwParserError $ PEAttributeNotFound attrName
314− Just aval -> case parser aval of
315− Left err -> throwParserError $ PEAttributeWrongFormat attrName err
316− Right a -> return a
313+parseAttribute attrName parser =
314+ parseAttributeMaybe attrName parser >>= \case
315+ Nothing -> throwParserError $ PEAttributeNotFound attrName
316+ Just a -> return a
317+
318+-- | Parses attribute with given name.
319+--
320+-- @since 1.0.0
321+parseAttributeMaybe
322+ :: (Monad m)
323+ => Text
324+ -- ^ Attribute name
325+ -> (Text -> Either Text a)
326+ -- ^ Attribute content parser
327+ -> DomParserT Identity m (Maybe a)
328+parseAttributeMaybe attrName parser =
329+ getCurrentAttribute attrName >>= \case
330+ Nothing -> return Nothing
331+ Just aval -> case parser aval of
332+ Left err -> throwParserError $ PEAttributeWrongFormat attrName err
333+ Right a -> return $ Just a