Skip to content

Commit

Permalink
Add enum safeguards and test accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 25, 2024
1 parent 59f9135 commit 8375c6f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
42 changes: 42 additions & 0 deletions nano/core_test/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,59 @@
TEST (enums, stat_type)
{
ASSERT_FALSE (nano::to_string (static_cast<nano::stat::type> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::type> (0)) });

ASSERT_FALSE (nano::to_string (nano::stat::type::_last).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::type::_last) });
ASSERT_EQ (nano::to_string (nano::stat::type::_last), "_last");
}

TEST (enums, stat_detail)
{
ASSERT_FALSE (nano::to_string (static_cast<nano::stat::detail> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::detail> (0)) });

ASSERT_FALSE (nano::to_string (nano::stat::detail::_last).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::detail::_last) });
ASSERT_EQ (nano::to_string (nano::stat::detail::_last), "_last");
}

TEST (enums, stat_dir)
{
ASSERT_FALSE (nano::to_string (static_cast<nano::stat::dir> (0)).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (static_cast<nano::stat::dir> (0)) });

ASSERT_FALSE (nano::to_string (nano::stat::dir::_last).empty ());
ASSERT_NO_THROW (std::string{ nano::to_string (nano::stat::dir::_last) });
ASSERT_EQ (nano::to_string (nano::stat::dir::_last), "_last");
}

TEST (enums, log_type)
{
ASSERT_FALSE (to_string (static_cast<nano::log::type> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::log::type> (0)) });

ASSERT_FALSE (to_string (nano::log::type::_last).empty ());
ASSERT_NO_THROW (std::string{ to_string (nano::log::type::_last) });
ASSERT_EQ (to_string (nano::log::type::_last), "_last");
}

TEST (enums, log_detail)
{
ASSERT_FALSE (to_string (static_cast<nano::log::detail> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::log::detail> (0)) });

ASSERT_FALSE (to_string (nano::log::detail::_last).empty ());
ASSERT_NO_THROW (std::string{ to_string (nano::log::detail::_last) });
ASSERT_EQ (to_string (nano::log::detail::_last), "_last");
}

TEST (enums, log_category)
{
ASSERT_FALSE (to_string (static_cast<nano::log::type> (0)).empty ());
ASSERT_NO_THROW (std::string{ to_string (static_cast<nano::log::type> (0)) });

ASSERT_FALSE (to_string (nano::log::type::_last).empty ());
ASSERT_NO_THROW (std::string{ to_string (nano::log::type::_last) });
ASSERT_EQ (to_string (nano::log::type::_last), "_last");
}
20 changes: 5 additions & 15 deletions nano/lib/logging_enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,22 @@ std::string_view nano::log::to_string (nano::log::level level)
const std::vector<nano::log::level> & nano::log::all_levels ()
{
static std::vector<nano::log::level> all = [] () {
std::vector<nano::log::level> result;
for (auto const & lvl : magic_enum::enum_values<nano::log::level> ())
{
result.push_back (lvl);
}
return result;
return nano::util::enum_values<nano::log::level> ();
}();
return all;
}

const std::vector<nano::log::type> & nano::log::all_types ()
{
static std::vector<nano::log::type> all = [] () {
std::vector<nano::log::type> result;
for (auto const & lvl : magic_enum::enum_values<nano::log::type> ())
{
result.push_back (lvl);
}
return result;
return nano::util::enum_values<nano::log::type> ();
}();
return all;
}

nano::log::level nano::log::parse_level (std::string_view name)
{
auto value = magic_enum::enum_cast<nano::log::level> (name);
auto value = nano::util::parse_enum<nano::log::level> (name);
if (value.has_value ())
{
return value.value ();
Expand All @@ -63,7 +53,7 @@ nano::log::level nano::log::parse_level (std::string_view name)

nano::log::type nano::log::parse_type (std::string_view name)
{
auto value = magic_enum::enum_cast<nano::log::type> (name);
auto value = nano::util::parse_enum<nano::log::type> (name);
if (value.has_value ())
{
return value.value ();
Expand All @@ -76,7 +66,7 @@ nano::log::type nano::log::parse_type (std::string_view name)

nano::log::detail nano::log::parse_detail (std::string_view name)
{
auto value = magic_enum::enum_cast<nano::log::detail> (name);
auto value = nano::util::parse_enum<nano::log::detail> (name);
if (value.has_value ())
{
return value.value ();
Expand Down
5 changes: 5 additions & 0 deletions nano/lib/logging_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ enum class type
bootstrap,
bootstrap_lazy,
bootstrap_legacy,

_last // Must be the last enum
};

enum class detail
Expand Down Expand Up @@ -112,6 +114,7 @@ enum class detail
requesting_account_or_head,
requesting_pending,

_last // Must be the last enum
};

// TODO: Additionally categorize logs by categories which can be enabled/disabled independently
Expand All @@ -121,6 +124,8 @@ enum class category

work_generation,
// ...

_last // Must be the last enum
};
}

Expand Down
35 changes: 35 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sstream>
#include <vector>

#include <magic_enum.hpp>
#include <magic_enum_containers.hpp>

namespace boost
Expand Down Expand Up @@ -262,4 +263,38 @@ std::string to_str (T const & val)
{
return boost::lexical_cast<std::string> (val);
}

/**
* Same as `magic_enum::enum_values (...)` but ignores reserved values (starting with underscore)
*/
template <class E>
std::vector<E> enum_values ()
{
std::vector<E> result;
for (auto const & [val, name] : magic_enum::enum_entries<E> ())
{
if (!name.starts_with ('_'))
{
result.push_back (val);
}
}
return result;
}

/**
* Same as `magic_enum::enum_cast (...)` but ignores reserved values (starting with underscore).
* Case insensitive.
*/
template <class E>
std::optional<E> parse_enum (std::string_view name)
{
if (name.starts_with ('_'))
{
return std::nullopt;
}
else
{
return magic_enum::enum_cast<E> (name, magic_enum::case_insensitive);
}
}
}

0 comments on commit 8375c6f

Please sign in to comment.