{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}
module Crypto.Error.Types
( CryptoError(..)
, CryptoFailable(..)
, throwCryptoErrorIO
, throwCryptoError
, onCryptoFailure
, eitherCryptoError
, maybeCryptoError
) where
import qualified Control.Exception as E
import Data.Data
import Basement.Monad (MonadFailure(..))
data CryptoError =
CryptoError_KeySizeInvalid
| CryptoError_IvSizeInvalid
| CryptoError_SeedSizeInvalid
| CryptoError_AEADModeNotSupported
| CryptoError_SecretKeySizeInvalid
| CryptoError_SecretKeyStructureInvalid
| CryptoError_PublicKeySizeInvalid
| CryptoError_SharedSecretSizeInvalid
| CryptoError_EcScalarOutOfBounds
| CryptoError_PointSizeInvalid
| CryptoError_PointFormatInvalid
| CryptoError_PointFormatUnsupported
| CryptoError_PointCoordinatesInvalid
| CryptoError_ScalarMultiplicationInvalid
| CryptoError_MacKeyInvalid
| CryptoError_AuthenticationTagSizeInvalid
| CryptoError_PrimeSizeInvalid
| CryptoError_SaltTooSmall
| CryptoError_OutputLengthTooSmall
| CryptoError_OutputLengthTooBig
deriving (Int -> CryptoError -> ShowS
[CryptoError] -> ShowS
CryptoError -> String
(Int -> CryptoError -> ShowS)
-> (CryptoError -> String)
-> ([CryptoError] -> ShowS)
-> Show CryptoError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CryptoError] -> ShowS
$cshowList :: [CryptoError] -> ShowS
show :: CryptoError -> String
$cshow :: CryptoError -> String
showsPrec :: Int -> CryptoError -> ShowS
$cshowsPrec :: Int -> CryptoError -> ShowS
Show,CryptoError -> CryptoError -> Bool
(CryptoError -> CryptoError -> Bool)
-> (CryptoError -> CryptoError -> Bool) -> Eq CryptoError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CryptoError -> CryptoError -> Bool
$c/= :: CryptoError -> CryptoError -> Bool
== :: CryptoError -> CryptoError -> Bool
$c== :: CryptoError -> CryptoError -> Bool
Eq,Int -> CryptoError
CryptoError -> Int
CryptoError -> [CryptoError]
CryptoError -> CryptoError
CryptoError -> CryptoError -> [CryptoError]
CryptoError -> CryptoError -> CryptoError -> [CryptoError]
(CryptoError -> CryptoError)
-> (CryptoError -> CryptoError)
-> (Int -> CryptoError)
-> (CryptoError -> Int)
-> (CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> CryptoError -> [CryptoError])
-> Enum CryptoError
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
$cenumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
enumFromTo :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromTo :: CryptoError -> CryptoError -> [CryptoError]
enumFromThen :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromThen :: CryptoError -> CryptoError -> [CryptoError]
enumFrom :: CryptoError -> [CryptoError]
$cenumFrom :: CryptoError -> [CryptoError]
fromEnum :: CryptoError -> Int
$cfromEnum :: CryptoError -> Int
toEnum :: Int -> CryptoError
$ctoEnum :: Int -> CryptoError
pred :: CryptoError -> CryptoError
$cpred :: CryptoError -> CryptoError
succ :: CryptoError -> CryptoError
$csucc :: CryptoError -> CryptoError
Enum,Typeable CryptoError
DataType
Constr
Typeable CryptoError
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError)
-> (CryptoError -> Constr)
-> (CryptoError -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError))
-> ((forall b. Data b => b -> b) -> CryptoError -> CryptoError)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r)
-> (forall u. (forall d. Data d => d -> u) -> CryptoError -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> CryptoError -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError)
-> Data CryptoError
CryptoError -> DataType
CryptoError -> Constr
(forall b. Data b => b -> b) -> CryptoError -> CryptoError
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
$cCryptoError_OutputLengthTooBig :: Constr
$cCryptoError_OutputLengthTooSmall :: Constr
$cCryptoError_SaltTooSmall :: Constr
$cCryptoError_PrimeSizeInvalid :: Constr
$cCryptoError_AuthenticationTagSizeInvalid :: Constr
$cCryptoError_MacKeyInvalid :: Constr
$cCryptoError_ScalarMultiplicationInvalid :: Constr
$cCryptoError_PointCoordinatesInvalid :: Constr
$cCryptoError_PointFormatUnsupported :: Constr
$cCryptoError_PointFormatInvalid :: Constr
$cCryptoError_PointSizeInvalid :: Constr
$cCryptoError_EcScalarOutOfBounds :: Constr
$cCryptoError_SharedSecretSizeInvalid :: Constr
$cCryptoError_PublicKeySizeInvalid :: Constr
$cCryptoError_SecretKeyStructureInvalid :: Constr
$cCryptoError_SecretKeySizeInvalid :: Constr
$cCryptoError_AEADModeNotSupported :: Constr
$cCryptoError_SeedSizeInvalid :: Constr
$cCryptoError_IvSizeInvalid :: Constr
$cCryptoError_KeySizeInvalid :: Constr
$tCryptoError :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapMp :: (forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapM :: (forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapQi :: Int -> (forall d. Data d => d -> u) -> CryptoError -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
gmapQ :: (forall d. Data d => d -> u) -> CryptoError -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
gmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
$cgmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c CryptoError)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
dataTypeOf :: CryptoError -> DataType
$cdataTypeOf :: CryptoError -> DataType
toConstr :: CryptoError -> Constr
$ctoConstr :: CryptoError -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
$cp1Data :: Typeable CryptoError
Data)
instance E.Exception CryptoError
data CryptoFailable a =
CryptoPassed a
| CryptoFailed CryptoError
deriving (Int -> CryptoFailable a -> ShowS
[CryptoFailable a] -> ShowS
CryptoFailable a -> String
(Int -> CryptoFailable a -> ShowS)
-> (CryptoFailable a -> String)
-> ([CryptoFailable a] -> ShowS)
-> Show (CryptoFailable a)
forall a. Show a => Int -> CryptoFailable a -> ShowS
forall a. Show a => [CryptoFailable a] -> ShowS
forall a. Show a => CryptoFailable a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CryptoFailable a] -> ShowS
$cshowList :: forall a. Show a => [CryptoFailable a] -> ShowS
show :: CryptoFailable a -> String
$cshow :: forall a. Show a => CryptoFailable a -> String
showsPrec :: Int -> CryptoFailable a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> CryptoFailable a -> ShowS
Show)
instance Eq a => Eq (CryptoFailable a) where
== :: CryptoFailable a -> CryptoFailable a -> Bool
(==) (CryptoPassed a
a) (CryptoPassed a
b) = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
b
(==) (CryptoFailed CryptoError
e1) (CryptoFailed CryptoError
e2) = CryptoError
e1 CryptoError -> CryptoError -> Bool
forall a. Eq a => a -> a -> Bool
== CryptoError
e2
(==) CryptoFailable a
_ CryptoFailable a
_ = Bool
False
instance Functor CryptoFailable where
fmap :: (a -> b) -> CryptoFailable a -> CryptoFailable b
fmap a -> b
f (CryptoPassed a
a) = b -> CryptoFailable b
forall a. a -> CryptoFailable a
CryptoPassed (a -> b
f a
a)
fmap a -> b
_ (CryptoFailed CryptoError
r) = CryptoError -> CryptoFailable b
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
r
instance Applicative CryptoFailable where
pure :: a -> CryptoFailable a
pure a
a = a -> CryptoFailable a
forall a. a -> CryptoFailable a
CryptoPassed a
a
<*> :: CryptoFailable (a -> b) -> CryptoFailable a -> CryptoFailable b
(<*>) CryptoFailable (a -> b)
fm CryptoFailable a
m = CryptoFailable (a -> b)
fm CryptoFailable (a -> b)
-> ((a -> b) -> CryptoFailable b) -> CryptoFailable b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a -> b
p -> CryptoFailable a
m CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r2 -> b -> CryptoFailable b
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
p a
r2)
instance Monad CryptoFailable where
return :: a -> CryptoFailable a
return = a -> CryptoFailable a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
>>= :: CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
(>>=) CryptoFailable a
m1 a -> CryptoFailable b
m2 = do
case CryptoFailable a
m1 of
CryptoPassed a
a -> a -> CryptoFailable b
m2 a
a
CryptoFailed CryptoError
e -> CryptoError -> CryptoFailable b
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
e
instance MonadFailure CryptoFailable where
type Failure CryptoFailable = CryptoError
mFail :: Failure CryptoFailable -> CryptoFailable ()
mFail = Failure CryptoFailable -> CryptoFailable ()
forall a. CryptoError -> CryptoFailable a
CryptoFailed
throwCryptoErrorIO :: CryptoFailable a -> IO a
throwCryptoErrorIO :: CryptoFailable a -> IO a
throwCryptoErrorIO (CryptoFailed CryptoError
e) = CryptoError -> IO a
forall e a. Exception e => e -> IO a
E.throwIO CryptoError
e
throwCryptoErrorIO (CryptoPassed a
r) = a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
throwCryptoError :: CryptoFailable a -> a
throwCryptoError :: CryptoFailable a -> a
throwCryptoError (CryptoFailed CryptoError
e) = CryptoError -> a
forall a e. Exception e => e -> a
E.throw CryptoError
e
throwCryptoError (CryptoPassed a
r) = a
r
onCryptoFailure :: (CryptoError -> r) -> (a -> r) -> CryptoFailable a -> r
onCryptoFailure :: (CryptoError -> r) -> (a -> r) -> CryptoFailable a -> r
onCryptoFailure CryptoError -> r
onError a -> r
_ (CryptoFailed CryptoError
e) = CryptoError -> r
onError CryptoError
e
onCryptoFailure CryptoError -> r
_ a -> r
onSuccess (CryptoPassed a
r) = a -> r
onSuccess a
r
eitherCryptoError :: CryptoFailable a -> Either CryptoError a
eitherCryptoError :: CryptoFailable a -> Either CryptoError a
eitherCryptoError (CryptoFailed CryptoError
e) = CryptoError -> Either CryptoError a
forall a b. a -> Either a b
Left CryptoError
e
eitherCryptoError (CryptoPassed a
a) = a -> Either CryptoError a
forall a b. b -> Either a b
Right a
a
maybeCryptoError :: CryptoFailable a -> Maybe a
maybeCryptoError :: CryptoFailable a -> Maybe a
maybeCryptoError (CryptoFailed CryptoError
_) = Maybe a
forall a. Maybe a
Nothing
maybeCryptoError (CryptoPassed a
r) = a -> Maybe a
forall a. a -> Maybe a
Just a
r