module Graphics.Text.Font.Choose.Result (Result(..), resultFromPointer) where import Foreign.Storable (peek) import Foreign.Ptr (Ptr, ptrNull) import Control.Exception (throwIO, throw, Exception) data Result = Match | NoMatch | TypeMismatch | ResultNoId | OutOfMemory deriving (Eq, Show, Read, Enum) resultFromPointer :: Ptr Int -> IO Result resultFromPointer res = toEnum <$> peek res data Error = ErrTypeMismatch | ErrResultNoId | ErrOutOfMemory deriving (Eq, Show, Read) instance Exception Error throwResult :: Result -> IO a -> IO (Maybe a) throwResult Match x = Just <$> x throwResult NoMatch _ = return Nothing throwResult TypeMismatch = throwIO ErrTypeMismatch throwResult ResultNoId = throwIO ErrResultNoId throwResult OutOfMemory = throwIO ErrOutOfMemory throwInt :: Int -> IO a -> IO (Maybe a) throwInt = throwResult . toEnum throwPtr :: Ptr Int -> IO a -> IO (Maybe a) throwPtr a b = resultFromPointer a >>= flip throwResult b throwFalse :: Bool -> IO () throwFalse True = return () throwFalse False = throwIO ErrOutOfMemory throwFalse' :: IO Bool -> IO () throwFalse' = (>>= throwFalse) throwNull :: Ptr a -> Ptr a throwNull ptr | ptr == ptrNull = throw ErrOutOfMemory | otherwise = ptr