{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Compatible.Run
  ( CompatibleCmdError
  , renderCompatibleCmdError
  , runAnyCompatibleCommand
  , runCompatibleCommand
  )
where

import           Cardano.Api

import           Cardano.CLI.Compatible.Commands
import           Cardano.CLI.Compatible.Governance
import           Cardano.CLI.Compatible.Transaction
import           Cardano.CLI.Render
import           Cardano.CLI.Types.Errors.CmdError

import           Data.Text (Text)

data CompatibleCmdError
  = CompatibleTransactionError CompatibleTransactionError
  | CompatibleGovernanceError CmdError

renderCompatibleCmdError :: Text -> CompatibleCmdError -> Doc ann
renderCompatibleCmdError :: forall ann. Text -> CompatibleCmdError -> Doc ann
renderCompatibleCmdError Text
cmdText = \case
  CompatibleTransactionError CompatibleTransactionError
e -> Text
-> (CompatibleTransactionError -> Doc ann)
-> CompatibleTransactionError
-> Doc ann
forall a ann. Text -> (a -> Doc ann) -> a -> Doc ann
renderAnyCmdError Text
cmdText CompatibleTransactionError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. CompatibleTransactionError -> Doc ann
prettyError CompatibleTransactionError
e
  CompatibleGovernanceError CmdError
e -> Text -> CmdError -> Doc ann
forall ann. Text -> CmdError -> Doc ann
renderCmdError Text
cmdText CmdError
e

runAnyCompatibleCommand :: AnyCompatibleCommand -> ExceptT CompatibleCmdError IO ()
runAnyCompatibleCommand :: AnyCompatibleCommand -> ExceptT CompatibleCmdError IO ()
runAnyCompatibleCommand (AnyCompatibleCommand CompatibleCommand era
cmd) = CompatibleCommand era -> ExceptT CompatibleCmdError IO ()
forall era.
CompatibleCommand era -> ExceptT CompatibleCmdError IO ()
runCompatibleCommand CompatibleCommand era
cmd

runCompatibleCommand :: CompatibleCommand era -> ExceptT CompatibleCmdError IO ()
runCompatibleCommand :: forall era.
CompatibleCommand era -> ExceptT CompatibleCmdError IO ()
runCompatibleCommand (CompatibleTransactionCmd CompatibleTransactionCmds era
txCmd) =
  (CompatibleTransactionError -> CompatibleCmdError)
-> ExceptT CompatibleTransactionError IO ()
-> ExceptT CompatibleCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT CompatibleTransactionError -> CompatibleCmdError
CompatibleTransactionError (ExceptT CompatibleTransactionError IO ()
 -> ExceptT CompatibleCmdError IO ())
-> ExceptT CompatibleTransactionError IO ()
-> ExceptT CompatibleCmdError IO ()
forall a b. (a -> b) -> a -> b
$ CompatibleTransactionCmds era
-> ExceptT CompatibleTransactionError IO ()
forall era.
CompatibleTransactionCmds era
-> ExceptT CompatibleTransactionError IO ()
runCompatibleTransactionCmd CompatibleTransactionCmds era
txCmd
runCompatibleCommand (CompatibleGovernanceCmds CompatibleGovernanceCmds era
govCmd) =
  (CmdError -> CompatibleCmdError)
-> ExceptT CmdError IO () -> ExceptT CompatibleCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT CmdError -> CompatibleCmdError
CompatibleGovernanceError (ExceptT CmdError IO () -> ExceptT CompatibleCmdError IO ())
-> ExceptT CmdError IO () -> ExceptT CompatibleCmdError IO ()
forall a b. (a -> b) -> a -> b
$ CompatibleGovernanceCmds era -> ExceptT CmdError IO ()
forall era. CompatibleGovernanceCmds era -> ExceptT CmdError IO ()
runCompatibleGovernanceCmds CompatibleGovernanceCmds era
govCmd