Skip to content
Snippets Groups Projects
Commit d40e4994 authored by Alice Brenon's avatar Alice Brenon
Browse files

Clean commented-out code, expose functions to handle errors and prepare FromXML implementation

parent 5c93d48d
No related branches found
No related tags found
No related merge requests found
......@@ -3,13 +3,12 @@ module Text.InvisiXML.Namespace (
uRI
, prefix
, ixml
--, at
--, to
, addAttr
, getAttr
, setChildren
) where
--import Data.PositionTree (Position(..))
import Data.List (find)
import Text.XML.Light (Attr(..), Content(..), Element(..), QName(..), add_attr)
uRI :: String
......@@ -21,14 +20,6 @@ prefix = "ixml"
ixml :: String -> QName
ixml qName = QName {qName, qURI = Just uRI, qPrefix = Just prefix}
{-
at :: Position -> Attr
at (Position p) = Attr (ixml "at") (show p)
to :: Position -> Attr
to (Position p) = Attr (ixml "to") (show p)
-}
onContent :: (Element -> Element) -> Content -> Content
onContent f (Elem e) = Elem (f e)
onContent _ x = x
......@@ -36,5 +27,8 @@ onContent _ x = x
addAttr :: Attr -> Content -> Content
addAttr = onContent . add_attr
getAttr :: QName -> Element -> Maybe String
getAttr key = fmap attrVal . find ((==) key . attrKey) . elAttribs
setChildren :: [Content] -> Content -> Content
setChildren elContent = onContent $ \e -> e {elContent}
module Text.XML.Light.Serializer (
FromXML(..)
, ToXML(..)
, (.=)
, decode
, eitherDecode
, encode
, (.=)
, expected
) where
import Data.List (intercalate)
import Text.Printf (printf)
import Text.XML.Light (Attr(..), Content, QName, parseXML, ppContent)
import Text.XML.Light.Lexer (XmlSource)
......@@ -16,14 +19,18 @@ class FromXML a where
class ToXML a where
toXML :: a -> [Content]
encode :: ToXML a => a -> String
encode = intercalate "\n" . fmap ppContent . toXML
decode :: (XmlSource s, FromXML a) => s -> Maybe a
decode = either (\_ -> Nothing) Just . eitherDecode
eitherDecode :: (XmlSource s, FromXML a) => s -> Either String a
eitherDecode = fromXML . parseXML
encode :: ToXML a => a -> String
encode = intercalate "\n" . fmap ppContent . toXML
(.=) :: Show v => QName -> v -> Attr
k .= v = Attr k $ show v
expected :: String -> [Content] -> Either String a
expected elemType =
Left . printf "Expected %s but got %s" elemType . concat . fmap ppContent
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment