{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.TypedEncoding.Common.Types.Exceptions where
import Data.Proxy
import GHC.TypeLits
data RecreateEx where
RecreateEx:: (Show e, KnownSymbol x) => Proxy x -> e -> RecreateEx
RecreateExUnkStep:: (Show e) => e -> RecreateEx
instance Show RecreateEx where
show (RecreateEx prxy a) = "(RecreateEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"
show (RecreateExUnkStep a) = "(UnknownDecodeStep (" ++ show a ++ "))"
recreateErrUnknown :: (Show e) => e -> RecreateEx
recreateErrUnknown = RecreateExUnkStep
data EncodeEx where
EncodeEx:: (Show a, KnownSymbol x) => Proxy x -> a -> EncodeEx
instance Show EncodeEx where
show (EncodeEx prxy a) = "(EncodeEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"
asEncodeEx :: (Show a, KnownSymbol x) => Proxy x -> Either a b -> Either EncodeEx b
asEncodeEx p = either (Left . EncodeEx p) Right
encToRecrEx :: EncodeEx -> RecreateEx
encToRecrEx (EncodeEx p a) = RecreateEx p a
mergeEncodeEx :: KnownSymbol x => Proxy x -> EncodeEx -> Maybe EncodeEx -> EncodeEx
mergeEncodeEx _ ex Nothing = ex
mergeEncodeEx p (EncodeEx _ a) (Just (EncodeEx _ b)) = EncodeEx p $ "Errors: " ++ show (a,b)
emptyEncErr :: KnownSymbol x => Proxy x -> EncodeEx
emptyEncErr p = EncodeEx p ("unexpected" :: String)
data UnexpectedDecodeEx where
UnexpectedDecodeEx :: (Show a, KnownSymbol x) => Proxy x -> a -> UnexpectedDecodeEx
instance Show UnexpectedDecodeEx where
show (UnexpectedDecodeEx prxy a) = "(UnexpectedDecodeEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"
mergeErrs :: err -> (err -> Maybe err -> err) -> Either err a -> Either err b -> Either err c
mergeErrs _ fn (Left er1) (Left er2) = Left (fn er1 $ Just er2)
mergeErrs _ fn (Left er1) _ = Left (fn er1 Nothing)
mergeErrs _ fn _ (Left er2) = Left (fn er2 Nothing)
mergeErrs de fn (Right r) (Right y) = Left de