Skip to content

Commit

Permalink
refactor: PR review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Sep 9, 2023
1 parent 2b8c92e commit eb11b6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions include/util/enum.hpp
Expand Up @@ -12,20 +12,19 @@ template <typename EnumType>
struct EnumParser {
EnumParser() {}

EnumType sortStringToEnum(const std::string& str,
const std::map<std::string, EnumType>& enumMap) {
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 {
throw std::invalid_argument("Invalid string representation for enum");
}
if (it != enumMap.end()) return it->second;

// Throw error if it doesnt return
throw std::invalid_argument("Invalid string representation for enum");
}

~EnumParser() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/hyprland/workspaces.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit eb11b6a

Please sign in to comment.