refactor: PR review cleanup

pull/2486/head
Austin Horstman 2023-09-09 12:02:56 -05:00
parent 2b8c92e8fd
commit 3ae2fe3272
No known key found for this signature in database
3 changed files with 12 additions and 13 deletions

View File

@ -88,7 +88,7 @@ class Workspaces : public AModule, public EventHandler {
bool show_special_ = false; bool show_special_ = false;
bool active_only_ = false; bool active_only_ = false;
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT }; enum class SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
util::EnumParser<SORT_METHOD> enum_parser_; util::EnumParser<SORT_METHOD> enum_parser_;
SORT_METHOD sort_by_ = SORT_METHOD::DEFAULT; SORT_METHOD sort_by_ = SORT_METHOD::DEFAULT;
std::map<std::string, SORT_METHOD> sort_map_ = {{"ID", SORT_METHOD::ID}, std::map<std::string, SORT_METHOD> sort_map_ = {{"ID", SORT_METHOD::ID},

View File

@ -12,20 +12,19 @@ template <typename EnumType>
struct EnumParser { struct EnumParser {
EnumParser() {} EnumParser() {}
EnumType sortStringToEnum(const std::string& str, EnumType parseStringToEnum(const std::string& str,
const std::map<std::string, EnumType>& enumMap) { const std::map<std::string, EnumType>& enumMap) {
// Convert the input string to uppercase // Convert the input string to uppercase
std::string uppercaseStr; std::string uppercaseStr = str;
for (char c : str) { std::transform(uppercaseStr.begin(), uppercaseStr.end(), uppercaseStr.begin(),
uppercaseStr += std::toupper(c); [](unsigned char c) { return std::toupper(c); });
}
// Return enum match of string
auto it = enumMap.find(uppercaseStr); auto it = enumMap.find(uppercaseStr);
if (it != enumMap.end()) { if (it != enumMap.end()) return it->second;
return it->second;
} else { // Throw error if it doesnt return
throw std::invalid_argument("Invalid string representation for enum"); throw std::invalid_argument("Invalid string representation for enum");
}
} }
~EnumParser() = default; ~EnumParser() = default;

View File

@ -61,7 +61,7 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
if (config_sort_by.isString()) { if (config_sort_by.isString()) {
auto sort_by_str = config_sort_by.asString(); auto sort_by_str = config_sort_by.asString();
try { try {
sort_by_ = enum_parser_.sortStringToEnum(sort_by_str, sort_map_); sort_by_ = enum_parser_.parseStringToEnum(sort_by_str, sort_map_);
} catch (const std::invalid_argument &e) { } catch (const std::invalid_argument &e) {
// Handle the case where the string is not a valid enum representation. // Handle the case where the string is not a valid enum representation.
sort_by_ = SORT_METHOD::DEFAULT; sort_by_ = SORT_METHOD::DEFAULT;