mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-13 22:52:30 +02:00
refactor: enumparser create implementation file
This commit is contained in:
@ -1,38 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "util/string.hpp"
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
template <typename EnumType>
|
||||
struct EnumParser {
|
||||
EnumParser() {}
|
||||
public:
|
||||
EnumParser();
|
||||
~EnumParser();
|
||||
|
||||
EnumType parseStringToEnum(const std::string& str,
|
||||
const std::map<std::string, EnumType>& enumMap) {
|
||||
// Convert the input string to uppercase
|
||||
std::string uppercaseStr = capitalize(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(capitalize(pair.first), pair.second); });
|
||||
|
||||
// Return enum match of string
|
||||
auto it = enumMap.find(uppercaseStr);
|
||||
if (it != enumMap.end()) return it->second;
|
||||
|
||||
// Throw error if it doesnt return
|
||||
throw std::invalid_argument("Invalid string representation for enum");
|
||||
}
|
||||
|
||||
~EnumParser() = default;
|
||||
const std::map<std::string, EnumType>& enumMap);
|
||||
};
|
||||
|
||||
} // namespace waybar::util
|
||||
|
Reference in New Issue
Block a user