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

module Cardano.CLI.Options.Hash
  ( pHashCmds
  )
where

import qualified Cardano.Api.Ledger as L

import qualified Cardano.CLI.Commands.Hash as Cmd
import           Cardano.CLI.EraBased.Options.Common
import           Cardano.CLI.Parser

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

pHashCmds :: Parser Cmd.HashCmds
pHashCmds :: Parser HashCmds
pHashCmds =
  String -> ParserInfo HashCmds -> Parser HashCmds
forall a. String -> ParserInfo a -> Parser a
subParser String
"hash" (ParserInfo HashCmds -> Parser HashCmds)
-> ParserInfo HashCmds -> Parser 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])
      ( 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
  String -> ParserInfo HashCmds -> Parser HashCmds
forall a. String -> ParserInfo a -> Parser a
subParser String
"anchor-data"
    (ParserInfo HashCmds -> Parser HashCmds)
-> ParserInfo HashCmds -> Parser 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 StandardCrypto AnchorData)
-> HashAnchorDataCmdArgs
Cmd.HashAnchorDataCmdArgs
              (AnchorDataHashSource
 -> HashGoal (SafeHash StandardCrypto AnchorData)
 -> HashAnchorDataCmdArgs)
-> Parser AnchorDataHashSource
-> Parser
     (HashGoal (SafeHash StandardCrypto AnchorData)
      -> HashAnchorDataCmdArgs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser AnchorDataHashSource
pAnchorDataHashSource
              Parser
  (HashGoal (SafeHash StandardCrypto AnchorData)
   -> HashAnchorDataCmdArgs)
-> Parser (HashGoal (SafeHash StandardCrypto 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 StandardCrypto 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.StandardCrypto L.AnchorData))
pHashGoal :: Parser (HashGoal (SafeHash StandardCrypto AnchorData))
pHashGoal =
  [Parser (HashGoal (SafeHash StandardCrypto AnchorData))]
-> Parser (HashGoal (SafeHash StandardCrypto AnchorData))
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
    [ SafeHash StandardCrypto AnchorData
-> HashGoal (SafeHash StandardCrypto AnchorData)
forall hash. hash -> HashGoal hash
Cmd.CheckHash (SafeHash StandardCrypto AnchorData
 -> HashGoal (SafeHash StandardCrypto AnchorData))
-> Parser (SafeHash StandardCrypto AnchorData)
-> Parser (HashGoal (SafeHash StandardCrypto AnchorData))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (SafeHash StandardCrypto AnchorData)
pExpectedAnchorDataHash
    , File () 'Out -> HashGoal (SafeHash StandardCrypto AnchorData)
forall hash. File () 'Out -> HashGoal hash
Cmd.HashToFile (File () 'Out -> HashGoal (SafeHash StandardCrypto AnchorData))
-> Parser (File () 'Out)
-> Parser (HashGoal (SafeHash StandardCrypto 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 StandardCrypto AnchorData))
-> Parser (HashGoal (SafeHash StandardCrypto AnchorData))
-> Parser (HashGoal (SafeHash StandardCrypto AnchorData))
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> HashGoal (SafeHash StandardCrypto AnchorData)
-> Parser (HashGoal (SafeHash StandardCrypto AnchorData))
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashGoal (SafeHash StandardCrypto 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
  String -> ParserInfo HashCmds -> Parser HashCmds
forall a. String -> ParserInfo a -> Parser a
subParser String
"script"
    (ParserInfo HashCmds -> Parser HashCmds)
-> ParserInfo HashCmds -> Parser 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)."