.
All checks were successful
PR Check / on-push-commit-check (push) Successful in 16m21s

This commit is contained in:
sha512sum 2024-12-24 21:57:10 +11:00
parent e7ca1d08e9
commit 8019371d46

View file

@ -30,77 +30,43 @@ struct Body {
namespace type {
struct Chat {
static constexpr auto TryParse(std::string_view value) -> std::optional<Chat> {
return value == "chat" ? std::optional{Chat{}} : std::nullopt;
template <typename T>
struct TypeImpl {
static constexpr auto TryParse(std::string_view value) -> std::optional<T> {
return value == T::kName ? std::optional{T{}} : std::nullopt;
}
friend constexpr auto ToString(const Chat&) -> std::string {
return "chat";
friend constexpr auto ToString(const T&) -> std::string {
return T::kName;
}
constexpr auto operator==(const Chat&) const -> bool {
friend constexpr auto operator==(const T&, const T&) -> bool {
return true;
};
};
static constexpr auto kChat = Chat{};
struct Chat : TypeImpl<Chat> {
static constexpr auto kName = "chat";
} constexpr kChat{};
struct Error {
static constexpr auto TryParse(std::string_view value) -> std::optional<Error> {
return value == "error" ? std::optional{Error{}} : std::nullopt;
}
friend constexpr auto ToString(const Error&) -> std::string {
return "error";
}
constexpr auto operator==(const Error&) const -> bool {
return true;
};
};
struct Error : TypeImpl<Error> {
static constexpr auto kName = "error";
} constexpr kError{};
static constexpr auto kError = Error{};
struct GroupChat : TypeImpl<GroupChat> {
static constexpr auto kName = "groupchat";
} constexpr kGroupChat{};
struct GroupChat {
static constexpr auto TryParse(std::string_view value) -> std::optional<GroupChat> {
return value == "groupchat" ? std::optional{GroupChat{}} : std::nullopt;
}
friend constexpr auto ToString(const GroupChat&) -> std::string {
return "groupchat";
}
constexpr auto operator==(const GroupChat&) const -> bool {
return true;
};
};
struct Headline : TypeImpl<Headline> {
static constexpr auto kName = "headline";
} constexpr kHeadline{};
static constexpr auto kGroupChat = GroupChat{};
struct Headline {
static constexpr auto TryParse(std::string_view value) -> std::optional<Headline> {
return value == "headline" ? std::optional{Headline{}} : std::nullopt;
}
friend constexpr auto ToString(const Headline&) -> std::string {
return "headline";
}
constexpr auto operator==(const Headline&) const -> bool {
return true;
};
};
static constexpr auto kHeadline = Headline{};
struct Normal {
struct Normal : TypeImpl<Normal> {
static constexpr auto kName = "normal";
static constexpr auto Parse(std::string_view value) -> Normal {
return value == "normal" ? Normal{}
: throw std::runtime_error(
std::format(R"(message::type::Normal Parsing error: [ expected "normal" but "{}" found ])", value));
return value == kName ? Normal{}
: throw std::runtime_error(
std::format(R"(message::type::Normal Parsing error: [ expected "normal" but "{}" found ])", value));
}
friend constexpr auto ToString(const Normal&) -> std::string {
return "normal";
}
constexpr auto operator==(const Normal&) const -> bool {
return true;
};
};
static constexpr auto kNormal = Normal{};
} constexpr kNormal;
} // namespace type