|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdlib> |
| 4 | +#include <cstring> |
| 5 | +#include <openPMD/auxiliary/JSON_internal.hpp> |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <sstream> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +namespace from_format_to_format |
| 12 | +{ |
| 13 | +namespace json = openPMD::json; |
| 14 | +struct ID |
| 15 | +{ |
| 16 | + template <json::SupportedLanguages originallySpecifiedAs> |
| 17 | + static auto call(nlohmann::json const &&val) |
| 18 | + // template <> |
| 19 | + // auto call<json::SupportedLanguages::JSON>(nlohmann::json const &val) -> |
| 20 | + // nlohmann::json const& |
| 21 | + { |
| 22 | + if constexpr (originallySpecifiedAs == json::SupportedLanguages::JSON) |
| 23 | + { |
| 24 | + return val; |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + return json::jsonToToml(val); |
| 29 | + } |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +struct switch_ |
| 34 | +{ |
| 35 | + template <json::SupportedLanguages> |
| 36 | + struct other_type; |
| 37 | + template <json::SupportedLanguages originallySpecifiedAs> |
| 38 | + static auto call(nlohmann::json const &&val) |
| 39 | + { |
| 40 | + return ID::call<other_type<originallySpecifiedAs>::value>( |
| 41 | + std::move(val)); |
| 42 | + } |
| 43 | +}; |
| 44 | +template <> |
| 45 | +struct switch_::other_type<json::SupportedLanguages::JSON> |
| 46 | +{ |
| 47 | + static constexpr json::SupportedLanguages value = |
| 48 | + json::SupportedLanguages::TOML; |
| 49 | +}; |
| 50 | +template <> |
| 51 | +struct switch_::other_type<json::SupportedLanguages::TOML> |
| 52 | +{ |
| 53 | + static constexpr json::SupportedLanguages value = |
| 54 | + json::SupportedLanguages::JSON; |
| 55 | +}; |
| 56 | +} // namespace from_format_to_format |
| 57 | + |
| 58 | +template <typename FromFormatToFormat> |
| 59 | +class convert_json_toml |
| 60 | +{ |
| 61 | + static void with_parsed_cmdline_args(std::string jsonOrToml) |
| 62 | + { |
| 63 | + namespace json = openPMD::json; |
| 64 | + auto [config, originallySpecifiedAs] = json::parseOptions( |
| 65 | + jsonOrToml, |
| 66 | + /* considerFiles = */ true, |
| 67 | + /* convertLowercase = */ false); |
| 68 | + { |
| 69 | + // NOLINTNEXTLINE(bugprone-unused-local-non-trivial-variable) |
| 70 | + [[maybe_unused]] auto _ = std::move(jsonOrToml); |
| 71 | + } |
| 72 | + switch (originallySpecifiedAs) |
| 73 | + { |
| 74 | + using SL = json::SupportedLanguages; |
| 75 | + case SL::JSON: { |
| 76 | + auto asToml = json::jsonToToml(config); |
| 77 | + std::cout << json::format_toml(asToml); |
| 78 | + } |
| 79 | + break; |
| 80 | + case SL::TOML: |
| 81 | + std::cout << config << '\n'; |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +public: |
| 87 | + static void run_application( |
| 88 | + int argc, char const **argv, void (*print_help_message)(char const *)) |
| 89 | + { |
| 90 | + std::string jsonOrToml; |
| 91 | + switch (argc) |
| 92 | + { |
| 93 | + case 0: |
| 94 | + case 1: |
| 95 | + // Just read the whole stream into memory |
| 96 | + // Not very elegant, but we'll hold the entire JSON/TOML dataset |
| 97 | + // in memory at some point anyway, so it doesn't really matter |
| 98 | + { |
| 99 | + std::stringbuf readEverything; |
| 100 | + std::cin >> &readEverything; |
| 101 | + jsonOrToml = readEverything.str(); |
| 102 | + } |
| 103 | + break; |
| 104 | + case 2: |
| 105 | + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) |
| 106 | + { |
| 107 | + print_help_message(argv[1]); |
| 108 | + exit(0); |
| 109 | + } |
| 110 | + jsonOrToml = argv[1]; |
| 111 | + break; |
| 112 | + default: |
| 113 | + throw std::runtime_error( |
| 114 | + std::string("Usage: ") + argv[0] + |
| 115 | + " [file location or inline JSON/TOML]"); |
| 116 | + } |
| 117 | + with_parsed_cmdline_args(std::move(jsonOrToml)); |
| 118 | + } |
| 119 | +}; |
0 commit comments