diff --git a/library/include/larra/bind.hpp b/library/include/larra/bind.hpp index ccc2a57..bdbdb90 100644 --- a/library/include/larra/bind.hpp +++ b/library/include/larra/bind.hpp @@ -47,7 +47,4 @@ using SetBind = Set; using ResultBind = Result; using IqBind = Iq; -inline auto MakeSetBind() { - return SetBind{.id = "1", .payload = Bind{}}; -} } // namespace larra::xmpp::iq diff --git a/library/include/larra/client/client.hpp b/library/include/larra/client/client.hpp index ba80489..3e467b9 100644 --- a/library/include/larra/client/client.hpp +++ b/library/include/larra/client/client.hpp @@ -80,7 +80,7 @@ struct Client { auto CreateResourceBind() -> boost::asio::awaitable { SPDLOG_INFO("Send IQ: Set::Bind"); - co_await this->Send(::iq::MakeSetBind()); + co_await this->Send(::iq::SetBind{.id = "1", .payload = ::iq::Bind{}}); auto bind_result = co_await connection.template Read<::iq::ResultBind>(); jid.resource = std::move(bind_result.payload.jid->resource); @@ -89,7 +89,7 @@ struct Client { auto UpdateListOfContacts() -> boost::asio::awaitable { SPDLOG_INFO("Send IQ: Get::Roster"); - co_await this->Send(::iq::MakeGetRoster(jid)); + co_await this->Send(::iq::GetRoster{.id = "1", .from = ToString(jid), .payload = ::iq::Roster{}}); const auto roster_result = co_await connection.template Read<::iq::ResultRoster>(); roster = std::move(roster_result.payload); @@ -143,14 +143,15 @@ struct Challenge { throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name())); } std::string decoded = DecodeBase64(node->get_first_child_text()->get_content()); - auto params = std::views::split(decoded, ',') | std::views::transform([](auto param) { - return std::string_view{param}; - }) | - std::views::transform([](std::string_view param) -> std::pair { - auto v = param.find("="); - return {param.substr(0, v), param.substr(v + 1)}; - }) | - std::ranges::to>(); + auto params = std::views::split(decoded, ',') // + | std::views::transform([](auto param) { // + return std::string_view{param}; // + }) // + | std::views::transform([](std::string_view param) -> std::pair { // + auto v = param.find("="); // + return {param.substr(0, v), param.substr(v + 1)}; // + }) // + | std::ranges::to>(); return {.body = std::move(decoded), .serverNonce = params.at("r"), .salt = DecodeBase64(params.at("s")), diff --git a/library/include/larra/iq.hpp b/library/include/larra/iq.hpp index 5efd612..ef0cee8 100644 --- a/library/include/larra/iq.hpp +++ b/library/include/larra/iq.hpp @@ -37,10 +37,10 @@ struct BaseImplWithPayload { friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) { element->set_attribute("id", self.id); - if (self.to) { + if(self.to) { element->set_attribute("to", *self.to); } - if (self.from) { + if(self.from) { element->set_attribute("from", *self.from); } element->set_attribute("type", kName); @@ -62,7 +62,6 @@ struct BaseImplWithPayload { } auto from = element->get_attribute("from"); auto to = element->get_attribute("to"); - using S = Serialization; auto payload = element->get_first_child(S::kDefaultName); @@ -73,11 +72,10 @@ struct BaseImplWithPayload { if(!payload2) { throw std::runtime_error("Invalid payload for parse Iq"); } - return { - .id = idNode->get_value(), - .from = (from ? std::optional{from->get_value()} : std::nullopt), - .to = (to ? std::optional{to->get_value()} : std::nullopt), - .payload = S::Parse(payload2)}; + return {.id = idNode->get_value(), + .from = (from ? std::optional{from->get_value()} : std::nullopt), + .to = (to ? std::optional{to->get_value()} : std::nullopt), + .payload = S::Parse(payload2)}; } }; static constexpr auto kGetName = "get"; diff --git a/library/include/larra/jid.hpp b/library/include/larra/jid.hpp index 1ea3cd8..34dcad4 100644 --- a/library/include/larra/jid.hpp +++ b/library/include/larra/jid.hpp @@ -54,9 +54,10 @@ struct FullJid { struct BareJid { std::string username; std::string server; - constexpr operator FullJid() const { - return FullJid{.username = username, .server = server, .resource = ""}; + constexpr operator FullJid(this auto&& self) { + return {.username = std::forward_like(self.username), .server = std::forward_like(self.server)}; } + [[nodiscard]] static auto Parse(std::string_view jid) -> BareJid; friend auto ToString(const BareJid& jid) -> std::string; diff --git a/library/include/larra/roster.hpp b/library/include/larra/roster.hpp index b1e6350..c0635da 100644 --- a/library/include/larra/roster.hpp +++ b/library/include/larra/roster.hpp @@ -38,9 +38,9 @@ struct Roster { } friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) { element->set_attribute("xmlns", Roster::kDefaultNamespace); - std::ranges::for_each(roster.items, [element](const auto& item) { + for(const auto& item : roster.items) { element->add_child_element("item")->set_attribute("jid", ToString(item)); - }); + } } [[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster { const auto& item_nodes = element->get_children("item"); @@ -69,8 +69,4 @@ using GetRoster = Get; using ResultRoster = Result; using IqRoster = Iq; -inline auto MakeGetRoster(const FullJid& jid) { - return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}}; -} - } // namespace larra::xmpp::iq diff --git a/library/include/larra/utils.hpp b/library/include/larra/utils.hpp index f3a702e..214e676 100644 --- a/library/include/larra/utils.hpp +++ b/library/include/larra/utils.hpp @@ -297,7 +297,7 @@ struct RangeToWrapper : T { template concept LengthCalculatable = requires(const T& obj) { - { obj.length() } -> std::convertible_to; // Checks if obj has a length() method returning a type convertible to std::size_t + { obj.length() } -> std::convertible_to; } || std::convertible_to; template diff --git a/tests/roster.cpp b/tests/roster.cpp index f6b6c45..b30e15b 100644 --- a/tests/roster.cpp +++ b/tests/roster.cpp @@ -1,17 +1,13 @@ #include +#include #include -#include "larra/jid.hpp" - namespace larra::xmpp { TEST(Roster, SerializeAndParse) { FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT - auto roster = iq::MakeGetRoster(jid); - roster.payload.items.emplace_back("u1", "s1"); - roster.payload.items.emplace_back("u2", "s2"); - roster.payload.items.emplace_back("u3", "s3"); + auto roster = iq::GetRoster{.id = "1", .from = ToString(jid), .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}}; xmlpp::Document doc; auto node = doc.create_root_node("iq"); @@ -22,18 +18,19 @@ TEST(Roster, SerializeAndParse) { ASSERT_EQ(roster.payload.items.size(), parse_res.payload.items.size()); for(const auto& [idx, expect_el, parsed_el] : std::views::zip(std::views::iota(0), roster.payload.items, parse_res.payload.items)) { EXPECT_EQ(expect_el, parsed_el) << "Mismatched on idx: " << idx; - // std::cerr << " " << "idx: " << idx << "; expect_el: " << expect_el << "; parsed_el: " << parsed_el << '\n'; } } +static constexpr std::string_view kRosterPrintExpectedData = "Roster: [\n\tu1@s1\n\tu2@s2\n\tu3@s3\n\t]"; TEST(Roster, Print) { FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT - auto roster = iq::MakeGetRoster(jid); - roster.payload.items.emplace_back("u1", "s1"); - roster.payload.items.emplace_back("u2", "s2"); - roster.payload.items.emplace_back("u3", "s3"); + auto roster = iq::GetRoster{.id = "1", .from = ToString(jid), .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}}; - EXPECT_NO_THROW({ std::cerr << "[ ] Roster payload: " << ToString(roster.payload) << '\n'; }); + EXPECT_NO_THROW({ + auto roster_str = ToString(roster.payload); + EXPECT_EQ(kRosterPrintExpectedData.length(), roster_str.capacity()); + EXPECT_EQ(kRosterPrintExpectedData, roster_str); + }); } } // namespace larra::xmpp