{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.Types.Errors.StakePoolCmdError
  ( StakePoolCmdError (..)
  , renderStakePoolCmdError
  )
where

import           Cardano.Api
import           Cardano.Api.Shelley (Hash (StakePoolMetadataHash))

import           Cardano.CLI.Types.Errors.HashCmdError (FetchURLError)

data StakePoolCmdError
  = StakePoolCmdReadFileError !(FileError TextEnvelopeError)
  | StakePoolCmdReadKeyFileError !(FileError InputDecodeError)
  | StakePoolCmdWriteFileError !(FileError ())
  | StakePoolCmdMetadataValidationError !StakePoolMetadataValidationError
  | StakePoolCmdHashMismatchError
      !(Hash StakePoolMetadata)
      -- ^ Expected hash
      !(Hash StakePoolMetadata)
      -- ^ Actual hash
  | StakePoolCmdFetchURLError !FetchURLError
  deriving Int -> StakePoolCmdError -> ShowS
[StakePoolCmdError] -> ShowS
StakePoolCmdError -> String
(Int -> StakePoolCmdError -> ShowS)
-> (StakePoolCmdError -> String)
-> ([StakePoolCmdError] -> ShowS)
-> Show StakePoolCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StakePoolCmdError -> ShowS
showsPrec :: Int -> StakePoolCmdError -> ShowS
$cshow :: StakePoolCmdError -> String
show :: StakePoolCmdError -> String
$cshowList :: [StakePoolCmdError] -> ShowS
showList :: [StakePoolCmdError] -> ShowS
Show

renderStakePoolCmdError :: StakePoolCmdError -> Doc ann
renderStakePoolCmdError :: forall ann. StakePoolCmdError -> Doc ann
renderStakePoolCmdError = \case
  StakePoolCmdMetadataValidationError StakePoolMetadataValidationError
validationErr ->
    Doc ann
"Error validating stake pool metadata: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> StakePoolMetadataValidationError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. StakePoolMetadataValidationError -> Doc ann
prettyError StakePoolMetadataValidationError
validationErr
  StakePoolCmdReadFileError FileError TextEnvelopeError
fileErr ->
    FileError TextEnvelopeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError TextEnvelopeError -> Doc ann
prettyError FileError TextEnvelopeError
fileErr
  StakePoolCmdReadKeyFileError FileError InputDecodeError
fileErr ->
    FileError InputDecodeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError InputDecodeError -> Doc ann
prettyError FileError InputDecodeError
fileErr
  StakePoolCmdWriteFileError FileError ()
fileErr ->
    FileError () -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError () -> Doc ann
prettyError FileError ()
fileErr
  StakePoolCmdHashMismatchError
    (StakePoolMetadataHash Hash StandardCrypto ByteString
expectedHash)
    (StakePoolMetadataHash Hash StandardCrypto ByteString
actualHash) ->
      Doc ann
"Hashes do not match!"
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\nExpected:"
          Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Hash Blake2b_256 ByteString -> String
forall a. Show a => a -> String
show Hash Blake2b_256 ByteString
Hash StandardCrypto ByteString
expectedHash)
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\n  Actual:"
          Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Hash Blake2b_256 ByteString -> String
forall a. Show a => a -> String
show Hash Blake2b_256 ByteString
Hash StandardCrypto ByteString
actualHash)
  StakePoolCmdFetchURLError FetchURLError
fetchErr ->
    Doc ann
"Error fetching stake pool metadata: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FetchURLError -> Doc ann
forall a ann. Exception a => a -> Doc ann
prettyException FetchURLError
fetchErr