From fe5b6522401e4a93538196660126a9c17a6f7ba2 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 24 Dec 2024 20:06:11 +1100 Subject: [PATCH] . --- library/include/larra/message.hpp | 6 ++++- library/include/larra/serialization/auto.hpp | 5 +--- tests/message.cpp | 28 +++++++++++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/library/include/larra/message.hpp b/library/include/larra/message.hpp index 5ca68db..c85f440 100644 --- a/library/include/larra/message.hpp +++ b/library/include/larra/message.hpp @@ -157,7 +157,11 @@ struct Error { if(!attr || attr->get_value() != "error") { return std::nullopt; } - return serialization::TryParse(element); + try { + return serialization::Parse(element); + } catch(...) { + return std::nullopt; + } } friend auto operator<<(xmlpp::Element* element, const Error& message) -> void { element->set_attribute("type", "error"); diff --git a/library/include/larra/serialization/auto.hpp b/library/include/larra/serialization/auto.hpp index 7c19a18..e27f3a6 100644 --- a/library/include/larra/serialization/auto.hpp +++ b/library/include/larra/serialization/auto.hpp @@ -37,10 +37,7 @@ constexpr auto Parse(xmlpp::Element* element, Tag = {}) -> T template constexpr auto Parse(xmlpp::Element* element, Tag = {}) -> T; -template -constexpr auto TryParse(xmlpp::Element* element, Tag = {}) -> std::optional { - return Serialization>::Parse(element); -} +// TODO(sha512sum): Add TryParse template constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void diff --git a/tests/message.cpp b/tests/message.cpp index 8f33327..538f0ef 100644 --- a/tests/message.cpp +++ b/tests/message.cpp @@ -21,6 +21,19 @@ auto CreateTestData() { return doc; } +auto CreateErrorTestData() { + auto doc = std::make_unique(); + auto node = doc->create_root_node("message"); + node->set_attribute("from", "user1@server.i2p"); + node->set_attribute("to", "user2@server.i2p"); + node->set_attribute("type", "error"); + auto error = node->add_child_element("error"); + error->set_attribute("type", "auth"); + auto notAuthorized = error->add_child_element("not-authorized"); + notAuthorized->set_namespace_declaration("urn:ietf:params:xml:ns:xmpp-stanzas"); + return doc; +} + const message::Message kMessage{ .from = {.username = "user1", .server = "server.i2p"}, .to = {.username = "user2", .server = "server.i2p"}, @@ -37,11 +50,18 @@ const message::Error kError{.from = {.username = "user1", .ser } // namespace TEST(Parse, Message) { - auto doc = CreateTestData(); - auto node = doc->get_root_node(); - auto message = Serialization>::Parse(node); + using T = Message; + { + auto doc = CreateTestData(); + auto node = doc->get_root_node(); + auto message = Serialization::Parse(node); + EXPECT_EQ(message, static_cast(kMessage)); + } - EXPECT_EQ(message, kMessage); + auto doc = CreateErrorTestData(); + auto node = doc->get_root_node(); + auto message = Serialization::Parse(node); + EXPECT_EQ(message, static_cast(kError)); } TEST(Serialize, Message) {