{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Ouroboros.Storage.Orphans () where

import Data.Maybe (isJust)
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Storage.ChainDB.API
  ( ChainDbError
  , ChainDbFailure
  )
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
import Ouroboros.Consensus.Storage.ImmutableDB.API (ImmutableDBError)
import qualified Ouroboros.Consensus.Storage.ImmutableDB.API as ImmutableDB
import Ouroboros.Consensus.Storage.VolatileDB.API (VolatileDBError)
import qualified Ouroboros.Consensus.Storage.VolatileDB.API as VolatileDB
import Ouroboros.Consensus.Util.CallStack
import System.FS.API.Types (FsError, sameFsError)

{-------------------------------------------------------------------------------
  PrettyCallStack
-------------------------------------------------------------------------------}

-- | NOTE: all 'PrettyCallStack' are equal to each other.
--
-- This is useful for testing, when comparing error types that embed a
-- 'PrettyCallStack'. The call stack will differ in practice, i.e., model vs
-- implementation.
instance Eq PrettyCallStack where
  PrettyCallStack
_ == :: PrettyCallStack -> PrettyCallStack -> Bool
== PrettyCallStack
_ = Bool
True

{-------------------------------------------------------------------------------
  FS
-------------------------------------------------------------------------------}

instance Eq FsError where
  == :: FsError -> FsError -> Bool
(==) = FsError -> FsError -> Bool
sameFsError

{-------------------------------------------------------------------------------
  VolatileDB
-------------------------------------------------------------------------------}

instance Eq VolatileDB.ApiMisuse where
  VolatileDB.ClosedDBError Maybe SomeException
mbEx1 == :: ApiMisuse -> ApiMisuse -> Bool
== VolatileDB.ClosedDBError Maybe SomeException
mbEx2 =
    -- The exceptions can differ, we only care about the presence of one.
    Maybe SomeException -> Bool
forall a. Maybe a -> Bool
isJust Maybe SomeException
mbEx1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe SomeException -> Bool
forall a. Maybe a -> Bool
isJust Maybe SomeException
mbEx2

deriving instance StandardHash blk => Eq (VolatileDB.UnexpectedFailure blk)

deriving instance StandardHash blk => Eq (VolatileDBError blk)

{-------------------------------------------------------------------------------
  ImmutableDB
-------------------------------------------------------------------------------}

deriving instance StandardHash blk => Eq (ImmutableDBError blk)

deriving instance StandardHash blk => Eq (ImmutableDB.ApiMisuse blk)

deriving instance StandardHash blk => Eq (ImmutableDB.UnexpectedFailure blk)

{-------------------------------------------------------------------------------
  ChainDB
-------------------------------------------------------------------------------}

deriving instance StandardHash blk => Eq (ChainDbFailure blk)

deriving instance StandardHash blk => Eq (ChainDbError blk)