Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE InstanceSigs #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
|
||
module Cardano.Api.IO | ||
( File(..) | ||
, Directory(..) | ||
, fileMap | ||
, In | ||
, Out | ||
, InOut | ||
, HasFileMode(..) | ||
) where | ||
|
||
import Data.Aeson (FromJSON, ToJSON) | ||
import Data.Kind (Type) | ||
import Data.String (IsString) | ||
|
||
-- | Phantom type to indicate a file for reading. | ||
data In | ||
|
||
-- | Phantom type to indicate a file for writing. | ||
data Out | ||
|
||
-- | Phantom type to indicate a file for reading or writing. | ||
data InOut | ||
|
||
-- | A file path with additional type information to indicate whether it is to | ||
-- be used for reading or writing. | ||
newtype File direction = File { unFile :: FilePath } | ||
deriving (Eq, Ord) | ||
deriving newtype (Show, IsString, FromJSON, ToJSON) | ||
|
||
-- | A directory path. | ||
newtype Directory = Directory { unDirectory :: FilePath } | ||
deriving (Eq, Ord) | ||
deriving newtype (Show, IsString, FromJSON, ToJSON) | ||
|
||
fileMap :: (FilePath -> FilePath) -> File direction -> File direction | ||
fileMap f = File . f . unFile | ||
|
||
class HasFileMode (f :: Type -> Type) where | ||
usingIn :: f InOut -> f In | ||
usingOut :: f InOut -> f Out | ||
|
||
instance HasFileMode File where | ||
usingIn :: File InOut -> File In | ||
usingIn = File . unFile | ||
|
||
usingOut :: File InOut -> File Out | ||
usingOut = File . unFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module Cardano.Api.Options | ||
( fileOption | ||
, inFileOption | ||
, outFileOption | ||
, parseFilePath | ||
, parseFile | ||
, parseFileIn | ||
, parseFileOut | ||
, parseDirectory | ||
) where | ||
|
||
import Cardano.Api.IO (Directory, File (..), In, Out) | ||
|
||
import Options.Applicative (OptionFields) | ||
import qualified Options.Applicative as Opt | ||
import Options.Applicative.Builder (Mod) | ||
import Options.Applicative.Types (Parser) | ||
|
||
fileOption :: forall direction. Mod OptionFields FilePath -> Parser (File direction) | ||
fileOption = fmap File . Opt.strOption | ||
|
||
inFileOption :: forall. Mod OptionFields FilePath -> Parser (File In) | ||
inFileOption = fileOption | ||
|
||
outFileOption :: forall. Mod OptionFields FilePath -> Parser (File Out) | ||
outFileOption = fileOption | ||
|
||
parseDirectory :: String -> String -> Opt.Parser Directory | ||
parseDirectory optname desc = | ||
Opt.strOption | ||
( Opt.long optname | ||
<> Opt.metavar "DIRECTORY" | ||
<> Opt.help desc | ||
<> Opt.completer (Opt.bashCompleter "file") | ||
) | ||
|
||
parseFilePath :: String -> String -> Opt.Parser FilePath | ||
parseFilePath optname desc = | ||
Opt.strOption | ||
( Opt.long optname | ||
<> Opt.metavar "FILEPATH" | ||
<> Opt.help desc | ||
<> Opt.completer (Opt.bashCompleter "file") | ||
) | ||
|
||
parseFile :: String -> String -> Opt.Parser (File direction) | ||
parseFile optname desc = | ||
Opt.strOption | ||
( Opt.long optname | ||
<> Opt.metavar "FILEPATH" | ||
<> Opt.help desc | ||
<> Opt.completer (Opt.bashCompleter "file") | ||
) | ||
|
||
parseFileIn :: String -> String -> Opt.Parser (File In) | ||
parseFileIn = parseFile | ||
|
||
parseFileOut :: String -> String -> Opt.Parser (File Out) | ||
parseFileOut = parseFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters