Skip to content

Commit c371105

Browse files
committed
Extract implementation of convert-toml-json to header
1 parent 6e75956 commit c371105

2 files changed

Lines changed: 128 additions & 60 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
};

src/cli/convert-toml-json.cpp

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
1-
#include <cstdlib>
2-
#include <cstring>
3-
#include <openPMD/auxiliary/JSON_internal.hpp>
1+
#include "openPMD/cli/convert-toml-json.hpp"
42

5-
#include <iostream>
6-
#include <sstream>
7-
#include <string>
8-
9-
namespace json = openPMD::json;
10-
11-
void parsed_main(std::string jsonOrToml)
12-
{
13-
auto [config, originallySpecifiedAs] = json::parseOptions(
14-
jsonOrToml, /* considerFiles = */ true, /* convertLowercase = */ false);
15-
{
16-
// NOLINTNEXTLINE(bugprone-unused-local-non-trivial-variable)
17-
[[maybe_unused]] auto _ = std::move(jsonOrToml);
18-
}
19-
switch (originallySpecifiedAs)
20-
{
21-
using SL = json::SupportedLanguages;
22-
case SL::JSON: {
23-
auto asToml = json::jsonToToml(config);
24-
std::cout << json::format_toml(asToml);
25-
}
26-
break;
27-
case SL::TOML:
28-
std::cout << config << '\n';
29-
break;
30-
}
31-
}
32-
33-
int main(int argc, char const **argv)
3+
void print_help_message(char const *program_name)
344
{
35-
std::string jsonOrToml;
36-
switch (argc)
37-
{
38-
case 0:
39-
case 1:
40-
// Just read the whole stream into memory
41-
// Not very elegant, but we'll hold the entire JSON/TOML dataset
42-
// in memory at some point anyway, so it doesn't really matter
43-
{
44-
std::stringbuf readEverything;
45-
std::cin >> &readEverything;
46-
jsonOrToml = readEverything.str();
47-
}
48-
break;
49-
case 2:
50-
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)
51-
{
52-
std::cout << "Usage: " << std::string(argv[0]) << R"( [json_or_toml]
5+
std::cout << "Usage: " << std::string(program_name) << R"( [json_or_toml]
536
'json_or_toml' can be a JSON or TOML dataset specified inline or a reference
547
to a file prepended by an '@'.
558
Inline datasets will be interpreted as JSON if they start with an '{', as TOML
@@ -60,14 +13,10 @@ Inline dataset specifications can be replaced by input read from stdin.
6013
If the input is JSON, then it will be converted to TOML and written to stdout,
6114
equivalently from TOML to JSON.
6215
)";
63-
exit(0);
64-
}
65-
jsonOrToml = argv[1];
66-
break;
67-
default:
68-
throw std::runtime_error(
69-
std::string("Usage: ") + argv[0] +
70-
" [file location or inline JSON/TOML]");
71-
}
72-
parsed_main(std::move(jsonOrToml));
16+
}
17+
18+
int main(int argc, char const **argv)
19+
{
20+
convert_json_toml<from_format_to_format::ID>::run_application(
21+
argc, argv, print_help_message);
7322
}

0 commit comments

Comments
 (0)