{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Network.AMQP.Worker.Key
( Key (..)
, Binding (..)
, Routing
, key
, word
, any1
, many
, keyText
, fromBind
, toBind
, toBindingKey
, RequireRouting
) where
import Data.Kind (Constraint, Type)
import qualified Data.List as List
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.TypeLits (ErrorMessage (..), TypeError)
newtype Key a msg = Key [Binding]
deriving (Key a msg -> Key a msg -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a msg. Key a msg -> Key a msg -> Bool
/= :: Key a msg -> Key a msg -> Bool
$c/= :: forall a msg. Key a msg -> Key a msg -> Bool
== :: Key a msg -> Key a msg -> Bool
$c== :: forall a msg. Key a msg -> Key a msg -> Bool
Eq, Int -> Key a msg -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a msg. Int -> Key a msg -> ShowS
forall a msg. [Key a msg] -> ShowS
forall a msg. Key a msg -> String
showList :: [Key a msg] -> ShowS
$cshowList :: forall a msg. [Key a msg] -> ShowS
show :: Key a msg -> String
$cshow :: forall a msg. Key a msg -> String
showsPrec :: Int -> Key a msg -> ShowS
$cshowsPrec :: forall a msg. Int -> Key a msg -> ShowS
Show, NonEmpty (Key a msg) -> Key a msg
Key a msg -> Key a msg -> Key a msg
forall b. Integral b => b -> Key a msg -> Key a msg
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a msg. NonEmpty (Key a msg) -> Key a msg
forall a msg. Key a msg -> Key a msg -> Key a msg
forall a msg b. Integral b => b -> Key a msg -> Key a msg
stimes :: forall b. Integral b => b -> Key a msg -> Key a msg
$cstimes :: forall a msg b. Integral b => b -> Key a msg -> Key a msg
sconcat :: NonEmpty (Key a msg) -> Key a msg
$csconcat :: forall a msg. NonEmpty (Key a msg) -> Key a msg
<> :: Key a msg -> Key a msg -> Key a msg
$c<> :: forall a msg. Key a msg -> Key a msg -> Key a msg
Semigroup, Key a msg
[Key a msg] -> Key a msg
Key a msg -> Key a msg -> Key a msg
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a msg. Semigroup (Key a msg)
forall a msg. Key a msg
forall a msg. [Key a msg] -> Key a msg
forall a msg. Key a msg -> Key a msg -> Key a msg
mconcat :: [Key a msg] -> Key a msg
$cmconcat :: forall a msg. [Key a msg] -> Key a msg
mappend :: Key a msg -> Key a msg -> Key a msg
$cmappend :: forall a msg. Key a msg -> Key a msg -> Key a msg
mempty :: Key a msg
$cmempty :: forall a msg. Key a msg
Monoid)
data Routing
data Binding
= Word Text
| Any
| Many
deriving (Binding -> Binding -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Binding -> Binding -> Bool
$c/= :: Binding -> Binding -> Bool
== :: Binding -> Binding -> Bool
$c== :: Binding -> Binding -> Bool
Eq, Int -> Binding -> ShowS
[Binding] -> ShowS
Binding -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Binding] -> ShowS
$cshowList :: [Binding] -> ShowS
show :: Binding -> String
$cshow :: Binding -> String
showsPrec :: Int -> Binding -> ShowS
$cshowsPrec :: Int -> Binding -> ShowS
Show)
fromBind :: Binding -> Text
fromBind :: Binding -> Text
fromBind (Word Text
t) = Text
t
fromBind Binding
Any = Text
"*"
fromBind Binding
Many = Text
"#"
toBind :: Text -> Binding
toBind :: Text -> Binding
toBind = Text -> Binding
Word
keyText :: Key a msg -> Text
keyText :: forall a msg. Key a msg -> Text
keyText (Key [Binding]
ns) =
Text -> [Text] -> Text
Text.intercalate Text
"." forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
List.map Binding -> Text
fromBind forall a b. (a -> b) -> a -> b
$ [Binding]
ns
any1 :: Key a msg -> Key Binding msg
any1 :: forall a msg. Key a msg -> Key Binding msg
any1 (Key [Binding]
ws) = forall a msg. [Binding] -> Key a msg
Key ([Binding]
ws forall a. [a] -> [a] -> [a]
++ [Binding
Any])
many :: Key a msg -> Key Binding msg
many :: forall a msg. Key a msg -> Key Binding msg
many (Key [Binding]
ws) = forall a msg. [Binding] -> Key a msg
Key ([Binding]
ws forall a. [a] -> [a] -> [a]
++ [Binding
Many])
word :: Text -> Key a msg -> Key a msg
word :: forall a msg. Text -> Key a msg -> Key a msg
word Text
w (Key [Binding]
ws) = forall a msg. [Binding] -> Key a msg
Key forall a b. (a -> b) -> a -> b
$ [Binding]
ws forall a. [a] -> [a] -> [a]
++ [Text -> Binding
toBind Text
w]
key :: Text -> Key Routing msg
key :: forall msg. Text -> Key Routing msg
key Text
t = forall a msg. [Binding] -> Key a msg
Key [Text -> Binding
Word Text
t]
toBindingKey :: Key a msg -> Key Binding msg
toBindingKey :: forall a msg. Key a msg -> Key Binding msg
toBindingKey (Key [Binding]
ws) = forall a msg. [Binding] -> Key a msg
Key [Binding]
ws
type family RequireRouting (a :: Type) :: Constraint where
RequireRouting Binding =
TypeError
( 'Text "Expected Routing Key but got Binding Key instead. Messages can be published only with keys that exclusivlely use `key` and `word`"
:$$: 'Text "\n key \"message\" & word \"new\" \n"
)
RequireRouting a = ()