{-| Module : Network.MQTT.Topic. Description : An MQTT client. Copyright : (c) Dustin Sallings, 2019 License : BSD3 Maintainer : dustin@spy.net Stability : experimental Topic and topic related utiilities. -} {-# LANGUAGE OverloadedStrings #-} module Network.MQTT.Topic ( Topic, match ) where import Data.Text (Text, splitOn, isPrefixOf) -- | An MQTT topic. type Topic = Text -- | match returns true iff the given pattern can be matched by the -- specified Topic as defined in the -- . match :: Text -> Topic -> Bool match pat top = cmp (splitOn "/" pat) (splitOn "/" top) where cmp [] [] = True cmp [] _ = False cmp _ [] = False cmp ["#"] (t:_) = not $ "$" `isPrefixOf` t cmp (p:ps) (t:ts) | p == t = cmp ps ts | p == "+" && not ("$" `isPrefixOf` t) = cmp ps ts | otherwise = False