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 .&&. :: a -> a -> Bool
.&&. a
flag = a
x a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
flag a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
flag

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