{-# LANGUAGE GADTs             #-}
{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE KindSignatures    #-}
{-# LANGUAGE OverloadedStrings #-}
--------------------------------------------------------------------------------
-- |
-- Module : Database.EventStore.Internal.Stream
-- Copyright : (C) 2015 Yorick Laupa
-- License : (see the file LICENSE)
--
-- Maintainer : Yorick Laupa <yo.eight@gmail.com>
-- Stability : provisional
-- Portability : non-portable
--
--------------------------------------------------------------------------------
module Database.EventStore.Internal.Stream where

--------------------------------------------------------------------------------
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Types

--------------------------------------------------------------------------------
-- | Represents a regular stream name or $all stream.
data StreamId loc where
    StreamName :: Text -> StreamId EventNumber
    All        :: StreamId Position

--------------------------------------------------------------------------------
-- | If the stream is the $all stream.
isAllStream :: StreamId t -> Bool
isAllStream :: StreamId t -> Bool
isAllStream StreamName{} = Bool
False
isAllStream StreamId t
_            = Bool
True

--------------------------------------------------------------------------------
instance Eq (StreamId t) where
  StreamName Text
n == :: StreamId t -> StreamId t -> Bool
== StreamName Text
v = Text
n Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
v
  StreamId t
All          == StreamId t
_            = Bool
True

--------------------------------------------------------------------------------
type StreamName = StreamId EventNumber

--------------------------------------------------------------------------------
streamIdRaw :: StreamId t -> Text
streamIdRaw :: StreamId t -> Text
streamIdRaw (StreamName Text
n) = Text
n
streamIdRaw StreamId t
All            = Text
""

--------------------------------------------------------------------------------
instance Show (StreamId t) where
    show :: StreamId t -> String
show (StreamName Text
n) = Text -> String
forall a. Show a => a -> String
show Text
n
    show StreamId t
All            = String
"$all"