{-# LANGUAGE CPP #-}
module Network.MPD.Commands.Query (Query, (=?), (<&>), anything) where
import Network.MPD.Commands.Arg
import Network.MPD.Commands.Types
import Data.Monoid
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup
#endif
newtype Query = Query [Match] deriving Show
data Match = Match Metadata Value
instance Show Match where
show (Match meta query) = show meta ++ " \"" ++ toString query ++ "\""
showList xs _ = unwords $ map show xs
instance Monoid Query where
mempty = Query []
Query a `mappend` Query b = Query (a ++ b)
#if MIN_VERSION_base(4,9,0)
instance Semigroup Query where
(<>) = mappend
#endif
instance MPDArg Query where
prep = foldl (<++>) (Args []) . f
where f (Query ms) = map (\(Match m q) -> Args [show m] <++> q) ms
anything :: Query
anything = mempty
(=?) :: Metadata -> Value -> Query
m =? s = Query [Match m s]
infixr 6 <&>
(<&>) :: Query -> Query -> Query
(<&>) = mappend