-- | Provides exception types/functions for usage with path-size.
--
-- @since 0.1
module PathSize.Exception
  ( PathE (..),
  )
where

import Control.DeepSeq (NFData)
import Control.Exception (Exception (displayException))
import Effects.FileSystem.OsPath (OsPath, decodeLenient)
import GHC.Generics (Generic)

-- | Exception for a path. The second param is the reason i.e. the exceptions'
-- displayException. The reason we convert to a string rather than leave it
-- as an exception is so we can have an NFData instance for benchmarking.
--
-- @since 0.1
data PathE = MkPathE !OsPath !String
  deriving stock
    ( -- | @since 0.1
      PathE -> PathE -> Bool
(PathE -> PathE -> Bool) -> (PathE -> PathE -> Bool) -> Eq PathE
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PathE -> PathE -> Bool
== :: PathE -> PathE -> Bool
$c/= :: PathE -> PathE -> Bool
/= :: PathE -> PathE -> Bool
Eq,
      -- | @since 0.1
      (forall x. PathE -> Rep PathE x)
-> (forall x. Rep PathE x -> PathE) -> Generic PathE
forall x. Rep PathE x -> PathE
forall x. PathE -> Rep PathE x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PathE -> Rep PathE x
from :: forall x. PathE -> Rep PathE x
$cto :: forall x. Rep PathE x -> PathE
to :: forall x. Rep PathE x -> PathE
Generic,
      -- | @since 0.1
      Eq PathE
Eq PathE =>
(PathE -> PathE -> Ordering)
-> (PathE -> PathE -> Bool)
-> (PathE -> PathE -> Bool)
-> (PathE -> PathE -> Bool)
-> (PathE -> PathE -> Bool)
-> (PathE -> PathE -> PathE)
-> (PathE -> PathE -> PathE)
-> Ord PathE
PathE -> PathE -> Bool
PathE -> PathE -> Ordering
PathE -> PathE -> PathE
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PathE -> PathE -> Ordering
compare :: PathE -> PathE -> Ordering
$c< :: PathE -> PathE -> Bool
< :: PathE -> PathE -> Bool
$c<= :: PathE -> PathE -> Bool
<= :: PathE -> PathE -> Bool
$c> :: PathE -> PathE -> Bool
> :: PathE -> PathE -> Bool
$c>= :: PathE -> PathE -> Bool
>= :: PathE -> PathE -> Bool
$cmax :: PathE -> PathE -> PathE
max :: PathE -> PathE -> PathE
$cmin :: PathE -> PathE -> PathE
min :: PathE -> PathE -> PathE
Ord,
      -- | @since 0.1
      Int -> PathE -> ShowS
[PathE] -> ShowS
PathE -> String
(Int -> PathE -> ShowS)
-> (PathE -> String) -> ([PathE] -> ShowS) -> Show PathE
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PathE -> ShowS
showsPrec :: Int -> PathE -> ShowS
$cshow :: PathE -> String
show :: PathE -> String
$cshowList :: [PathE] -> ShowS
showList :: [PathE] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.1
      PathE -> ()
(PathE -> ()) -> NFData PathE
forall a. (a -> ()) -> NFData a
$crnf :: PathE -> ()
rnf :: PathE -> ()
NFData
    )

-- | @since 0.1
instance Exception PathE where
  displayException :: PathE -> String
displayException (MkPathE OsPath
p String
e) =
    [String] -> String
forall a. Monoid a => [a] -> a
mconcat
      [ String
"Error for path '",
        OsPath -> String
decodeLenient OsPath
p,
        String
"': ",
        String
e
      ]