{-# LANGUAGE CPP #-}

module Vulkan.Internal.Utils
  ( enumReadPrec
  , enumShowsPrec
  , traceAroundEvent
  ) where

import           Data.Foldable
import           Debug.Trace
import           GHC.Read                       ( expectP )
import           Text.ParserCombinators.ReadP   ( skipSpaces
                                                , string
                                                )
import           Text.Read

-- | The common bits of enumeration and bitmask read instances
enumReadPrec
  :: Read i
  => String
  -- ^ The common constructor prefix
  -> [(a, String)]
  -- ^ The table of values to constructor suffixes
  -> String
  -- ^ The newtype constructor name
  -> (i -> a)
  -- ^ The newtype constructor
  -> ReadPrec a
enumReadPrec :: forall i a.
Read i =>
String -> [(a, String)] -> String -> (i -> a) -> ReadPrec a
enumReadPrec String
prefix [(a, String)]
table String
conName i -> a
con = forall a. ReadPrec a -> ReadPrec a
parens
  (   forall a. ReadP a -> ReadPrec a
lift
      (do
        ReadP ()
skipSpaces
        String
_ <- String -> ReadP String
string String
prefix
        forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((\(a
e, String
s) -> a
e forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ String -> ReadP String
string String
s) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(a, String)]
table)
      )
  forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
+++ forall a. Prec -> ReadPrec a -> ReadPrec a
prec
        Prec
10
        (do
          Lexeme -> ReadPrec ()
expectP (String -> Lexeme
Ident String
conName)
          i
v <- forall a. ReadPrec a -> ReadPrec a
step forall a. Read a => ReadPrec a
readPrec
          forall (f :: * -> *) a. Applicative f => a -> f a
pure (i -> a
con i
v)
        )
  )

-- | The common bits of enumeration and bitmask show instances
enumShowsPrec
  :: Eq a
  => String
  -- ^ The common constructor prefix
  -> [(a, String)]
  -- ^ A table of values to constructor suffixes
  -> String
  -- ^ The newtype constructor name
  -> (a -> i)
  -- ^ Unpack the newtype
  -> (i -> ShowS)
  -- ^ Show the underlying value
  -> Int
  -> a
  -> ShowS
enumShowsPrec :: forall a i.
Eq a =>
String
-> [(a, String)]
-> String
-> (a -> i)
-> (i -> ShowS)
-> Prec
-> a
-> ShowS
enumShowsPrec String
prefix [(a, String)]
table String
conName a -> i
getInternal i -> ShowS
showsInternal Prec
p a
e =
  case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
e [(a, String)]
table of
    Just String
s -> String -> ShowS
showString String
prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
s
    Maybe String
Nothing ->
      let x :: i
x = a -> i
getInternal a
e
      in  Bool -> ShowS -> ShowS
showParen (Prec
p forall a. Ord a => a -> a -> Bool
>= Prec
11)
                    (String -> ShowS
showString String
conName forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" " forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> ShowS
showsInternal i
x)

-- | Wrap an IO action with a pair of 'traceEventIO' using the specified
-- message with "begin" or "end" appended.
traceAroundEvent :: String -> IO a -> IO a
#if defined(TRACE_CALLS)
traceAroundEvent msg a =
  traceEventIO (msg <> " begin") *> a <* traceEventIO (msg <> " end")
#else
traceAroundEvent :: forall a. String -> IO a -> IO a
traceAroundEvent String
_ IO a
a = IO a
a
#endif