{-# LANGUAGE PackageImports #-}
module Refined.Extras.Utils
(
pattern MkRefined,
showRefineException,
showtRefineException,
refineExceptionToType,
)
where
import Data.Text (Text)
import Data.Text qualified as T
import Data.Typeable (TypeRep)
import Refined
( RefineException
( RefineAndException,
RefineNotException,
RefineOrException,
RefineOtherException,
RefineSomeException,
RefineXorException
),
)
import Refined.Unsafe.Type (Refined (Refined))
import "these-skinny" Data.These (These (That, These, This))
pattern MkRefined :: a -> Refined p a
pattern $mMkRefined :: forall {r} {k} {a} {p :: k}.
Refined p a -> (a -> r) -> ((# #) -> r) -> r
MkRefined a <- Refined a
{-# COMPLETE MkRefined #-}
showRefineException :: RefineException -> String
showRefineException :: RefineException -> String
showRefineException = Text -> String
T.unpack (Text -> String)
-> (RefineException -> Text) -> RefineException -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RefineException -> Text
showtRefineException
showtRefineException :: RefineException -> Text
showtRefineException :: RefineException -> Text
showtRefineException (RefineNotException TypeRep
ty) = Text
"RefineNotException " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty
showtRefineException (RefineAndException TypeRep
ty These RefineException RefineException
th) =
Text
"RefineAndException " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> These RefineException RefineException -> Text
showThese These RefineException RefineException
th Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showtRefineException (RefineOrException TypeRep
ty RefineException
e1 RefineException
e2) =
Text
"RefineOrException "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e1
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e2
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showtRefineException (RefineXorException TypeRep
ty Maybe (RefineException, RefineException)
mEx) =
Text
"RefineXorException " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> case Maybe (RefineException, RefineException)
mEx of
Maybe (RefineException, RefineException)
Nothing -> Text
" Nothing"
Just (RefineException
e1, RefineException
e2) ->
Text
" ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e1
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e2
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showtRefineException (RefineSomeException TypeRep
ty SomeException
someEx) =
Text
"RefineSomeException " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (SomeException -> String
forall a. Show a => a -> String
show SomeException
someEx) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showtRefineException (RefineOtherException TypeRep
ty Text
txt) =
Text
"RefineOtherException " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TypeRep -> Text
forall a. Show a => a -> Text
showParens TypeRep
ty Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Text -> String
forall a. Show a => a -> String
show Text
txt)
refineExceptionToType :: RefineException -> TypeRep
refineExceptionToType :: RefineException -> TypeRep
refineExceptionToType (RefineNotException TypeRep
ty) = TypeRep
ty
refineExceptionToType (RefineAndException TypeRep
ty These RefineException RefineException
_) = TypeRep
ty
refineExceptionToType (RefineOrException TypeRep
ty RefineException
_ RefineException
_) = TypeRep
ty
refineExceptionToType (RefineXorException TypeRep
ty Maybe (RefineException, RefineException)
_) = TypeRep
ty
refineExceptionToType (RefineSomeException TypeRep
ty SomeException
_) = TypeRep
ty
refineExceptionToType (RefineOtherException TypeRep
ty Text
_) = TypeRep
ty
showParens :: (Show a) => a -> Text
showParens :: forall a. Show a => a -> Text
showParens a
ty = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (a -> String
forall a. Show a => a -> String
show a
ty) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showThese :: These RefineException RefineException -> Text
showThese :: These RefineException RefineException -> Text
showThese (These RefineException
e1 RefineException
e2) =
Text
"These ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e1
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e2
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showThese (This RefineException
e1) =
Text
"This ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e1
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
showThese (That RefineException
e2) =
Text
"That ("
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RefineException -> Text
showtRefineException RefineException
e2
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"