Skip to content

Instantly share code, notes, and snippets.

@k0001
Created April 11, 2018 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0001/e48b330fc09267f8890ef67f3361f879 to your computer and use it in GitHub Desktop.
Save k0001/e48b330fc09267f8890ef67f3361f879 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
module Main where
import qualified Data.ByteString as BS
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import qualified Xmlbf as X
import qualified Xmlbf.Xeno as Xx
data AtomFeed = AtomFeed
{ feedName :: !Text
, feedAttrs :: !(HashMap Text Text)
, feedChildren :: ![X.Node]
} deriving (Show, Eq)
instance X.ToXml AtomFeed where
toXml = \(AtomFeed n as cs) ->
let title = X.element' "atom:title" [] [X.text n]
in [X.element' "atom:feed" as (title : cs)]
instance X.FromXml AtomFeed where
fromXml = X.pElement "atom:feed" $ do
title <- X.pElement "atom:title" X.pText
attrs <- X.pAttrs
pure (AtomFeed title attrs [])
rawFeed :: BS.ByteString
rawFeed =
"<atom:feed xmlns:atom=\"http://www.w3.org/2005/Atom\">\
\ <atom:title>ServicePlan Feed</atom:title>\
\</atom:feed>"
expectedFeed :: AtomFeed
expectedFeed = AtomFeed
{ feedName = "ServicePlan Feed"
, feedAttrs = [("xmlns:atom", "http://www.w3.org/2005/Atom")]
, feedChildren = []
}
actualFeed :: AtomFeed
Right actualFeed = X.runParser X.fromXml =<< Xx.nodes rawFeed
@k0001
Copy link
Author

k0001 commented Apr 11, 2018

Ok, two modules loaded.                                                                                                                  
> expectedFeed                                                                                                                           
AtomFeed {feedName = "ServicePlan Feed", feedAttrs = fromList [("xmlns:atom","http://www.w3.org/2005/Atom")], feedChildren = []}         
> actualFeed                                                                   
AtomFeed {feedName = "ServicePlan Feed", feedAttrs = fromList [("xmlns:atom","http://www.w3.org/2005/Atom")], feedChildren = []}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment