{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.EraIndependent.Cip.Common
  ( -- * Input related
    Input (..)
  , InputError (..)
  , pInputFile
  , pInputHexText
  , pInputBech32Text

    -- * Output related
  , Output (..)
  , pOutputFile
  , pOutputText
  )
where

import Cardano.Api

import Cardano.CLI.EraBased.Common.Option hiding (pOutputFile)

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

data Input
  = InputTextEnvelopeFile (File () In)
  | InputHexText Text
  | InputBech32Text Text

newtype InputError = InputError Text deriving Int -> InputError -> ShowS
[InputError] -> ShowS
InputError -> [Char]
(Int -> InputError -> ShowS)
-> (InputError -> [Char])
-> ([InputError] -> ShowS)
-> Show InputError
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputError -> ShowS
showsPrec :: Int -> InputError -> ShowS
$cshow :: InputError -> [Char]
show :: InputError -> [Char]
$cshowList :: [InputError] -> ShowS
showList :: [InputError] -> ShowS
Show

instance Error InputError where
  prettyError :: forall ann. InputError -> Doc ann
prettyError (InputError Text
err) = Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Text
err

pInputFile :: String -> String -> Opt.Parser Input
pInputFile :: [Char] -> [Char] -> Parser Input
pInputFile [Char]
optName [Char]
desc =
  File () 'In -> Input
InputTextEnvelopeFile (File () 'In -> Input) -> Parser (File () 'In) -> Parser Input
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [Char] -> Parser (File () 'In)
forall a. [Char] -> [Char] -> Parser (File a 'In)
pFileInDirection [Char]
optName [Char]
desc

pInputHexText :: String -> String -> String -> Opt.Parser Input
pInputHexText :: [Char] -> [Char] -> [Char] -> Parser Input
pInputHexText [Char]
optName [Char]
metavar [Char]
help =
  ([Char] -> Input) -> Parser [Char] -> Parser Input
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Input
InputHexText (Text -> Input) -> ([Char] -> Text) -> [Char] -> Input
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
Text.pack) (Parser [Char] -> Parser Input) -> Parser [Char] -> Parser Input
forall a b. (a -> b) -> a -> b
$
    Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$
      [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat
        [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
optName
        , [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
metavar
        , [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
help
        ]

pInputBech32Text :: String -> String -> String -> Opt.Parser Input
pInputBech32Text :: [Char] -> [Char] -> [Char] -> Parser Input
pInputBech32Text [Char]
optName [Char]
metavar [Char]
help =
  ([Char] -> Input) -> Parser [Char] -> Parser Input
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Input
InputBech32Text (Text -> Input) -> ([Char] -> Text) -> [Char] -> Input
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
Text.pack) (Parser [Char] -> Parser Input) -> Parser [Char] -> Parser Input
forall a b. (a -> b) -> a -> b
$
    Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$
      [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat
        [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
optName
        , [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
metavar
        , [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
help
        ]

data Output
  = OutputFile (File () Out)
  | OutputText

pOutputFile :: String -> String -> Opt.Parser Output
pOutputFile :: [Char] -> [Char] -> Parser Output
pOutputFile [Char]
optName [Char]
desc =
  File () 'Out -> Output
OutputFile (File () 'Out -> Output) -> Parser (File () 'Out) -> Parser Output
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [Char] -> Parser (File () 'Out)
forall a. [Char] -> [Char] -> Parser (File a 'Out)
pFileOutDirection [Char]
optName [Char]
desc

pOutputText :: String -> String -> Opt.Parser Output
pOutputText :: [Char] -> [Char] -> Parser Output
pOutputText [Char]
optName [Char]
help =
  Output -> Mod FlagFields Output -> Parser Output
forall a. a -> Mod FlagFields a -> Parser a
Opt.flag' Output
OutputText (Mod FlagFields Output -> Parser Output)
-> Mod FlagFields Output -> Parser Output
forall a b. (a -> b) -> a -> b
$
    [Mod FlagFields Output] -> Mod FlagFields Output
forall a. Monoid a => [a] -> a
mconcat
      [ [Char] -> Mod FlagFields Output
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
optName
      , [Char] -> Mod FlagFields Output
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
help
      ]