2023-09-09 14:32:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace waybar::util {
|
|
|
|
|
|
|
|
struct EnumParser {
|
|
|
|
EnumParser() {}
|
|
|
|
|
|
|
|
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
|
|
|
|
|
|
|
|
SORT_METHOD sortStringToEnum(const std::string& str) {
|
2023-09-09 14:38:03 +00:00
|
|
|
// Convert the input string to uppercase (make it lenient on config input)
|
|
|
|
std::string uppercaseStr;
|
|
|
|
for (char c : str) {
|
|
|
|
uppercaseStr += std::toupper(c);
|
|
|
|
}
|
|
|
|
|
2023-09-09 14:32:55 +00:00
|
|
|
static const std::map<std::string, SORT_METHOD> enumMap = {
|
|
|
|
{"ID", ID}, {"NAME", NAME}, {"NUMBER", NUMBER}, {"DEFAULT", DEFAULT}};
|
|
|
|
|
2023-09-09 14:38:03 +00:00
|
|
|
auto it = enumMap.find(uppercaseStr);
|
2023-09-09 14:32:55 +00:00
|
|
|
if (it != enumMap.end()) {
|
|
|
|
return it->second;
|
|
|
|
} else {
|
|
|
|
throw std::invalid_argument("Invalid string representation for enum");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~EnumParser() = default;
|
|
|
|
};
|
|
|
|
} // namespace waybar::util
|