~singpolyma/network-protocol-xmpp

490bb5f41a8176333f4a7ce9b8365fbc7f46c238 — John Millikin 12 years ago 92b4b6e
Make 'JID' an instance of 'IsString'.
1 files changed, 10 insertions(+), 0 deletions(-)

M Network/Protocol/XMPP/JID.hs
M Network/Protocol/XMPP/JID.hs => Network/Protocol/XMPP/JID.hs +10 -0
@@ 21,12 21,14 @@ module Network.Protocol.XMPP.JID
	, Resource (..)
	
	, parseJID
	, parseJID_
	, formatJID
	) where
import qualified Data.Text as T
import qualified Text.StringPrep as SP
import Text.NamePrep (namePrepProfile)
import Data.Ranges (single)
import Data.String (IsString, fromString)

newtype Node = Node { strNode :: T.Text }
newtype Domain = Domain { strDomain :: T.Text }


@@ 64,6 66,9 @@ instance Show JID where
	showsPrec d jid =  showParen (d > 10) $
		showString "JID " . shows (formatJID jid)

instance IsString JID where
	fromString = parseJID_ . fromString

parseJID :: T.Text -> Maybe JID
parseJID str = maybeJID where
	(node, postNode) = case T.spanBy (/= '@') str of


@@ 82,6 87,11 @@ parseJID str = maybeJID where
		SP.runStringPrep resourcePrep resource
		Just $ JID mNode (Domain domain) mResource

parseJID_ :: T.Text -> JID
parseJID_ text = case parseJID text of
	Just jid -> jid
	Nothing -> error "Malformed JID"

formatJID :: JID -> T.Text
formatJID (JID node (Domain domain) resource) = formatted where
	formatted = T.concat [node', domain, resource']