diff --git a/library/include/larra/message.hpp b/library/include/larra/message.hpp index c85f440..780a3b9 100644 --- a/library/include/larra/message.hpp +++ b/library/include/larra/message.hpp @@ -30,77 +30,43 @@ struct Body { namespace type { -struct Chat { - static constexpr auto TryParse(std::string_view value) -> std::optional { - return value == "chat" ? std::optional{Chat{}} : std::nullopt; +template +struct TypeImpl { + static constexpr auto TryParse(std::string_view value) -> std::optional { + 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 { + static constexpr auto kName = "chat"; +} constexpr kChat{}; -struct Error { - static constexpr auto TryParse(std::string_view value) -> std::optional { - 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 { + static constexpr auto kName = "error"; +} constexpr kError{}; -static constexpr auto kError = Error{}; +struct GroupChat : TypeImpl { + static constexpr auto kName = "groupchat"; +} constexpr kGroupChat{}; -struct GroupChat { - static constexpr auto TryParse(std::string_view value) -> std::optional { - 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 { + static constexpr auto kName = "headline"; +} constexpr kHeadline{}; -static constexpr auto kGroupChat = GroupChat{}; - -struct Headline { - static constexpr auto TryParse(std::string_view value) -> std::optional { - 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 { + 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