From b3a4757b2feadd826a5e68937f0cbf9c03f51aff Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 24 Dec 2024 19:46:59 +1100 Subject: [PATCH] . --- library/include/larra/message.hpp | 45 ++++++++++++++++++++++++++++--- tests/message.cpp | 17 +++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/library/include/larra/message.hpp b/library/include/larra/message.hpp index eb3f46d..5ca68db 100644 --- a/library/include/larra/message.hpp +++ b/library/include/larra/message.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -123,8 +124,6 @@ struct Type : TypeVariant { } }; -} // namespace message - template struct Message { static constexpr auto kDefaultName = "message"; @@ -144,12 +143,50 @@ struct Message { }; template -struct serialization::SerializationConfigT> { - static constexpr auto kValue = serialization::SerializationConfig>{} // +struct Error { + static constexpr auto kDefaultName = "message"; + static auto Parse(xmlpp::Element* element) -> Error { + auto attr = element->get_attribute("type"); + if(!attr || attr->get_value() != "error") { + throw std::runtime_error{"attribute type: message::Type invalid"}; + } + return serialization::Parse(element); + } + static auto TryParse(xmlpp::Element* element) -> std::optional { + auto attr = element->get_attribute("type"); + if(!attr || attr->get_value() != "error") { + return std::nullopt; + } + return serialization::TryParse(element); + } + friend auto operator<<(xmlpp::Element* element, const Error& message) -> void { + element->set_attribute("type", "error"); + serialization::Serialize(element, message); + } + constexpr auto operator==(const Error& other) const -> bool = default; + From from; + To to; + stanza::error::StanzaError error; +}; + +} // namespace message + +template +using Message = std::variant, message::Message>; + +template +struct serialization::SerializationConfigT> { + static constexpr auto kValue = serialization::SerializationConfig>{} // .template With<"type">(serialization::AttributeConfig{}) .template With<"body">(serialization::Config>{}); }; +template +struct serialization::SerializationConfigT> { + static constexpr auto kValue = serialization::SerializationConfig>{} // + .template With<"error">(serialization::Config{}); +}; + template struct serialization::AttributeSerializer : serialization::AttributeSerializer {}; diff --git a/tests/message.cpp b/tests/message.cpp index 68826af..8f33327 100644 --- a/tests/message.cpp +++ b/tests/message.cpp @@ -21,7 +21,7 @@ auto CreateTestData() { return doc; } -const Message kMessage{ +const message::Message kMessage{ .from = {.username = "user1", .server = "server.i2p"}, .to = {.username = "user2", .server = "server.i2p"}, .type = message::type::kChat, @@ -30,12 +30,16 @@ const Message kMessage{ .body = {{.content = "hello"}} // }; +const message::Error kError{.from = {.username = "user1", .server = "server.i2p"}, // + .to = {.username = "user2", .server = "server.i2p"}, + .error = stanza::error::NotAuthorized{}}; + } // namespace TEST(Parse, Message) { auto doc = CreateTestData(); auto node = doc->get_root_node(); - auto message = Serialization>::Parse(node); + auto message = Serialization>::Parse(node); EXPECT_EQ(message, kMessage); } @@ -47,8 +51,15 @@ TEST(Serialize, Message) { )"; xmlpp::Document doc; auto node = doc.create_root_node("message"); - Serialization>::Serialize(node, kMessage); + Serialization>::Serialize(node, kMessage); EXPECT_EQ(doc.write_to_string(), expected); + doc.~Document(); + new(&doc) xmlpp::Document{}; + node = doc.create_root_node("message"); + Serialization>::Serialize(node, kError); + EXPECT_EQ(doc.write_to_string(), R"( + +)"); } } // namespace larra::xmpp