add crypto errors standard enumeration and some helper.

This commit is contained in:
Vincent Hanquez 2015-04-07 10:56:38 +01:00
parent 54ba47384c
commit 3b19f768ce
3 changed files with 68 additions and 0 deletions

12
Crypto/Error.hs Normal file
View File

@ -0,0 +1,12 @@
-- |
-- Module : Crypto.Error
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : Stable
-- Portability : Excellent
--
module Crypto.Error
( module Crypto.Error.Types
) where
import Crypto.Error.Types

54
Crypto/Error/Types.hs Normal file
View File

@ -0,0 +1,54 @@
-- |
-- Module : Crypto.Error.Types
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : stable
-- Portability : Good
--
-- Cryptographic Error enumeration and handling
--
{-# LANGUAGE DeriveDataTypeable #-}
module Crypto.Error.Types
( CryptoError(..)
, CryptoFailable(..)
) where
import qualified Control.Exception as E
import Data.Data
import Crypto.Internal.Imports
-- | Enumeration of all possible errors that can be found in this library
data CryptoError =
-- symmetric cipher errors
CryptoError_KeySizeInvalid
| CryptoError_IvSizeInvalid
deriving (Show,Eq,Enum,Data,Typeable)
instance E.Exception CryptoError
-- | A simple Either like type to represent a computation that can fail
--
-- 2 possibles values are:
-- * 'CryptoPassed' :
data CryptoFailable a =
CryptoPassed a
| CryptoFailed CryptoError
instance Functor CryptoFailable where
fmap f (CryptoPassed a) = CryptoPassed (f a)
fmap _ (CryptoFailed r) = CryptoFailed r
instance Applicative CryptoFailable where
pure a = CryptoPassed a
instance Monad CryptoFailable where
return a = CryptoPassed a
throwCryptoError :: CryptoFailable a -> IO a
throwCryptoError = undefined
eitherCryptoError :: CryptoFailable a -> Either CryptoError a
eitherCryptoError = undefined
maybeCryptoError :: CryptoFailable a -> Maybe a
maybeCryptoError = undefined

View File

@ -32,6 +32,7 @@ Library
Crypto.Cipher.RC4
Crypto.Cipher.Types
Crypto.Data.AFIS
Crypto.Error
Crypto.MAC.Poly1305
Crypto.MAC.HMAC
Crypto.Number.Basic
@ -87,6 +88,7 @@ Library
Crypto.Cipher.Types.GF
Crypto.Cipher.Types.Stream
Crypto.Cipher.Types.Utils
Crypto.Error.Types
Crypto.Hash.Utils
Crypto.Hash.Utils.Cpu
Crypto.Hash.Types