git.iko.soy/typeable/dom-parser
on master help sign in
Add explicit method for FromAttribute
commite0b34d0fdb8de14ab702ce99e2628b337c134d9a authorAlexey Uimanov <s9gf4ult@gmail.com> dateThu 5 Jan 2017, 00:40 (10 years ago) parentb5da244
2 files changed+121−53
src/Text/XML/DOM/Parser/FromAttribute.hs+77−7 src/Text/XML/DOM/Parser/FromDom.hs+44−46
src/Text/XML/DOM/Parser/FromAttribute.hs+77−7
@@ -1,12 +1,27 @@
11module Text.XML.DOM.Parser.FromAttribute
22 ( -- * FromAttribute
33 FromAttribute(..)
4+ , proxyFromAttribute
5+ -- * Explicit methods
6+ , textFromAttribute
7+ , stringFromAttribute
8+ , charFromAttribute
9+ , intFromAttribute
10+ , integerFromAttribute
11+ , doubleFromAttribute
12+ , fixedFromAttribute
13+ , boolFromAttribute
14+ , unitFromAttribute
15+ , voidFromAttribute
16+ , scientificFromAttribute
17+
418 ) where
519
620import Data.Fixed
721import Data.Scientific
822import Data.Text as T
923import Data.Typeable
24+import Data.Void
1025import Text.XML.DOM.Parser.Common
1126
1227class FromAttribute a where
@@ -16,23 +31,78 @@ class FromAttribute a where
1631 -> Either Text a
1732 -- ^ Either error message or result
1833
34+proxyFromAttribute
35+ :: FromAttribute a
36+ => Proxy a
37+ -> Text
38+ -> Either Text a
39+proxyFromAttribute _ = fromAttribute
40+
1941instance FromAttribute () where
20− fromAttribute _ = Right ()
42+ fromAttribute = unitFromAttribute
43+
44+unitFromAttribute :: Text -> Either Text ()
45+unitFromAttribute _ = Right ()
46+
47+instance FromAttribute Void where
48+ fromAttribute = voidFromAttribute
49+
50+voidFromAttribute :: Text -> Either Text Void
51+voidFromAttribute _ = Left "Void has been parsed"
2152
2253instance FromAttribute Text where
23− fromAttribute = Right
54+ fromAttribute = textFromAttribute
55+
56+textFromAttribute :: Text -> Either Text Text
57+textFromAttribute = Right
58+
59+instance FromAttribute String where
60+ fromAttribute = stringFromAttribute
61+
62+stringFromAttribute :: Text -> Either Text String
63+stringFromAttribute = Right . T.unpack
2464
2565instance FromAttribute Char where
26− fromAttribute = readChar
66+ fromAttribute = charFromAttribute
67+
68+charFromAttribute :: Text -> Either Text Char
69+charFromAttribute = readChar
2770
2871instance FromAttribute Int where
29− fromAttribute = readContent
72+ fromAttribute = intFromAttribute
73+
74+intFromAttribute :: Text -> Either Text Int
75+intFromAttribute = readContent
3076
3177instance FromAttribute Integer where
32− fromAttribute = readContent
78+ fromAttribute = integerFromAttribute
79+
80+integerFromAttribute :: Text -> Either Text Integer
81+integerFromAttribute = readContent
82+
83+instance FromAttribute Double where
84+ fromAttribute = doubleFromAttribute
85+
86+doubleFromAttribute :: Text -> Either Text Double
87+doubleFromAttribute = readContent
3388
3489instance (Typeable a, HasResolution a) => FromAttribute (Fixed a) where
35− fromAttribute = readContent
90+ fromAttribute = fixedFromAttribute
91+
92+fixedFromAttribute
93+ :: (HasResolution a, Typeable a)
94+ => Text
95+ -> Either Text (Fixed a)
96+fixedFromAttribute = readContent
97+
98+instance FromAttribute Bool where
99+ fromAttribute = boolFromAttribute
100+
101+boolFromAttribute :: Text -> Either Text Bool
102+boolFromAttribute = readBool
36103
37104instance FromAttribute Scientific where
38− fromAttribute = readContent
105+ fromAttribute = scientificFromAttribute
106+
107+scientificFromAttribute :: Text -> Either Text Scientific
108+scientificFromAttribute = readContent
src/Text/XML/DOM/Parser/FromDom.hs+44−46
@@ -8,13 +8,11 @@ module Text.XML.DOM.Parser.FromDom
88 , textFromDom
99 , stringFromDom
1010 , charFromDom
11− , readChar
1211 , intFromDom
1312 , integerFromDom
1413 , doubleFromDom
1514 , fixedFromDom
1615 , boolFromDom
17− , readBool
1816 , unitFromDom
1917 , voidFromDom
2018 , scientificFromDom
@@ -47,36 +45,76 @@ class FromDom a where
4745instance FromDom () where
4846 fromDom = unitFromDom
4947
48+-- | Always successfully parses any DOM to @()@
49+unitFromDom :: (Monad m) => DomParserT Identity m ()
50+unitFromDom = pure ()
51+
5052instance FromDom Void where
5153 fromDom = voidFromDom
5254
55+-- | Never parses successfully. It is just 'empty'
56+voidFromDom :: (Monad m) => DomParserT Identity m Void
57+voidFromDom = empty
58+
5359instance FromDom Text where
5460 fromDom = textFromDom
5561
62+textFromDom :: (Monad m) => DomParserT Identity m Text
63+textFromDom = parseContent Right
64+
5665instance FromDom String where
5766 fromDom = stringFromDom
5867
68+stringFromDom :: (Monad m) => DomParserT Identity m String
69+stringFromDom = parseContent $ Right . T.unpack
70+
5971instance FromDom Char where
6072 fromDom = charFromDom
6173
74+charFromDom :: (Monad m) => DomParserT Identity m Char
75+charFromDom = parseContent readChar
76+
6277instance FromDom Int where
6378 fromDom = intFromDom
6479
80+intFromDom :: (Monad m) => DomParserT Identity m Int
81+intFromDom = parseContent readContent
82+
6583instance FromDom Integer where
6684 fromDom = integerFromDom
6785
86+integerFromDom :: (Monad m) => DomParserT Identity m Integer
87+integerFromDom = parseContent readContent
88+
6889instance FromDom Double where
6990 fromDom = doubleFromDom
7091
92+doubleFromDom :: (Monad m) => DomParserT Identity m Double
93+doubleFromDom = parseContent readContent
94+
7195instance (HasResolution a, Typeable a) => FromDom (Fixed a) where
7296 fromDom = fixedFromDom
7397
98+fixedFromDom
99+ :: (Monad m, Typeable a, HasResolution a)
100+ => DomParserT Identity m (Fixed a)
101+fixedFromDom = parseContent readContent
102+
74103instance FromDom Scientific where
75104 fromDom = scientificFromDom
76105
106+scientificFromDom :: Monad m => DomParserT Identity m Scientific
107+scientificFromDom = parseContent readContent
108+
77109instance FromDom Bool where
78110 fromDom = boolFromDom
79111
112+-- | Expects content to be y, yes, t, true or 1 for True Values n, no,
113+-- f, false or 0 for False. Case is not significant, blank characters
114+-- are striped.
115+boolFromDom :: (Monad m) => DomParserT Identity m Bool
116+boolFromDom = parseContent readBool
117+
80118instance FromDom (Union '[]) where
81119 fromDom = empty
82120
@@ -88,54 +126,14 @@ instance
88126 fromDom = (liftUnion <$> (proxyFromDom (Proxy :: Proxy a)))
89127 <|> (reUnion <$> (proxyFromDom (Proxy :: Proxy (Union as))))
90128
91−instance FromDom Element where
92− fromDom = elementFromDom
93−
94−elementFromDom :: (Monad m) => DomParserT Identity m Element
95−elementFromDom = view $ pdElements . to runIdentity
96−
97129unionFromDom
98130 :: (Monad m, FromDom (Union as))
99131 => proxy as
100132 -> DomParserT Identity m (Union as)
101133unionFromDom _ = fromDom
102134
103−textFromDom :: (Monad m) => DomParserT Identity m Text
104−textFromDom = parseContent Right
105−
106−stringFromDom :: (Monad m) => DomParserT Identity m String
107−stringFromDom = parseContent $ Right . T.unpack
108−
109−charFromDom :: (Monad m) => DomParserT Identity m Char
110−charFromDom = parseContent readChar
111−
112−intFromDom :: (Monad m) => DomParserT Identity m Int
113−intFromDom = parseContent readContent
114−
115−integerFromDom :: (Monad m) => DomParserT Identity m Integer
116−integerFromDom = parseContent readContent
117−
118−doubleFromDom :: (Monad m) => DomParserT Identity m Double
119−doubleFromDom = parseContent readContent
120−
121−fixedFromDom
122− :: (Monad m, Typeable a, HasResolution a)
123− => DomParserT Identity m (Fixed a)
124−fixedFromDom = parseContent readContent
125−
126−scientificFromDom :: Monad m => DomParserT Identity m Scientific
127−scientificFromDom = parseContent readContent
128−
129−-- | Expects content to be y, yes, t, true or 1 for True value. n, no,
130−-- f, false or 0 for False value. Case is not significant, blank
131−-- characters are striped.
132−boolFromDom :: (Monad m) => DomParserT Identity m Bool
133−boolFromDom = parseContent readBool
134−
135−-- | Always successfully parses any DOM to @()@
136−unitFromDom :: (Monad m) => DomParserT Identity m ()
137−unitFromDom = pure ()
135+instance FromDom Element where
136+ fromDom = elementFromDom
138137
139−-- | Never parses successfully. It is just 'mzero'
140−voidFromDom :: (Monad m) => DomParserT Identity m Void
141−voidFromDom = empty
138+elementFromDom :: (Monad m) => DomParserT Identity m Element
139+elementFromDom = view $ pdElements . to runIdentity