summaryrefslogtreecommitdiff
path: root/src/crypto/reflibs/Crypto/Error.hs
blob: 8f03db744af3c5239db937b1bdfa8ea2b23c8868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Crypto.Error (
    CryptoError(..),
    CryptoFailable(..),

    throwCryptoErrorIO,
    maybeCryptoError,
) where

import Control.Exception
import Control.Monad


data CryptoError = CryptoError
    deriving (Show)

instance Exception CryptoError

data CryptoFailable a
    = CryptoPassed a
    | CryptoFailed CryptoError
    deriving (Show)

instance Functor CryptoFailable where
    fmap f (CryptoPassed x) = CryptoPassed (f x)
    fmap _ (CryptoFailed e) = CryptoFailed e

instance Applicative CryptoFailable where
    pure = CryptoPassed
    (<*>) = ap

instance Monad CryptoFailable where
    CryptoPassed x >>= f = f x
    CryptoFailed e >>= _ = CryptoFailed e

throwCryptoErrorIO :: CryptoFailable a -> IO a
throwCryptoErrorIO (CryptoPassed r) = return r
throwCryptoErrorIO (CryptoFailed e) = throwIO e

maybeCryptoError :: CryptoFailable a -> Maybe a
maybeCryptoError (CryptoPassed x) = Just x
maybeCryptoError (CryptoFailed _) = Nothing