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 active_only_ = false;
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
enum class SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
util::EnumParser<SORT_METHOD> enum_parser_;
SORT_METHOD sort_by_ = SORT_METHOD::DEFAULT;
std::map<std::string, SORT_METHOD> sort_map_ = {{"ID", SORT_METHOD::ID},

View File

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

View File

@ -61,7 +61,7 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
if (config_sort_by.isString()) {
auto sort_by_str = config_sort_by.asString();
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) {
// Handle the case where the string is not a valid enum representation.
sort_by_ = SORT_METHOD::DEFAULT;