refactor: PR review cleanup
parent
2b8c92e8fd
commit
3ae2fe3272
|
@ -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},
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue