{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE StrictData                #-}

{-|
Module      : Data.VCS.Ignore.Types
Description : Shared data types
Copyright   : (c) 2020-2021 Vaclav Svejcar
License     : BSD-3-Clause
Maintainer  : vaclav.svejcar@gmail.com
Stability   : experimental
Portability : POSIX

This module contains data types and functions shared across the library.
-}

module Data.VCS.Ignore.Types
  ( VCSIgnoreError(..)
  , fromVCSIgnoreError
  , toVCSIgnoreError
  )
where

import           Control.Exception              ( Exception(..)
                                                , SomeException
                                                )
import           Data.Typeable                  ( cast )


---------------------------------  DATA TYPES  ---------------------------------

-- | Top-level of any exception thrown by this library.
data VCSIgnoreError = forall e . Exception e => VCSIgnoreError e

instance Show VCSIgnoreError where
  show :: VCSIgnoreError -> String
show (VCSIgnoreError e
e) = e -> String
forall a. Show a => a -> String
show e
e

instance Exception VCSIgnoreError where
  displayException :: VCSIgnoreError -> String
displayException (VCSIgnoreError e
e) = e -> String
forall e. Exception e => e -> String
displayException e
e


------------------------------  PUBLIC FUNCTIONS  ------------------------------

-- | Unwraps given exception from 'VCSIgnoreError'.
fromVCSIgnoreError :: Exception e
                   => SomeException
                   -- ^ exception to unwrap
                   -> Maybe e
                   -- ^ unwrapped exception
fromVCSIgnoreError :: SomeException -> Maybe e
fromVCSIgnoreError SomeException
e = do
  VCSIgnoreError e
e' <- SomeException -> Maybe VCSIgnoreError
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e
  e -> Maybe e
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast e
e'


-- | Wraps given exception from 'VCSIgnoreError'.
toVCSIgnoreError :: Exception e
                 => e
                 -- ^ exception to wrap
                 -> SomeException
                 -- ^ wrapped exception
toVCSIgnoreError :: e -> SomeException
toVCSIgnoreError = VCSIgnoreError -> SomeException
forall e. Exception e => e -> SomeException
toException (VCSIgnoreError -> SomeException)
-> (e -> VCSIgnoreError) -> e -> SomeException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> VCSIgnoreError
forall e. Exception e => e -> VCSIgnoreError
VCSIgnoreError