-- Does this need to be exported from Cal3D?

module Graphics.Animation.Cal3D.Error
    (Fallible, IOFallible
    , checkErrorValue, checkError
    )

where

type Fallible a = Either String a --  ???
type IOFallible a = IO (Fallible a) --  ???

-- | Compares the result of an action with a bad value, such as an 
-- error code, returns (Left errormessage) if action value == bad,
-- otherwise (Right actionresult)
checkErrorValue :: (Eq a) => 
                   IO a         -- ^ action
                -> a            -- ^ bad value
                -> String       -- ^ error message
                -> IO (Either String a)
checkErrorValue action bad errmsg = do
  {
    result <- action
  ; return (if result == bad
            then Left errmsg
            else Right result)
  }

-- | Like checkError, but returns (Right ()) instead of an interesting value.
checkError :: (Eq a) => 
              IO a              -- ^ action
           -> a                 -- ^ bad value
           -> String            -- ^ error message
           -> IO (Either String ())
checkError action bad errmsg = do
  {
    eresult <- checkErrorValue action bad errmsg
  ; return (case eresult of
              Left errmsg -> Left errmsg
              Right _ -> Right ())
  }