packages feed

fontconfig-pure-0.5.1.0: lib/Graphics/Text/Font/Choose/Result.hs

-- | Exceptions which can be thrown by FontConfig.
module Graphics.Text.Font.Choose.Result (FcException(..), throwBool, throwNull, throwString) where

import Foreign.Ptr (Ptr, nullPtr)
import Text.Read (readMaybe)
import Control.Exception (Exception, throwIO)

-- | Exceptions which can be thrown by FontConfig.
data FcException = ErrType | ErrNoId | ErrOOM | ErrOther deriving (Read, Show, Eq, Enum)
instance Exception FcException

-- | Converts a pass|failure return value into a Haskell exception.
throwBool :: Bool -> IO ()
throwBool False = throwIO ErrOOM
throwBool True = return ()

-- | Turns a failed allocation into a Haskell exception.
throwNull :: Ptr a -> IO (Ptr a)
throwNull a | a == nullPtr = throwIO ErrOOM
    | otherwise = return a

-- | Turns a serialized string into a Haskell exception.
throwString :: String -> IO ()
throwString a | Just b <- readMaybe a :: Maybe FcException = throwIO b
    | otherwise = return ()