{-# 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
enumReadPrec
:: Read i
=> String
-> [(a, String)]
-> String
-> (i -> a)
-> 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)
)
)
enumShowsPrec
:: Eq a
=> String
-> [(a, String)]
-> String
-> (a -> i)
-> (i -> ShowS)
-> 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)
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