summaryrefslogtreecommitdiff
path: root/src/crypto/reflibs/Crypto/Error.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/crypto/reflibs/Crypto/Error.hs')
-rw-r--r--src/crypto/reflibs/Crypto/Error.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/crypto/reflibs/Crypto/Error.hs b/src/crypto/reflibs/Crypto/Error.hs
new file mode 100644
index 0000000..8f03db7
--- /dev/null
+++ b/src/crypto/reflibs/Crypto/Error.hs
@@ -0,0 +1,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