|
1 | | -module Main where |
| 1 | +{-# LANGUAGE ImportQualifiedPost #-} |
| 2 | +{-# LANGUAGE TypeApplications #-} |
| 3 | + |
| 4 | +module Main (main) where |
| 5 | + |
| 6 | +import Control.Applicative (Alternative (many), (<**>)) |
| 7 | + |
| 8 | +import Codec.Serialise (deserialise, deserialiseOrFail) |
| 9 | +import Data.Aeson (decode, decodeStrict', encodeFile, json) |
| 10 | +import Data.Aeson.Parser (decodeStrictWith) |
| 11 | +import Data.ByteString qualified as B |
| 12 | +import Data.ByteString.Lazy qualified as LB |
| 13 | +import Data.Maybe (fromMaybe) |
| 14 | +import Options.Applicative ( |
| 15 | + Parser, |
| 16 | + ParserInfo, |
| 17 | + command, |
| 18 | + customExecParser, |
| 19 | + flag, |
| 20 | + fullDesc, |
| 21 | + help, |
| 22 | + helper, |
| 23 | + info, |
| 24 | + long, |
| 25 | + metavar, |
| 26 | + prefs, |
| 27 | + progDesc, |
| 28 | + short, |
| 29 | + showDefault, |
| 30 | + showHelpOnEmpty, |
| 31 | + showHelpOnError, |
| 32 | + strOption, |
| 33 | + subparser, |
| 34 | + ) |
| 35 | +import PlutusJson (jsonToPlutusData, plutusDataToJson) |
| 36 | +import PlutusTx (Data, ToData (toBuiltinData)) |
| 37 | +import PlutusTx.Builtins (dataToBuiltinData, fromBuiltin, serialiseData, toBuiltin) |
| 38 | + |
| 39 | +data Command |
| 40 | + = ToJson FilePath FilePath |
| 41 | + | FromJson FilePath FilePath |
| 42 | + |
| 43 | +toJsonOptsP :: Parser Command |
| 44 | +toJsonOptsP = |
| 45 | + ToJson |
| 46 | + <$> strOption |
| 47 | + ( long "in" |
| 48 | + <> short 'i' |
| 49 | + <> metavar "FILEPATH" |
| 50 | + <> help "PlutusData file (CBOR encoded) to translate into a Json file" |
| 51 | + ) |
| 52 | + <*> strOption |
| 53 | + ( long "out" |
| 54 | + <> short 'o' |
| 55 | + <> metavar "FILEPATH" |
| 56 | + <> help "Translated Json file" |
| 57 | + ) |
| 58 | + |
| 59 | +fromJsonOptsP :: Parser Command |
| 60 | +fromJsonOptsP = |
| 61 | + FromJson |
| 62 | + <$> strOption |
| 63 | + ( long "in" |
| 64 | + <> short 'i' |
| 65 | + <> metavar "FILEPATH" |
| 66 | + <> help "Json file to translate into a PlutusData file (CBOR encoded)" |
| 67 | + ) |
| 68 | + <*> strOption |
| 69 | + ( long "out" |
| 70 | + <> short 'o' |
| 71 | + <> metavar "FILEPATH" |
| 72 | + <> help "Translated PlutusData file (CBOR encoded)" |
| 73 | + ) |
| 74 | + |
| 75 | +optionsP :: Parser Command |
| 76 | +optionsP = |
| 77 | + subparser $ |
| 78 | + command |
| 79 | + "to-json" |
| 80 | + (info (toJsonOptsP <* helper) (progDesc "Translate a PlutusData file (CBOR encoded) into a Json file")) |
| 81 | + <> command |
| 82 | + "from-json" |
| 83 | + (info (fromJsonOptsP <* helper) (progDesc "Translate a Json file into a PlutusData file (CBOR encoded)")) |
| 84 | + |
| 85 | +parserInfo :: ParserInfo Command |
| 86 | +parserInfo = info (optionsP <**> helper) (fullDesc <> progDesc "COOP plutus-json") |
2 | 87 |
|
3 | 88 | main :: IO () |
4 | | -main = putStrLn "Hello, Haskell!" |
| 89 | +main = do |
| 90 | + cmd <- customExecParser (prefs (showHelpOnEmpty <> showHelpOnError)) parserInfo |
| 91 | + case cmd of |
| 92 | + ToJson inf outf -> do |
| 93 | + cborBytes <- LB.readFile inf |
| 94 | + let errOrDecoded = deserialiseOrFail @Data cborBytes |
| 95 | + plData <- either (\err -> error $ "File " <> inf <> " can't be parsed into PlutusData CBOR: " <> show err) return errOrDecoded |
| 96 | + jsVal <- plutusDataToJson plData |
| 97 | + encodeFile outf jsVal |
| 98 | + FromJson inf outf -> do |
| 99 | + jsonBytes <- B.readFile inf |
| 100 | + let mayDecoded = decodeStrictWith json return jsonBytes |
| 101 | + decoded <- maybe (error $ "File " <> inf <> " can't be parsed into Json") return mayDecoded |
| 102 | + let plData = jsonToPlutusData decoded |
| 103 | + B.writeFile outf (fromBuiltin . serialiseData . dataToBuiltinData $ plData) |
| 104 | + return () |
0 commit comments