Skip to content

Commit

Permalink
Simplify enum conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 25, 2024
1 parent 9b61655 commit dd78fa6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 90 deletions.
21 changes: 3 additions & 18 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,24 +693,9 @@ std::vector<nano::vote_with_weight_info> nano::election::votes_with_weight () co

nano::stat::detail nano::to_stat_detail (nano::election_behavior behavior)
{
switch (behavior)
{
case nano::election_behavior::normal:
{
return nano::stat::detail::normal;
}
case nano::election_behavior::hinted:
{
return nano::stat::detail::hinted;
}
case nano::election_behavior::optimistic:
{
return nano::stat::detail::optimistic;
}
}

debug_assert (false, "unknown election behavior");
return {};
auto value = magic_enum::enum_cast<nano::stat::detail> (magic_enum::enum_integer (behavior));
debug_assert (value);
return value.value_or (nano::stat::detail{});
}

nano::election_behavior nano::election::behavior () const
Expand Down
76 changes: 4 additions & 72 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,80 +1995,12 @@ void nano::asc_pull_ack::frontiers_payload::deserialize (nano::stream & stream)

std::string_view nano::to_string (nano::message_type type)
{
switch (type)
{
case nano::message_type::invalid:
return "invalid";
case nano::message_type::not_a_type:
return "not_a_type";
case nano::message_type::keepalive:
return "keepalive";
case nano::message_type::publish:
return "publish";
case nano::message_type::confirm_req:
return "confirm_req";
case nano::message_type::confirm_ack:
return "confirm_ack";
case nano::message_type::bulk_pull:
return "bulk_pull";
case nano::message_type::bulk_push:
return "bulk_push";
case nano::message_type::frontier_req:
return "frontier_req";
case nano::message_type::node_id_handshake:
return "node_id_handshake";
case nano::message_type::bulk_pull_account:
return "bulk_pull_account";
case nano::message_type::telemetry_req:
return "telemetry_req";
case nano::message_type::telemetry_ack:
return "telemetry_ack";
case nano::message_type::asc_pull_req:
return "asc_pull_req";
case nano::message_type::asc_pull_ack:
return "asc_pull_ack";
// default case intentionally omitted to cause warnings for unhandled enums
}

return "n/a";
return magic_enum::enum_name (type);
}

nano::stat::detail nano::to_stat_detail (nano::message_type type)
{
switch (type)
{
case nano::message_type::invalid:
return nano::stat::detail::invalid;
case nano::message_type::not_a_type:
return nano::stat::detail::not_a_type;
case nano::message_type::keepalive:
return nano::stat::detail::keepalive;
case nano::message_type::publish:
return nano::stat::detail::publish;
case nano::message_type::confirm_req:
return nano::stat::detail::confirm_req;
case nano::message_type::confirm_ack:
return nano::stat::detail::confirm_ack;
case nano::message_type::bulk_pull:
return nano::stat::detail::bulk_pull;
case nano::message_type::bulk_push:
return nano::stat::detail::bulk_push;
case nano::message_type::frontier_req:
return nano::stat::detail::frontier_req;
case nano::message_type::node_id_handshake:
return nano::stat::detail::node_id_handshake;
case nano::message_type::bulk_pull_account:
return nano::stat::detail::bulk_pull_account;
case nano::message_type::telemetry_req:
return nano::stat::detail::telemetry_req;
case nano::message_type::telemetry_ack:
return nano::stat::detail::telemetry_ack;
case nano::message_type::asc_pull_req:
return nano::stat::detail::asc_pull_req;
case nano::message_type::asc_pull_ack:
return nano::stat::detail::asc_pull_ack;
// default case intentionally omitted to cause warnings for unhandled enums
}
debug_assert (false);
return {};
auto value = magic_enum::enum_cast<nano::stat::detail> (magic_enum::enum_name (type));
debug_assert (value);
return value.value_or (nano::stat::detail{});
}

0 comments on commit dd78fa6

Please sign in to comment.