blob: 7b83d4f5bc9efb83b30d3a01ee3615d36a3c68bb [file]
#include "tlbmc/resource/reset_type.h"
#include <algorithm>
#include <array>
#include <utility>
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "resource.pb.h"
namespace milotic_tlbmc {
namespace {
constexpr auto kSupportedResetTypesMap =
std::to_array<std::pair<std::string_view, ResetType>>({
// go/keep-sorted start
{"ForceOff", RESET_TYPE_FORCE_OFF},
{"ForceOn", RESET_TYPE_FORCE_ON},
{"ForceRestart", RESET_TYPE_FORCE_RESTART},
{"GracefulRestart", RESET_TYPE_GRACEFUL_RESTART},
{"GracefulShutdown", RESET_TYPE_GRACEFUL_SHUTDOWN},
{"On", RESET_TYPE_ON},
{"PowerCycle", RESET_TYPE_POWER_CYCLE},
// go/keep-sorted end
});
} // namespace
ResetType GetResetType(absl::string_view reset_type) {
const auto* it = std::lower_bound(
kSupportedResetTypesMap.begin(), kSupportedResetTypesMap.end(),
std::pair<std::string_view, ResetType>{reset_type,
RESET_TYPE_UNSPECIFIED},
[](const auto& a, const auto& b) { return a.first < b.first; });
if (it == kSupportedResetTypesMap.end() || it->first != reset_type) {
return RESET_TYPE_UNSPECIFIED;
}
return it->second;
}
absl::StatusOr<absl::string_view> GetResetTypeString(ResetType reset_type) {
for (const auto& [str, type] : kSupportedResetTypesMap) {
if (type == reset_type) {
return str;
}
}
return absl::NotFoundError(
absl::StrCat("Unknown reset type: ", static_cast<int>(reset_type)));
}
} // namespace milotic_tlbmc