{-|
Module      : Pulsar.Types
Description : End-user Pulsar API types.
License     : Apache-2.0
Maintainer  : gabriel.volpe@chatroulette.com
Stability   : experimental

End-user types to configure consumers and producers.
-}
module Pulsar.Types where

import qualified Data.ByteString.Lazy.Char8    as CL
import           Data.Char                      ( toLower )
import           Data.String
import qualified Data.Text                     as T
import           Proto.PulsarApi                ( MessageIdData )

{- | A Topic is in the form "type:\/\/tenant\/namespace\/topic-name", which is what the 'Show' instance does. -}
data Topic = Topic
  { Topic -> TopicType
type' :: TopicType
  , Topic -> Tenant
tenant :: Tenant
  , Topic -> NameSpace
namespace :: NameSpace
  , Topic -> TopicName
name :: TopicName
  }

{- | A default 'Topic': "non-persistent:\/\/public\/default\/my-topic". -}
defaultTopic :: String -> Topic
defaultTopic :: String -> Topic
defaultTopic n :: String
n = Topic :: TopicType -> Tenant -> NameSpace -> TopicName -> Topic
Topic { type' :: TopicType
type'     = TopicType
NonPersistent
                       , tenant :: Tenant
tenant    = String -> Tenant
Tenant "public"
                       , namespace :: NameSpace
namespace = String -> NameSpace
NameSpace "default"
                       , name :: TopicName
name      = String -> TopicName
TopicName String
n
                       }

instance Show Topic where
  show :: Topic -> String
show (Topic typ :: TopicType
typ tn :: Tenant
tn ns :: NameSpace
ns n :: TopicName
n) =
    Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TopicType -> String
forall a. Show a => a -> String
show TopicType
typ String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "://" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Tenant -> String
forall a. Show a => a -> String
show Tenant
tn String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "/" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> NameSpace -> String
forall a. Show a => a -> String
show NameSpace
ns String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "/" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TopicName -> String
forall a. Show a => a -> String
show TopicName
n

{- | A topic can be either 'Persistent' or 'NonPersistent'. -}
data TopicType = Persistent | NonPersistent

instance Show TopicType where
  show :: TopicType -> String
show Persistent    = "persistent"
  show NonPersistent = "non-persistent"

{- | A tenant can be any string value. Default value is "public". -}
newtype Tenant = Tenant String

instance Show Tenant where
  show :: Tenant -> String
show (Tenant t :: String
t) = String
t

{- | A namespace can be any string value. Default value is "default". -}
newtype NameSpace = NameSpace String

instance Show NameSpace where
  show :: NameSpace -> String
show (NameSpace t :: String
t) = String
t

{- | A topic name can be any string value. -}
newtype TopicName = TopicName String

instance Show TopicName where
  show :: TopicName -> String
show (TopicName t :: String
t) = String
t

{- | A message id, needed for acknowledging messages. See 'Pulsar.Consumer.ack'. -}
newtype MsgId = MsgId MessageIdData

{- | A consumed message, containing both 'MsgId' and payload as bytestring. -}
data Message = Message MsgId CL.ByteString

{- | A produced message, containing just a payload as bytestring. -}
newtype PulsarMessage = PulsarMessage CL.ByteString deriving Int -> PulsarMessage -> ShowS
[PulsarMessage] -> ShowS
PulsarMessage -> String
(Int -> PulsarMessage -> ShowS)
-> (PulsarMessage -> String)
-> ([PulsarMessage] -> ShowS)
-> Show PulsarMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PulsarMessage] -> ShowS
$cshowList :: [PulsarMessage] -> ShowS
show :: PulsarMessage -> String
$cshow :: PulsarMessage -> String
showsPrec :: Int -> PulsarMessage -> ShowS
$cshowsPrec :: Int -> PulsarMessage -> ShowS
Show

instance IsString PulsarMessage where
  fromString :: String -> PulsarMessage
fromString = ByteString -> PulsarMessage
PulsarMessage (ByteString -> PulsarMessage)
-> (String -> ByteString) -> String -> PulsarMessage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
CL.pack

{- | A subscription name can be any string value. -}
newtype SubscriptionName = SubscriptionName T.Text deriving Int -> SubscriptionName -> ShowS
[SubscriptionName] -> ShowS
SubscriptionName -> String
(Int -> SubscriptionName -> ShowS)
-> (SubscriptionName -> String)
-> ([SubscriptionName] -> ShowS)
-> Show SubscriptionName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SubscriptionName] -> ShowS
$cshowList :: [SubscriptionName] -> ShowS
show :: SubscriptionName -> String
$cshow :: SubscriptionName -> String
showsPrec :: Int -> SubscriptionName -> ShowS
$cshowsPrec :: Int -> SubscriptionName -> ShowS
Show

instance IsString SubscriptionName where
  fromString :: String -> SubscriptionName
fromString = Text -> SubscriptionName
SubscriptionName (Text -> SubscriptionName)
-> (String -> Text) -> String -> SubscriptionName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack