You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
-- Marshalling between XML and Native Types
module Network.XMPP.Pickle where
14 years ago
import Data.XML.Types
import Data.XML.Pickle
14 years ago
mbToBool :: Maybe t -> Bool
mbToBool (Just _) = True
mbToBool _ = False
14 years ago
xpElemEmpty :: Name -> PU [Node] ()
xpElemEmpty name = xpWrap (\((),()) -> ())
(\() -> ((),())) $
xpElem name xpUnit xpUnit
14 years ago
-- xpElemExists :: Name -> PU [Node] Bool
-- xpElemExists name = xpWrap (\x -> mbToBool x)
-- (\x -> if x then Just () else Nothing) $
-- xpOption (xpElemEmpty name)
14 years ago
xpNodeElem :: PU [Node] a -> PU Element a
xpNodeElem xp = PU { pickleTree = \x -> head $ (pickleTree xp x) >>= \y ->
case y of
NodeElement e -> [e]
14 years ago
_ -> []
14 years ago
, unpickleTree = \x -> case unpickleTree xp $ [NodeElement x] of
Left l -> Left l
Right (a,(_,c)) -> Right (a,(Nothing,c))
}
14 years ago
ignoreAttrs :: PU t ((), b) -> PU t b
14 years ago
ignoreAttrs = xpWrap snd ((),)
14 years ago
mbl :: Maybe [a] -> [a]
mbl (Just l) = l
mbl Nothing = []
14 years ago
lmb :: [t] -> Maybe [t]
lmb [] = Nothing
lmb x = Just x
14 years ago
right :: Either [Char] t -> t
right (Left l) = error l
right (Right r) = r
14 years ago
14 years ago
unpickleElem :: PU [Node] c -> Element -> c
unpickleElem p = right . unpickle (xpNodeElem p)
pickleElem :: PU [Node] a -> a -> Element
pickleElem p = pickle $ xpNodeElem p