module Data.GI.GIR.Deprecation
    ( DeprecationInfo(..)
    , queryDeprecated
    ) where

import qualified Data.Map as M
import Data.Text (Text)
import Text.XML (Element(elementAttributes))

import Data.GI.GIR.XMLUtils (firstChildWithLocalName, getElementContent)

-- | Deprecation information on a symbol.
data DeprecationInfo = DeprecationInfo {
      DeprecationInfo -> Maybe Text
deprecatedSinceVersion :: Maybe Text,
      DeprecationInfo -> Maybe Text
deprecationMessage     :: Maybe Text
    } deriving (Int -> DeprecationInfo -> ShowS
[DeprecationInfo] -> ShowS
DeprecationInfo -> String
(Int -> DeprecationInfo -> ShowS)
-> (DeprecationInfo -> String)
-> ([DeprecationInfo] -> ShowS)
-> Show DeprecationInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DeprecationInfo -> ShowS
showsPrec :: Int -> DeprecationInfo -> ShowS
$cshow :: DeprecationInfo -> String
show :: DeprecationInfo -> String
$cshowList :: [DeprecationInfo] -> ShowS
showList :: [DeprecationInfo] -> ShowS
Show, DeprecationInfo -> DeprecationInfo -> Bool
(DeprecationInfo -> DeprecationInfo -> Bool)
-> (DeprecationInfo -> DeprecationInfo -> Bool)
-> Eq DeprecationInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DeprecationInfo -> DeprecationInfo -> Bool
== :: DeprecationInfo -> DeprecationInfo -> Bool
$c/= :: DeprecationInfo -> DeprecationInfo -> Bool
/= :: DeprecationInfo -> DeprecationInfo -> Bool
Eq)

-- | Parse the deprecation information for the given element of the GIR file.
queryDeprecated :: Element -> Maybe DeprecationInfo
queryDeprecated :: Element -> Maybe DeprecationInfo
queryDeprecated Element
element =
    case Name -> Map Name Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
"deprecated" Map Name Text
attrs of
      Just Text
_ -> let version :: Maybe Text
version = Name -> Map Name Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
"deprecated-version" Map Name Text
attrs
                    msg :: Maybe Text
msg = Text -> Element -> Maybe Element
firstChildWithLocalName Text
"doc-deprecated" Element
element Maybe Element -> (Element -> Maybe Text) -> Maybe Text
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
                          Element -> Maybe Text
getElementContent
                in DeprecationInfo -> Maybe DeprecationInfo
forall a. a -> Maybe a
Just (Maybe Text -> Maybe Text -> DeprecationInfo
DeprecationInfo Maybe Text
version Maybe Text
msg)
      Maybe Text
Nothing -> Maybe DeprecationInfo
forall a. Maybe a
Nothing
    where attrs :: Map Name Text
attrs = Element -> Map Name Text
elementAttributes Element
element