module Data.SpirV.Reflect.Enums.Common
  ( Flags
  , (.&&.)

  , Word32
  , Int32

  , IsString
  , Bits
  , FiniteBits

  , toLabel
  ) where

import Data.Word (Word32)
import Data.Bits (Bits, FiniteBits, (.&.))
import Data.Int (Int32)
import Data.String (IsString)
import Data.IntMap qualified as IntMap

type Flags = Word32

(.&&.) :: Bits a => a -> a -> Bool
a
x .&&. :: forall a. Bits a => a -> a -> Bool
.&&. a
flag = a
x forall a. Bits a => a -> a -> a
.&. a
flag forall a. Eq a => a -> a -> Bool
== a
flag

{-# INLINE toLabel #-}
toLabel :: (Enum i) => [(i, label)] -> i -> Maybe label
toLabel :: forall i label. Enum i => [(i, label)] -> i -> Maybe label
toLabel [(i, label)]
names i
key = forall a. Key -> IntMap a -> Maybe a
IntMap.lookup (forall a. Enum a => a -> Key
fromEnum i
key) IntMap label
indexed
  where
    indexed :: IntMap label
indexed = forall a. [(Key, a)] -> IntMap a
IntMap.fromList do
      (i
i, label
label) <- [(i, label)]
names
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Enum a => a -> Key
fromEnum i
i, label
label)