-- GENERATED by C->Haskell Compiler, version 0.26.1 Budburst, 4 April 2015 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/Data/Grib/Raw/Exception.chs" #-}
{- |
Module      : Data.Grib.Raw.Exception
Copyright   : (c) Mattias Jakobsson 2015
License     : GPL-3

Maintainer  : mjakob422@gmail.com
Stability   : unstable
Portability : portable

Exceptions for HGrib.
-}

{-# LANGUAGE DeriveDataTypeable #-}

module Data.Grib.Raw.Exception
       ( -- *GRIB Exceptions
         GribException(..)

         -- **Predicates
       , isAnyGribException
       , isGribException
       , isNullPtrReturned

         -- **Error codes
       , ErrorCode(..)
       ) where



import Control.Exception ( Exception )
import Data.Typeable     ( Typeable )




-- |Error codes returned by the grib_api functions.
data ErrorCode = GribInvalidKeyValue
               | GribValueDifferent
               | GribDifferentEdition
               | GribInvalidBpv
               | GribCorruptedIndex
               | GribMessageMalformed
               | GribUnderflow
               | GribSwitchNoMatch
               | GribConstantField
               | GribMessageTooLarge
               | GribInternalArrayTooSmall
               | GribPrematureEndOfFile
               | GribNullIndex
               | GribEndOfIndex
               | GribWrongGrid
               | GribNoValues
               | GribEnd
               | GribWrongType
               | GribNoDefinitions
               | GribConceptNoMatch
               | GribOutOfArea
               | GribMissingKey
               | GribInvalidOrderby
               | GribInvalidNearest
               | GribInvalidKeysIterator
               | GribInvalidIterator
               | GribInvalidIndex
               | GribInvalidGrib
               | GribInvalidFile
               | GribWrongStepUnit
               | GribWrongStep
               | GribInvalidType
               | GribWrongLength
               | GribValueCannotBeMissing
               | GribInvalidSectionNumber
               | GribNullHandle
               | GribInvalidArgument
               | GribReadOnly
               | GribOutOfMemory
               | GribGeocalculusProblem
               | GribNoMoreInSet
               | GribEncodingError
               | GribDecodingError
               | GribInvalidMessage
               | GribIoProblem
               | GribNotFound
               | GribWrongArraySize
               | GribCodeNotFoundInTable
               | GribFileNotFound
               | GribArrayTooSmall
               | Grib7777NotFound
               | GribNotImplemented
               | GribBufferTooSmall
               | GribInternalError
               | GribEndOfFile
               | GribSuccess
  deriving (Eq,Show)
instance Enum ErrorCode where
  succ GribInvalidKeyValue = GribValueDifferent
  succ GribValueDifferent = GribDifferentEdition
  succ GribDifferentEdition = GribInvalidBpv
  succ GribInvalidBpv = GribCorruptedIndex
  succ GribCorruptedIndex = GribMessageMalformed
  succ GribMessageMalformed = GribUnderflow
  succ GribUnderflow = GribSwitchNoMatch
  succ GribSwitchNoMatch = GribConstantField
  succ GribConstantField = GribMessageTooLarge
  succ GribMessageTooLarge = GribInternalArrayTooSmall
  succ GribInternalArrayTooSmall = GribPrematureEndOfFile
  succ GribPrematureEndOfFile = GribNullIndex
  succ GribNullIndex = GribEndOfIndex
  succ GribEndOfIndex = GribWrongGrid
  succ GribWrongGrid = GribNoValues
  succ GribNoValues = GribEnd
  succ GribEnd = GribWrongType
  succ GribWrongType = GribNoDefinitions
  succ GribNoDefinitions = GribConceptNoMatch
  succ GribConceptNoMatch = GribOutOfArea
  succ GribOutOfArea = GribMissingKey
  succ GribMissingKey = GribInvalidOrderby
  succ GribInvalidOrderby = GribInvalidNearest
  succ GribInvalidNearest = GribInvalidKeysIterator
  succ GribInvalidKeysIterator = GribInvalidIterator
  succ GribInvalidIterator = GribInvalidIndex
  succ GribInvalidIndex = GribInvalidGrib
  succ GribInvalidGrib = GribInvalidFile
  succ GribInvalidFile = GribWrongStepUnit
  succ GribWrongStepUnit = GribWrongStep
  succ GribWrongStep = GribInvalidType
  succ GribInvalidType = GribWrongLength
  succ GribWrongLength = GribValueCannotBeMissing
  succ GribValueCannotBeMissing = GribInvalidSectionNumber
  succ GribInvalidSectionNumber = GribNullHandle
  succ GribNullHandle = GribInvalidArgument
  succ GribInvalidArgument = GribReadOnly
  succ GribReadOnly = GribOutOfMemory
  succ GribOutOfMemory = GribGeocalculusProblem
  succ GribGeocalculusProblem = GribNoMoreInSet
  succ GribNoMoreInSet = GribEncodingError
  succ GribEncodingError = GribDecodingError
  succ GribDecodingError = GribInvalidMessage
  succ GribInvalidMessage = GribIoProblem
  succ GribIoProblem = GribNotFound
  succ GribNotFound = GribWrongArraySize
  succ GribWrongArraySize = GribCodeNotFoundInTable
  succ GribCodeNotFoundInTable = GribFileNotFound
  succ GribFileNotFound = GribArrayTooSmall
  succ GribArrayTooSmall = Grib7777NotFound
  succ Grib7777NotFound = GribNotImplemented
  succ GribNotImplemented = GribBufferTooSmall
  succ GribBufferTooSmall = GribInternalError
  succ GribInternalError = GribEndOfFile
  succ GribEndOfFile = GribSuccess
  succ GribSuccess = error "ErrorCode.succ: GribSuccess has no successor"

  pred GribValueDifferent = GribInvalidKeyValue
  pred GribDifferentEdition = GribValueDifferent
  pred GribInvalidBpv = GribDifferentEdition
  pred GribCorruptedIndex = GribInvalidBpv
  pred GribMessageMalformed = GribCorruptedIndex
  pred GribUnderflow = GribMessageMalformed
  pred GribSwitchNoMatch = GribUnderflow
  pred GribConstantField = GribSwitchNoMatch
  pred GribMessageTooLarge = GribConstantField
  pred GribInternalArrayTooSmall = GribMessageTooLarge
  pred GribPrematureEndOfFile = GribInternalArrayTooSmall
  pred GribNullIndex = GribPrematureEndOfFile
  pred GribEndOfIndex = GribNullIndex
  pred GribWrongGrid = GribEndOfIndex
  pred GribNoValues = GribWrongGrid
  pred GribEnd = GribNoValues
  pred GribWrongType = GribEnd
  pred GribNoDefinitions = GribWrongType
  pred GribConceptNoMatch = GribNoDefinitions
  pred GribOutOfArea = GribConceptNoMatch
  pred GribMissingKey = GribOutOfArea
  pred GribInvalidOrderby = GribMissingKey
  pred GribInvalidNearest = GribInvalidOrderby
  pred GribInvalidKeysIterator = GribInvalidNearest
  pred GribInvalidIterator = GribInvalidKeysIterator
  pred GribInvalidIndex = GribInvalidIterator
  pred GribInvalidGrib = GribInvalidIndex
  pred GribInvalidFile = GribInvalidGrib
  pred GribWrongStepUnit = GribInvalidFile
  pred GribWrongStep = GribWrongStepUnit
  pred GribInvalidType = GribWrongStep
  pred GribWrongLength = GribInvalidType
  pred GribValueCannotBeMissing = GribWrongLength
  pred GribInvalidSectionNumber = GribValueCannotBeMissing
  pred GribNullHandle = GribInvalidSectionNumber
  pred GribInvalidArgument = GribNullHandle
  pred GribReadOnly = GribInvalidArgument
  pred GribOutOfMemory = GribReadOnly
  pred GribGeocalculusProblem = GribOutOfMemory
  pred GribNoMoreInSet = GribGeocalculusProblem
  pred GribEncodingError = GribNoMoreInSet
  pred GribDecodingError = GribEncodingError
  pred GribInvalidMessage = GribDecodingError
  pred GribIoProblem = GribInvalidMessage
  pred GribNotFound = GribIoProblem
  pred GribWrongArraySize = GribNotFound
  pred GribCodeNotFoundInTable = GribWrongArraySize
  pred GribFileNotFound = GribCodeNotFoundInTable
  pred GribArrayTooSmall = GribFileNotFound
  pred Grib7777NotFound = GribArrayTooSmall
  pred GribNotImplemented = Grib7777NotFound
  pred GribBufferTooSmall = GribNotImplemented
  pred GribInternalError = GribBufferTooSmall
  pred GribEndOfFile = GribInternalError
  pred GribSuccess = GribEndOfFile
  pred GribInvalidKeyValue = error "ErrorCode.pred: GribInvalidKeyValue has no predecessor"

  enumFromTo from to = go from
    where
      end = fromEnum to
      go v = case compare (fromEnum v) end of
                 LT -> v : go (succ v)
                 EQ -> [v]
                 GT -> []

  enumFrom from = enumFromTo from GribSuccess

  fromEnum GribInvalidKeyValue = (-55)
  fromEnum GribValueDifferent = (-54)
  fromEnum GribDifferentEdition = (-53)
  fromEnum GribInvalidBpv = (-52)
  fromEnum GribCorruptedIndex = (-51)
  fromEnum GribMessageMalformed = (-50)
  fromEnum GribUnderflow = (-49)
  fromEnum GribSwitchNoMatch = (-48)
  fromEnum GribConstantField = (-47)
  fromEnum GribMessageTooLarge = (-46)
  fromEnum GribInternalArrayTooSmall = (-45)
  fromEnum GribPrematureEndOfFile = (-44)
  fromEnum GribNullIndex = (-43)
  fromEnum GribEndOfIndex = (-42)
  fromEnum GribWrongGrid = (-41)
  fromEnum GribNoValues = (-40)
  fromEnum GribEnd = (-39)
  fromEnum GribWrongType = (-38)
  fromEnum GribNoDefinitions = (-37)
  fromEnum GribConceptNoMatch = (-36)
  fromEnum GribOutOfArea = (-35)
  fromEnum GribMissingKey = (-34)
  fromEnum GribInvalidOrderby = (-33)
  fromEnum GribInvalidNearest = (-32)
  fromEnum GribInvalidKeysIterator = (-31)
  fromEnum GribInvalidIterator = (-30)
  fromEnum GribInvalidIndex = (-29)
  fromEnum GribInvalidGrib = (-28)
  fromEnum GribInvalidFile = (-27)
  fromEnum GribWrongStepUnit = (-26)
  fromEnum GribWrongStep = (-25)
  fromEnum GribInvalidType = (-24)
  fromEnum GribWrongLength = (-23)
  fromEnum GribValueCannotBeMissing = (-22)
  fromEnum GribInvalidSectionNumber = (-21)
  fromEnum GribNullHandle = (-20)
  fromEnum GribInvalidArgument = (-19)
  fromEnum GribReadOnly = (-18)
  fromEnum GribOutOfMemory = (-17)
  fromEnum GribGeocalculusProblem = (-16)
  fromEnum GribNoMoreInSet = (-15)
  fromEnum GribEncodingError = (-14)
  fromEnum GribDecodingError = (-13)
  fromEnum GribInvalidMessage = (-12)
  fromEnum GribIoProblem = (-11)
  fromEnum GribNotFound = (-10)
  fromEnum GribWrongArraySize = (-9)
  fromEnum GribCodeNotFoundInTable = (-8)
  fromEnum GribFileNotFound = (-7)
  fromEnum GribArrayTooSmall = (-6)
  fromEnum Grib7777NotFound = (-5)
  fromEnum GribNotImplemented = (-4)
  fromEnum GribBufferTooSmall = (-3)
  fromEnum GribInternalError = (-2)
  fromEnum GribEndOfFile = (-1)
  fromEnum GribSuccess = 0

  toEnum (-55) = GribInvalidKeyValue
  toEnum (-54) = GribValueDifferent
  toEnum (-53) = GribDifferentEdition
  toEnum (-52) = GribInvalidBpv
  toEnum (-51) = GribCorruptedIndex
  toEnum (-50) = GribMessageMalformed
  toEnum (-49) = GribUnderflow
  toEnum (-48) = GribSwitchNoMatch
  toEnum (-47) = GribConstantField
  toEnum (-46) = GribMessageTooLarge
  toEnum (-45) = GribInternalArrayTooSmall
  toEnum (-44) = GribPrematureEndOfFile
  toEnum (-43) = GribNullIndex
  toEnum (-42) = GribEndOfIndex
  toEnum (-41) = GribWrongGrid
  toEnum (-40) = GribNoValues
  toEnum (-39) = GribEnd
  toEnum (-38) = GribWrongType
  toEnum (-37) = GribNoDefinitions
  toEnum (-36) = GribConceptNoMatch
  toEnum (-35) = GribOutOfArea
  toEnum (-34) = GribMissingKey
  toEnum (-33) = GribInvalidOrderby
  toEnum (-32) = GribInvalidNearest
  toEnum (-31) = GribInvalidKeysIterator
  toEnum (-30) = GribInvalidIterator
  toEnum (-29) = GribInvalidIndex
  toEnum (-28) = GribInvalidGrib
  toEnum (-27) = GribInvalidFile
  toEnum (-26) = GribWrongStepUnit
  toEnum (-25) = GribWrongStep
  toEnum (-24) = GribInvalidType
  toEnum (-23) = GribWrongLength
  toEnum (-22) = GribValueCannotBeMissing
  toEnum (-21) = GribInvalidSectionNumber
  toEnum (-20) = GribNullHandle
  toEnum (-19) = GribInvalidArgument
  toEnum (-18) = GribReadOnly
  toEnum (-17) = GribOutOfMemory
  toEnum (-16) = GribGeocalculusProblem
  toEnum (-15) = GribNoMoreInSet
  toEnum (-14) = GribEncodingError
  toEnum (-13) = GribDecodingError
  toEnum (-12) = GribInvalidMessage
  toEnum (-11) = GribIoProblem
  toEnum (-10) = GribNotFound
  toEnum (-9) = GribWrongArraySize
  toEnum (-8) = GribCodeNotFoundInTable
  toEnum (-7) = GribFileNotFound
  toEnum (-6) = GribArrayTooSmall
  toEnum (-5) = Grib7777NotFound
  toEnum (-4) = GribNotImplemented
  toEnum (-3) = GribBufferTooSmall
  toEnum (-2) = GribInternalError
  toEnum (-1) = GribEndOfFile
  toEnum 0 = GribSuccess
  toEnum unmatched = error ("ErrorCode.toEnum: Cannot match " ++ show unmatched)

{-# LINE 92 "src/Data/Grib/Raw/Exception.chs" #-}


-- This comment is inserted to help Haddock keep all docs.

-- |An exception carrying an 'ErrorCode' or representing a returned
-- null pointer.
data GribException = GribException ErrorCode
                   | NullPtrReturned
                   deriving (Show, Typeable)

instance Exception GribException

-- |True for any 'GribException'.
isAnyGribException :: GribException -> Bool
isAnyGribException = const True

-- |True if a 'GribException' carries the given 'ErrorCode'.
isGribException :: ErrorCode -> GribException -> Bool
isGribException code (GribException code') = code' == code
isGribException _    NullPtrReturned       = False

-- |True for 'NullPtrReturned'.
isNullPtrReturned :: GribException -> Bool
isNullPtrReturned NullPtrReturned = True
isNullPtrReturned _               = False