2023-09-09 14:32:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-09-09 16:18:12 +00:00
|
|
|
#include <cctype>
|
2023-09-09 14:32:55 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2023-09-09 16:18:12 +00:00
|
|
|
#include <stdexcept>
|
2023-09-09 14:32:55 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace waybar::util {
|
|
|
|
|
2023-09-09 16:18:12 +00:00
|
|
|
template <typename EnumType>
|
2023-09-09 14:32:55 +00:00
|
|
|
struct EnumParser {
|
|
|
|
EnumParser() {}
|
|
|
|
|
2023-09-09 17:14:52 +00:00
|
|
|
// Helper function to capitalize a string
|
|
|
|
std::string capitalizeString(const std::string& str) {
|
|
|
|
std::string result = str;
|
|
|
|
std::transform(result.begin(), result.end(), result.begin(),
|
|
|
|
[](unsigned char c) { return std::toupper(c); });
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:02:56 +00:00
|
|
|
EnumType parseStringToEnum(const std::string& str,
|
|
|
|
const std::map<std::string, EnumType>& enumMap) {
|
2023-09-09 16:18:12 +00:00
|
|
|
// Convert the input string to uppercase
|
2023-09-09 17:14:52 +00:00
|
|
|
std::string uppercaseStr = capitalizeString(str);
|
|
|
|
|
|
|
|
// Capitalize the map keys before searching
|
|
|
|
std::map<std::string, EnumType> capitalizedEnumMap;
|
|
|
|
std::transform(enumMap.begin(), enumMap.end(),
|
|
|
|
std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
|
|
|
[this](const auto& pair) {
|
|
|
|
return std::make_pair(capitalizeString(pair.first), pair.second);
|
|
|
|
});
|
2023-09-09 14:38:03 +00:00
|
|
|
|
2023-09-09 17:02:56 +00:00
|
|
|
// Return enum match of string
|
2023-09-09 14:38:03 +00:00
|
|
|
auto it = enumMap.find(uppercaseStr);
|
2023-09-09 17:02:56 +00:00
|
|
|
if (it != enumMap.end()) return it->second;
|
|
|
|
|
|
|
|
// Throw error if it doesnt return
|
|
|
|
throw std::invalid_argument("Invalid string representation for enum");
|
2023-09-09 14:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~EnumParser() = default;
|
|
|
|
};
|
|
|
|
} // namespace waybar::util
|