{-# 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 :: String -> [(a, String)] -> String -> (i -> a) -> ReadPrec a
enumReadPrec prefix :: String
prefix table :: [(a, String)]
table conName :: String
conName con :: i -> a
con = ReadPrec a -> ReadPrec a
forall a. ReadPrec a -> ReadPrec a
parens
( ReadP a -> ReadPrec a
forall a. ReadP a -> ReadPrec a
lift
(do
ReadP ()
skipSpaces
String
_ <- String -> ReadP String
string String
prefix
[ReadP a] -> ReadP a
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((\(e :: a
e, s :: String
s) -> a
e a -> ReadP String -> ReadP a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ String -> ReadP String
string String
s) ((a, String) -> ReadP a) -> [(a, String)] -> [ReadP a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(a, String)]
table)
)
ReadPrec a -> ReadPrec a -> ReadPrec a
forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
+++ Prec -> ReadPrec a -> ReadPrec a
forall a. Prec -> ReadPrec a -> ReadPrec a
prec
10
(do
Lexeme -> ReadPrec ()
expectP (String -> Lexeme
Ident String
conName)
i
v <- ReadPrec i -> ReadPrec i
forall a. ReadPrec a -> ReadPrec a
step ReadPrec i
forall a. Read a => ReadPrec a
readPrec
a -> ReadPrec a
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 :: String
-> [(a, String)]
-> String
-> (a -> i)
-> (i -> ShowS)
-> Prec
-> a
-> ShowS
enumShowsPrec prefix :: String
prefix table :: [(a, String)]
table conName :: String
conName getInternal :: a -> i
getInternal showsInternal :: i -> ShowS
showsInternal p :: Prec
p e :: a
e =
case a -> [(a, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
e [(a, String)]
table of
Just s :: String
s -> String -> ShowS
showString String
prefix ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
s
Nothing ->
let x :: i
x = a -> i
getInternal a
e
in Bool -> ShowS -> ShowS
showParen (Prec
p Prec -> Prec -> Bool
forall a. Ord a => a -> a -> Bool
>= 11)
(String -> ShowS
showString String
conName ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString " " ShowS -> ShowS -> ShowS
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 :: String -> IO a -> IO a
traceAroundEvent _ a :: IO a
a = IO a
a
#endif