{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

module Cardano.CLI.EraIndependent.Hash.Option
  ( pHashCmds
  )
where

import Cardano.Api.Ledger qualified as L

import Cardano.CLI.EraBased.Common.Option
import Cardano.CLI.EraIndependent.Hash.Command qualified as Cmd
import Cardano.CLI.Parser

import Data.Foldable
import Options.Applicative
import Options.Applicative qualified as Opt

pHashCmds :: Parser Cmd.HashCmds
pHashCmds :: Parser HashCmds
pHashCmds =
  Mod CommandFields HashCmds -> Parser HashCmds
forall a. Mod CommandFields a -> Parser a
Opt.hsubparser (Mod CommandFields HashCmds -> Parser HashCmds)
-> Mod CommandFields HashCmds -> Parser HashCmds
forall a b. (a -> b) -> a -> b
$
    String -> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a. String -> ParserInfo a -> Mod CommandFields a
commandWithMetavar String
"hash" (ParserInfo HashCmds -> Mod CommandFields HashCmds)
-> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a b. (a -> b) -> a -> b
$
      Parser HashCmds -> InfoMod HashCmds -> ParserInfo HashCmds
forall a. Parser a -> InfoMod a -> ParserInfo a
Opt.info ([Parser HashCmds] -> Parser HashCmds
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [Parser HashCmds
pHashAnchorDataCmd, Parser HashCmds
pHashScriptCmd, Parser HashCmds
pHashGenesisHashCmd]) (InfoMod HashCmds -> ParserInfo HashCmds)
-> InfoMod HashCmds -> ParserInfo HashCmds
forall a b. (a -> b) -> a -> b
$
        String -> InfoMod HashCmds
forall a. String -> InfoMod a
Opt.progDesc (String -> InfoMod HashCmds) -> String -> InfoMod HashCmds
forall a b. (a -> b) -> a -> b
$
          [String] -> String
forall a. Monoid a => [a] -> a
mconcat
            [ String
"Compute the hash to pass to the various --*-hash arguments of commands."
            ]

pHashAnchorDataCmd :: Parser Cmd.HashCmds
pHashAnchorDataCmd :: Parser HashCmds
pHashAnchorDataCmd = do
  Mod CommandFields HashCmds -> Parser HashCmds
forall a. Mod CommandFields a -> Parser a
Opt.hsubparser
    (Mod CommandFields HashCmds -> Parser HashCmds)
-> Mod CommandFields HashCmds -> Parser HashCmds
forall a b. (a -> b) -> a -> b
$ String -> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a. String -> ParserInfo a -> Mod CommandFields a
commandWithMetavar String
"anchor-data"
    (ParserInfo HashCmds -> Mod CommandFields HashCmds)
-> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a b. (a -> b) -> a -> b
$ Parser HashCmds -> InfoMod HashCmds -> ParserInfo HashCmds
forall a. Parser a -> InfoMod a -> ParserInfo a
Opt.info
      ( (HashAnchorDataCmdArgs -> HashCmds)
-> Parser HashAnchorDataCmdArgs -> Parser HashCmds
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
          HashAnchorDataCmdArgs -> HashCmds
Cmd.HashAnchorDataCmd
          ( AnchorDataHashSource
-> HashGoal (SafeHash AnchorData) -> HashAnchorDataCmdArgs
Cmd.HashAnchorDataCmdArgs
              (AnchorDataHashSource
 -> HashGoal (SafeHash AnchorData) -> HashAnchorDataCmdArgs)
-> Parser AnchorDataHashSource
-> Parser (HashGoal (SafeHash AnchorData) -> HashAnchorDataCmdArgs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser AnchorDataHashSource
pAnchorDataHashSource
              Parser (HashGoal (SafeHash AnchorData) -> HashAnchorDataCmdArgs)
-> Parser (HashGoal (SafeHash AnchorData))
-> Parser HashAnchorDataCmdArgs
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (HashGoal (SafeHash AnchorData))
pHashGoal
          )
      )
    (InfoMod HashCmds -> ParserInfo HashCmds)
-> InfoMod HashCmds -> ParserInfo HashCmds
forall a b. (a -> b) -> a -> b
$ String -> InfoMod HashCmds
forall a. String -> InfoMod a
Opt.progDesc String
"Compute the hash of some anchor data (to then pass it to other commands)."

pHashGoal :: Parser (Cmd.HashGoal (L.SafeHash L.AnchorData))
pHashGoal :: Parser (HashGoal (SafeHash AnchorData))
pHashGoal =
  [Parser (HashGoal (SafeHash AnchorData))]
-> Parser (HashGoal (SafeHash AnchorData))
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
    [ SafeHash AnchorData -> HashGoal (SafeHash AnchorData)
forall hash. hash -> HashGoal hash
Cmd.CheckHash (SafeHash AnchorData -> HashGoal (SafeHash AnchorData))
-> Parser (SafeHash AnchorData)
-> Parser (HashGoal (SafeHash AnchorData))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (SafeHash AnchorData)
pExpectedAnchorDataHash
    , File () 'Out -> HashGoal (SafeHash AnchorData)
forall hash. File () 'Out -> HashGoal hash
Cmd.HashToFile (File () 'Out -> HashGoal (SafeHash AnchorData))
-> Parser (File () 'Out) -> Parser (HashGoal (SafeHash AnchorData))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (File () 'Out)
forall content. Parser (File content 'Out)
pOutputFile
    ]
    Parser (HashGoal (SafeHash AnchorData))
-> Parser (HashGoal (SafeHash AnchorData))
-> Parser (HashGoal (SafeHash AnchorData))
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> HashGoal (SafeHash AnchorData)
-> Parser (HashGoal (SafeHash AnchorData))
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashGoal (SafeHash AnchorData)
forall hash. HashGoal hash
Cmd.HashToStdout

pAnchorDataHashSource :: Parser Cmd.AnchorDataHashSource
pAnchorDataHashSource :: Parser AnchorDataHashSource
pAnchorDataHashSource =
  [Parser AnchorDataHashSource] -> Parser AnchorDataHashSource
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
    [ Text -> AnchorDataHashSource
Cmd.AnchorDataHashSourceText
        (Text -> AnchorDataHashSource)
-> Parser Text -> Parser AnchorDataHashSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mod OptionFields Text -> Parser Text
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption
          ( [Mod OptionFields Text] -> Mod OptionFields Text
forall a. Monoid a => [a] -> a
mconcat
              [ String -> Mod OptionFields Text
forall (f :: * -> *) a. HasName f => String -> Mod f a
Opt.long String
"text"
              , String -> Mod OptionFields Text
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
Opt.metavar String
"TEXT"
              , String -> Mod OptionFields Text
forall (f :: * -> *) a. String -> Mod f a
Opt.help String
"Text to hash as UTF-8"
              ]
          )
    , File ProposalBinary 'In -> AnchorDataHashSource
Cmd.AnchorDataHashSourceBinaryFile
        (File ProposalBinary 'In -> AnchorDataHashSource)
-> Parser (File ProposalBinary 'In) -> Parser AnchorDataHashSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Parser (File ProposalBinary 'In)
forall a. String -> String -> Parser (File a 'In)
pFileInDirection String
"file-binary" String
"Binary file to hash"
    , File ProposalText 'In -> AnchorDataHashSource
Cmd.AnchorDataHashSourceTextFile
        (File ProposalText 'In -> AnchorDataHashSource)
-> Parser (File ProposalText 'In) -> Parser AnchorDataHashSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Parser (File ProposalText 'In)
forall a. String -> String -> Parser (File a 'In)
pFileInDirection String
"file-text" String
"Text file to hash"
    , Url -> AnchorDataHashSource
Cmd.AnchorDataHashSourceURL
        (Url -> AnchorDataHashSource)
-> Parser Url -> Parser AnchorDataHashSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> String -> Parser Url
pUrl String
"url" String
"A URL to the file to hash (HTTP(S) and IPFS only)"
    ]

pHashScriptCmd :: Parser Cmd.HashCmds
pHashScriptCmd :: Parser HashCmds
pHashScriptCmd = do
  Mod CommandFields HashCmds -> Parser HashCmds
forall a. Mod CommandFields a -> Parser a
Opt.hsubparser
    (Mod CommandFields HashCmds -> Parser HashCmds)
-> Mod CommandFields HashCmds -> Parser HashCmds
forall a b. (a -> b) -> a -> b
$ String -> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a. String -> ParserInfo a -> Mod CommandFields a
commandWithMetavar String
"script"
    (ParserInfo HashCmds -> Mod CommandFields HashCmds)
-> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a b. (a -> b) -> a -> b
$ Parser HashCmds -> InfoMod HashCmds -> ParserInfo HashCmds
forall a. Parser a -> InfoMod a -> ParserInfo a
Opt.info
      ( (HashScriptCmdArgs -> HashCmds)
-> Parser HashScriptCmdArgs -> Parser HashCmds
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
          HashScriptCmdArgs -> HashCmds
Cmd.HashScriptCmd
          ( ScriptFile -> Maybe (File () 'Out) -> HashScriptCmdArgs
Cmd.HashScriptCmdArgs
              (ScriptFile -> Maybe (File () 'Out) -> HashScriptCmdArgs)
-> Parser ScriptFile
-> Parser (Maybe (File () 'Out) -> HashScriptCmdArgs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ScriptFile
pScript
              Parser (Maybe (File () 'Out) -> HashScriptCmdArgs)
-> Parser (Maybe (File () 'Out)) -> Parser HashScriptCmdArgs
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (File () 'Out) -> Parser (Maybe (File () 'Out))
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser (File () 'Out)
forall content. Parser (File content 'Out)
pOutputFile
          )
      )
    (InfoMod HashCmds -> ParserInfo HashCmds)
-> InfoMod HashCmds -> ParserInfo HashCmds
forall a b. (a -> b) -> a -> b
$ String -> InfoMod HashCmds
forall a. String -> InfoMod a
Opt.progDesc String
"Compute the hash of a script (to then pass it to other commands)."

pHashGenesisHashCmd :: Parser Cmd.HashCmds
pHashGenesisHashCmd :: Parser HashCmds
pHashGenesisHashCmd =
  Mod CommandFields HashCmds -> Parser HashCmds
forall a. Mod CommandFields a -> Parser a
Opt.hsubparser (Mod CommandFields HashCmds -> Parser HashCmds)
-> Mod CommandFields HashCmds -> Parser HashCmds
forall a b. (a -> b) -> a -> b
$
    String -> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a. String -> ParserInfo a -> Mod CommandFields a
commandWithMetavar String
"genesis-file" (ParserInfo HashCmds -> Mod CommandFields HashCmds)
-> ParserInfo HashCmds -> Mod CommandFields HashCmds
forall a b. (a -> b) -> a -> b
$
      Parser HashCmds -> InfoMod HashCmds -> ParserInfo HashCmds
forall a. Parser a -> InfoMod a -> ParserInfo a
Opt.info Parser HashCmds
pGenesisHash (InfoMod HashCmds -> ParserInfo HashCmds)
-> InfoMod HashCmds -> ParserInfo HashCmds
forall a b. (a -> b) -> a -> b
$
        String -> InfoMod HashCmds
forall a. String -> InfoMod a
Opt.progDesc String
"Compute the hash of a genesis file."

pGenesisHash :: Parser Cmd.HashCmds
pGenesisHash :: Parser HashCmds
pGenesisHash =
  GenesisFile -> HashCmds
Cmd.HashGenesisFile (GenesisFile -> HashCmds) -> Parser GenesisFile -> Parser HashCmds
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Parser GenesisFile
pGenesisFile String
"The genesis file."