{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Witch.Utility where
import qualified Control.Exception as Exception
import qualified Data.Typeable as Typeable
import qualified GHC.Stack as Stack
import qualified Witch.Cast as Cast
import qualified Witch.Identity as Identity
import qualified Witch.TryCast as TryCast
import qualified Witch.TryCastException as TryCastException
as :: forall s source . Identity.Identity s ~ source => source -> source
as :: source -> source
as = source -> source
forall a. a -> a
id
from
:: forall s target source
. (Identity.Identity s ~ source, Cast.Cast source target)
=> source
-> target
from :: source -> target
from = source -> target
forall source target. Cast source target => source -> target
Cast.cast
into
:: forall t source target
. (Identity.Identity t ~ target, Cast.Cast source target)
=> source
-> target
into :: source -> target
into = source -> target
forall source target. Cast source target => source -> target
Cast.cast
over
:: forall t source target
. ( Identity.Identity t ~ target
, Cast.Cast source target
, Cast.Cast target source
)
=> (target -> target)
-> source
-> source
over :: (target -> target) -> source -> source
over target -> target
f = target -> source
forall source target. Cast source target => source -> target
Cast.cast (target -> source) -> (source -> target) -> source -> source
forall b c a. (b -> c) -> (a -> b) -> a -> c
. target -> target
f (target -> target) -> (source -> target) -> source -> target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. source -> target
forall source target. Cast source target => source -> target
Cast.cast
via
:: forall u source target through
. ( Identity.Identity u ~ through
, Cast.Cast source through
, Cast.Cast through target
)
=> source
-> target
via :: source -> target
via = through -> target
forall source target. Cast source target => source -> target
Cast.cast (through -> target) -> (source -> through) -> source -> target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\through
x -> through
x :: through) (through -> through) -> (source -> through) -> source -> through
forall b c a. (b -> c) -> (a -> b) -> a -> c
. source -> through
forall source target. Cast source target => source -> target
Cast.cast
tryFrom
:: forall s target source
. (Identity.Identity s ~ source, TryCast.TryCast source target)
=> source
-> Either (TryCastException.TryCastException source target) target
tryFrom :: source -> Either (TryCastException source target) target
tryFrom = source -> Either (TryCastException source target) target
forall source target.
TryCast source target =>
source -> Either (TryCastException source target) target
TryCast.tryCast
tryInto
:: forall t source target
. (Identity.Identity t ~ target, TryCast.TryCast source target)
=> source
-> Either (TryCastException.TryCastException source target) target
tryInto :: source -> Either (TryCastException source target) target
tryInto = source -> Either (TryCastException source target) target
forall source target.
TryCast source target =>
source -> Either (TryCastException source target) target
TryCast.tryCast
maybeTryCast
:: (source -> Maybe target)
-> source
-> Either (TryCastException.TryCastException source target) target
maybeTryCast :: (source -> Maybe target)
-> source -> Either (TryCastException source target) target
maybeTryCast source -> Maybe target
f source
s = case source -> Maybe target
f source
s of
Maybe target
Nothing -> TryCastException source target
-> Either (TryCastException source target) target
forall a b. a -> Either a b
Left (TryCastException source target
-> Either (TryCastException source target) target)
-> TryCastException source target
-> Either (TryCastException source target) target
forall a b. (a -> b) -> a -> b
$ source -> Maybe SomeException -> TryCastException source target
forall source target.
source -> Maybe SomeException -> TryCastException source target
TryCastException.TryCastException source
s Maybe SomeException
forall a. Maybe a
Nothing
Just target
t -> target -> Either (TryCastException source target) target
forall a b. b -> Either a b
Right target
t
eitherTryCast
:: Exception.Exception exception
=> (source -> Either exception target)
-> source
-> Either (TryCastException.TryCastException source target) target
eitherTryCast :: (source -> Either exception target)
-> source -> Either (TryCastException source target) target
eitherTryCast source -> Either exception target
f source
s = case source -> Either exception target
f source
s of
Left exception
e ->
TryCastException source target
-> Either (TryCastException source target) target
forall a b. a -> Either a b
Left (TryCastException source target
-> Either (TryCastException source target) target)
-> (SomeException -> TryCastException source target)
-> SomeException
-> Either (TryCastException source target) target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. source -> Maybe SomeException -> TryCastException source target
forall source target.
source -> Maybe SomeException -> TryCastException source target
TryCastException.TryCastException source
s (Maybe SomeException -> TryCastException source target)
-> (SomeException -> Maybe SomeException)
-> SomeException
-> TryCastException source target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SomeException -> Maybe SomeException
forall a. a -> Maybe a
Just (SomeException -> Either (TryCastException source target) target)
-> SomeException -> Either (TryCastException source target) target
forall a b. (a -> b) -> a -> b
$ exception -> SomeException
forall e. Exception e => e -> SomeException
Exception.toException exception
e
Right target
t -> target -> Either (TryCastException source target) target
forall a b. b -> Either a b
Right target
t
tryVia
:: forall u source target through
. ( Identity.Identity u ~ through
, TryCast.TryCast source through
, TryCast.TryCast through target
)
=> source
-> Either (TryCastException.TryCastException source target) target
tryVia :: source -> Either (TryCastException source target) target
tryVia source
s = case source -> Either (TryCastException source through) through
forall source target.
TryCast source target =>
source -> Either (TryCastException source target) target
TryCast.tryCast source
s of
Left (TryCastException.TryCastException source
_ Maybe SomeException
e) ->
TryCastException source target
-> Either (TryCastException source target) target
forall a b. a -> Either a b
Left (TryCastException source target
-> Either (TryCastException source target) target)
-> TryCastException source target
-> Either (TryCastException source target) target
forall a b. (a -> b) -> a -> b
$ source -> Maybe SomeException -> TryCastException source target
forall source target.
source -> Maybe SomeException -> TryCastException source target
TryCastException.TryCastException source
s Maybe SomeException
e
Right through
u -> case through -> Either (TryCastException through target) target
forall source target.
TryCast source target =>
source -> Either (TryCastException source target) target
TryCast.tryCast (through
u :: through) of
Left (TryCastException.TryCastException through
_ Maybe SomeException
e) ->
TryCastException source target
-> Either (TryCastException source target) target
forall a b. a -> Either a b
Left (TryCastException source target
-> Either (TryCastException source target) target)
-> TryCastException source target
-> Either (TryCastException source target) target
forall a b. (a -> b) -> a -> b
$ source -> Maybe SomeException -> TryCastException source target
forall source target.
source -> Maybe SomeException -> TryCastException source target
TryCastException.TryCastException source
s Maybe SomeException
e
Right target
t -> target -> Either (TryCastException source target) target
forall a b. b -> Either a b
Right target
t
unsafeCast
:: forall source target
. ( Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeCast :: source -> target
unsafeCast = (TryCastException source target -> target)
-> (target -> target)
-> Either (TryCastException source target) target
-> target
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either TryCastException source target -> target
forall a e. Exception e => e -> a
Exception.throw target -> target
forall a. a -> a
id (Either (TryCastException source target) target -> target)
-> (source -> Either (TryCastException source target) target)
-> source
-> target
forall b c a. (b -> c) -> (a -> b) -> a -> c
. source -> Either (TryCastException source target) target
forall source target.
TryCast source target =>
source -> Either (TryCastException source target) target
TryCast.tryCast
unsafeFrom
:: forall s target source
. ( Identity.Identity s ~ source
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeFrom :: source -> target
unsafeFrom = source -> target
forall source target.
(HasCallStack, TryCast source target, Show source, Typeable source,
Typeable target) =>
source -> target
unsafeCast
unsafeInto
:: forall t source target
. ( Identity.Identity t ~ target
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeInto :: source -> target
unsafeInto = source -> target
forall source target.
(HasCallStack, TryCast source target, Show source, Typeable source,
Typeable target) =>
source -> target
unsafeCast