-- | Parsing of Enums.
module Data.GI.GIR.Enum
    ( Enumeration(..)
    , EnumerationMember(..)
    , parseEnum
    ) where

import Data.Int (Int64)
import Data.Text (Text)
import Foreign.C (CInt(..))

import Data.GI.GIR.Parser
import Data.GI.GIR.Type (parseCType)

data Enumeration = Enumeration {
    Enumeration -> [EnumerationMember]
enumMembers :: [EnumerationMember],
    Enumeration -> Maybe Text
enumErrorDomain :: Maybe Text,
    Enumeration -> Maybe Text
enumTypeInit :: Maybe Text,
    Enumeration -> Documentation
enumDocumentation :: Documentation,
    Enumeration -> Text
enumCType    :: Text,
    Enumeration -> Int
enumStorageBytes :: Int, -- ^ Bytes used for storage of this struct.
    Enumeration -> Maybe DeprecationInfo
enumDeprecated :: Maybe DeprecationInfo }
    deriving Int -> Enumeration -> ShowS
[Enumeration] -> ShowS
Enumeration -> String
(Int -> Enumeration -> ShowS)
-> (Enumeration -> String)
-> ([Enumeration] -> ShowS)
-> Show Enumeration
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Enumeration] -> ShowS
$cshowList :: [Enumeration] -> ShowS
show :: Enumeration -> String
$cshow :: Enumeration -> String
showsPrec :: Int -> Enumeration -> ShowS
$cshowsPrec :: Int -> Enumeration -> ShowS
Show

-- | Member of an enumeration.
data EnumerationMember = EnumerationMember {
  EnumerationMember -> Text
enumMemberName   :: Text,
  EnumerationMember -> Int64
enumMemberValue  :: Int64,
  EnumerationMember -> Text
enumMemberCId    :: Text,
  EnumerationMember -> Documentation
enumMemberDoc    :: Documentation
  } deriving Int -> EnumerationMember -> ShowS
[EnumerationMember] -> ShowS
EnumerationMember -> String
(Int -> EnumerationMember -> ShowS)
-> (EnumerationMember -> String)
-> ([EnumerationMember] -> ShowS)
-> Show EnumerationMember
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EnumerationMember] -> ShowS
$cshowList :: [EnumerationMember] -> ShowS
show :: EnumerationMember -> String
$cshow :: EnumerationMember -> String
showsPrec :: Int -> EnumerationMember -> ShowS
$cshowsPrec :: Int -> EnumerationMember -> ShowS
Show

-- | Parse a struct member.
parseEnumMember :: Parser EnumerationMember
parseEnumMember :: Parser EnumerationMember
parseEnumMember = do
  Text
name <- Name -> Parser Text
getAttr Name
"name"
  Int64
value <- Name -> Parser Text
getAttr Name
"value" Parser Text
-> (Text -> ReaderT ParseContext (Except Text) Int64)
-> ReaderT ParseContext (Except Text) Int64
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> ReaderT ParseContext (Except Text) Int64
forall a. Integral a => Text -> Parser a
parseIntegral
  Text
cid <- GIRXMLNamespace -> Name -> Parser Text
getAttrWithNamespace GIRXMLNamespace
CGIRNS Name
"identifier"
  Documentation
doc <- Parser Documentation
parseDocumentation
  EnumerationMember -> Parser EnumerationMember
forall (m :: * -> *) a. Monad m => a -> m a
return (EnumerationMember -> Parser EnumerationMember)
-> EnumerationMember -> Parser EnumerationMember
forall a b. (a -> b) -> a -> b
$ EnumerationMember :: Text -> Int64 -> Text -> Documentation -> EnumerationMember
EnumerationMember {
    enumMemberName :: Text
enumMemberName = Text
name,
    enumMemberValue :: Int64
enumMemberValue = Int64
value,
    enumMemberCId :: Text
enumMemberCId = Text
cid,
    enumMemberDoc :: Documentation
enumMemberDoc = Documentation
doc
    }

foreign import ccall "_gi_get_enum_storage_bytes" get_storage_bytes ::
    Int64 -> Int64 -> CInt

-- | Return the number of bytes that should be allocated for storage
-- of the given values in an enum.
extractEnumStorageBytes :: [Int64] -> Int
extractEnumStorageBytes :: [Int64] -> Int
extractEnumStorageBytes [Int64]
values =
    CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> CInt -> Int
forall a b. (a -> b) -> a -> b
$ Int64 -> Int64 -> CInt
get_storage_bytes ([Int64] -> Int64
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Int64]
values) ([Int64] -> Int64
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Int64]
values)

-- | Parse an "enumeration" element from the GIR file.
parseEnum :: Parser (Name, Enumeration)
parseEnum :: Parser (Name, Enumeration)
parseEnum = do
  Name
name <- Parser Name
parseName
  Text
ctype <- Parser Text
parseCType
  Documentation
doc <- Parser Documentation
parseDocumentation
  Maybe DeprecationInfo
deprecated <- Parser (Maybe DeprecationInfo)
parseDeprecation
  Maybe Text
errorDomain <- GIRXMLNamespace -> Name -> Parser (Maybe Text)
queryAttrWithNamespace GIRXMLNamespace
GLibGIRNS Name
"error-domain"
  Maybe Text
typeInit <- GIRXMLNamespace -> Name -> Parser (Maybe Text)
queryAttrWithNamespace GIRXMLNamespace
GLibGIRNS Name
"get-type"
  [EnumerationMember]
members <- Text -> Parser EnumerationMember -> Parser [EnumerationMember]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName Text
"member" Parser EnumerationMember
parseEnumMember
  (Name, Enumeration) -> Parser (Name, Enumeration)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name
name,
          Enumeration :: [EnumerationMember]
-> Maybe Text
-> Maybe Text
-> Documentation
-> Text
-> Int
-> Maybe DeprecationInfo
-> Enumeration
Enumeration {
            enumMembers :: [EnumerationMember]
enumMembers = [EnumerationMember]
members
          , enumErrorDomain :: Maybe Text
enumErrorDomain = Maybe Text
errorDomain
          , enumDocumentation :: Documentation
enumDocumentation = Documentation
doc
          , enumTypeInit :: Maybe Text
enumTypeInit = Maybe Text
typeInit
          , enumCType :: Text
enumCType = Text
ctype
          , enumStorageBytes :: Int
enumStorageBytes = [Int64] -> Int
extractEnumStorageBytes ((EnumerationMember -> Int64) -> [EnumerationMember] -> [Int64]
forall a b. (a -> b) -> [a] -> [b]
map EnumerationMember -> Int64
enumMemberValue [EnumerationMember]
members)
          , enumDeprecated :: Maybe DeprecationInfo
enumDeprecated = Maybe DeprecationInfo
deprecated
          })