2023-09-09 09:32:55 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-09-09 11:18:12 -05:00
|
|
|
#include <cctype>
|
2023-09-09 09:32:55 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <map>
|
2023-09-09 11:18:12 -05:00
|
|
|
#include <stdexcept>
|
2023-09-09 09:32:55 -05:00
|
|
|
#include <string>
|
|
|
|
|
|
2023-09-09 13:23:17 -05:00
|
|
|
#include "util/string.hpp"
|
|
|
|
|
|
2023-09-09 09:32:55 -05:00
|
|
|
namespace waybar::util {
|
|
|
|
|
|
2023-09-09 11:18:12 -05:00
|
|
|
template <typename EnumType>
|
2023-09-09 09:32:55 -05:00
|
|
|
struct EnumParser {
|
|
|
|
|
EnumParser() {}
|
|
|
|
|
|
2023-09-09 12:02:56 -05:00
|
|
|
EnumType parseStringToEnum(const std::string& str,
|
|
|
|
|
const std::map<std::string, EnumType>& enumMap) {
|
2023-09-09 11:18:12 -05:00
|
|
|
// Convert the input string to uppercase
|
2023-09-09 13:23:17 -05:00
|
|
|
std::string uppercaseStr = capitalize(str);
|
2023-09-09 12:14:52 -05:00
|
|
|
|
|
|
|
|
// Capitalize the map keys before searching
|
|
|
|
|
std::map<std::string, EnumType> capitalizedEnumMap;
|
2023-09-09 13:23:17 -05:00
|
|
|
std::transform(
|
|
|
|
|
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
|
|
|
|
[this](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
|
2023-09-09 09:38:03 -05:00
|
|
|
|
2023-09-09 12:02:56 -05:00
|
|
|
// Return enum match of string
|
2023-09-09 09:38:03 -05:00
|
|
|
auto it = enumMap.find(uppercaseStr);
|
2023-09-09 12:02:56 -05: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 09:32:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~EnumParser() = default;
|
|
|
|
|
};
|
|
|
|
|
} // namespace waybar::util
|