Skip to content

Commit

Permalink
config: save enum value with str
Browse files Browse the repository at this point in the history
  • Loading branch information
rtgiskard committed Oct 6, 2024
1 parent 406bced commit e4b0828
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion data/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sync = true # sync/async log, use sync log
pattern = "%Y%m%d_%H%M%S.%e %^%L%$ %t %n: %v"

[udp2p]
mode = 2
mode = 'HYBRID'
peer_id = 'aaccaaccaacceecc'
server_addr = '127.0.0.1'
server_port = 2048
33 changes: 20 additions & 13 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <toml++/toml.h>
#include <spdlog/spdlog.h>
#include <argparse/argparse.hpp>
#include <magic_enum.hpp>

#include "const.h"
#include "config.h"
#include "udp2p/udp2p.h"
#include "utils/misc.h"

namespace mtrx {
Expand Down Expand Up @@ -46,15 +48,23 @@ bool Config::load(SView path) {
log.flush_interval = tbl["log"]["flush_interval"].value_or(1);
log.sync = tbl["log"]["sync"].value_or(true);

udp2p.mode = tbl["udp2p"]["mode"].value_or(udp2p::Udp2p_Mode::UDP2P_MODE_PEER);
udp2p.server_port = tbl["udp2p"]["server_port"].value_or(2048);
udp2p.server_addr = tbl["udp2p"]["server_addr"].value_or("127.0.0.1");

auto mode = magic_enum::enum_cast<udp2p::Udp2p_Mode>(tbl["udp2p"]["mode"].value_or(""));
if (mode.has_value())
udp2p.mode = mode.value();
else {
std::cerr << "* udp2p: invalid mode, using PEER instead" << std::endl;
udp2p.mode = udp2p::Udp2p_Mode::PEER;
}

auto ret =
utils::hexToBytes(tbl["udp2p"]["peer_id"].value_or("00"),
reinterpret_cast<uint8_t *>(&udp2p.peer_id), sizeof(udp2p.peer_id));
if (!ret) {
std::cout << "* invalid peer_id: should be valid hex string. using 0" << std::endl;
std::cerr << "* udp2p: invalid peer_id: should be valid hex string. using 0 instead"
<< std::endl;
udp2p.peer_id = 0;
}

Expand All @@ -76,11 +86,11 @@ bool Config::dump(SView path) {
auto peer_id_hex = utils::hexFromBytes(reinterpret_cast<uint8_t *>(&udp2p.peer_id),
sizeof(udp2p.peer_id));
auto tbl_udp2p = toml::table{
{"mode", udp2p.mode },
{"peer_id", peer_id_hex },
{"server_addr", udp2p.server_addr},
{"server_port", udp2p.server_port},
};
{"mode", magic_enum::enum_name(udp2p.mode)},
{"peer_id", peer_id_hex },
{"server_addr", udp2p.server_addr },
{"server_port", udp2p.server_port },
}; // namespace mtrx

auto tbl = toml::table{
{"name", name },
Expand Down Expand Up @@ -172,12 +182,9 @@ Config Config::fromArgs(int argc, char ** argv) {
Config config;

// op is verified on arg parse
config.op = std::unordered_map<SView, Operation>{
{"dump", OP_DUMP },
{"run", OP_RUN },
{"test", OP_TEST },
{"udp2p", OP_UDP2P},
}[parser.get<std::string>("op")];
auto operation = magic_enum::enum_cast<Operation>(parser.get<std::string>("op"),
magic_enum::case_insensitive);
config.op = operation.value_or(Operation::DUMP);

// load config
config.load(parser.get<std::string>("-c"));
Expand Down

0 comments on commit e4b0828

Please sign in to comment.