Compare commits
1 commit
495b58489e
...
a4e973ca32
Author | SHA1 | Date | |
---|---|---|---|
a4e973ca32 |
7 changed files with 81 additions and 76 deletions
|
@ -14,37 +14,11 @@ struct Bind {
|
|||
|
||||
std::optional<FullJid> jid;
|
||||
|
||||
friend constexpr auto operator<<(xmlpp::Element* element, const Bind& bind) {
|
||||
element->set_attribute("xmlns", Bind::kDefaultNamespace);
|
||||
|
||||
if(bind.jid) {
|
||||
auto* jid_el = element->add_child_element("jid");
|
||||
jid_el->add_child_text(ToString(*bind.jid));
|
||||
}
|
||||
}
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Bind {
|
||||
const auto* jid_node = element->get_first_child("jid");
|
||||
if(!jid_node) {
|
||||
SPDLOG_DEBUG("No Jid Node at Iq::Bind");
|
||||
return {};
|
||||
}
|
||||
|
||||
auto* jid_el = dynamic_cast<const xmlpp::Element*>(jid_node);
|
||||
if(!jid_el) {
|
||||
throw std::runtime_error("dynamic_cast to const xmlpp::Element* failed");
|
||||
}
|
||||
|
||||
const auto* text = jid_el->get_first_child_text();
|
||||
if(!jid_el) {
|
||||
throw std::runtime_error("No text at Iq::Bind jid child");
|
||||
}
|
||||
|
||||
return {.jid = (jid_node ? std::optional{FullJid::Parse(text->get_content())} : std::nullopt)};
|
||||
}
|
||||
friend auto operator<<(xmlpp::Element* element, const Bind& bind) -> void;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> Bind;
|
||||
};
|
||||
|
||||
using SetBind = Set<Bind>;
|
||||
using ResultBind = Result<Bind>;
|
||||
using IqBind = Iq<Bind>;
|
||||
|
||||
} // namespace larra::xmpp::iq
|
||||
|
|
|
@ -24,11 +24,11 @@ struct BaseImplWithPayload {
|
|||
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
|
||||
}
|
||||
template <typename Self>
|
||||
[[nodiscard]] constexpr auto To(this Self&& self, std::string to) -> std::decay_t<Self> {
|
||||
[[nodiscard]] constexpr auto To(this Self&& self, Jid to) -> std::decay_t<Self> {
|
||||
return utils::FieldSetHelper::With<"to", BaseImplWithPayload>(std::forward<Self>(self), std::move(to));
|
||||
}
|
||||
template <typename Self>
|
||||
[[nodiscard]] constexpr auto From(this Self&& self, std::string from) -> std::decay_t<Self> {
|
||||
[[nodiscard]] constexpr auto From(this Self&& self, Jid from) -> std::decay_t<Self> {
|
||||
return utils::FieldSetHelper::With<"from", BaseImplWithPayload>(std::forward<Self>(self), std::move(from));
|
||||
}
|
||||
template <typename NewPayloadType, typename Self>
|
||||
|
@ -87,8 +87,8 @@ struct BaseImplWithPayload {
|
|||
throw std::runtime_error("Invalid payload for parse Iq");
|
||||
}
|
||||
return {.id = idNode->get_value(),
|
||||
.from = (from ? std::optional{BareJid::Parse(from->get_value())} : std::nullopt),
|
||||
.to = (to ? std::optional{BareJid::Parse(to->get_value())} : std::nullopt),
|
||||
.from = (from ? std::optional{Jid::Parse(from->get_value())} : std::nullopt),
|
||||
.to = (to ? std::optional{Jid::Parse(to->get_value())} : std::nullopt),
|
||||
.payload = S::Parse(payload2)};
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
#include <larra/iq.hpp>
|
||||
#include <larra/jid.hpp>
|
||||
#include <larra/serialization/auto.hpp>
|
||||
#include <larra/utils.hpp>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
|
@ -18,8 +17,8 @@ struct RosterItem {
|
|||
return ToString(item.jid);
|
||||
}
|
||||
constexpr auto operator==(const RosterItem&) const -> bool = default;
|
||||
friend constexpr auto operator<<(xmlpp::Element* element, const RosterItem& item);
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> RosterItem;
|
||||
friend auto operator<<(xmlpp::Element* element, const RosterItem& item) -> void;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> RosterItem;
|
||||
};
|
||||
|
||||
struct Roster {
|
||||
|
@ -47,40 +46,11 @@ struct Roster {
|
|||
}
|
||||
return s += suffix;
|
||||
}
|
||||
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster);
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster;
|
||||
friend auto operator<<(xmlpp::Element* element, const Roster& roster) -> void;
|
||||
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> Roster;
|
||||
};
|
||||
|
||||
using GetRoster = Get<Roster>;
|
||||
using ResultRoster = Result<Roster>;
|
||||
using IqRoster = Iq<Roster>;
|
||||
|
||||
} // namespace larra::xmpp::iq
|
||||
|
||||
namespace larra::xmpp::serialization {
|
||||
namespace iq = larra::xmpp::iq;
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<iq::RosterItem> = SerializationConfig<iq::RosterItem>{};
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<iq::Roster> = SerializationConfig<iq::Roster>{}.With<"items">({Config<std::vector<iq::RosterItem>>{}});
|
||||
} // namespace larra::xmpp::serialization
|
||||
|
||||
namespace larra::xmpp::iq {
|
||||
namespace S = larra::xmpp::serialization;
|
||||
|
||||
constexpr auto operator<<(xmlpp::Element* element, const RosterItem& self) {
|
||||
S::Serialize(element, self);
|
||||
}
|
||||
constexpr auto RosterItem::Parse(xmlpp::Element* element) -> RosterItem {
|
||||
return S::Parse<RosterItem>(element);
|
||||
}
|
||||
|
||||
constexpr auto operator<<(xmlpp::Element* element, const Roster& self) {
|
||||
element->set_attribute("xmlns", Roster::kDefaultNamespace);
|
||||
S::Serialize(element, self);
|
||||
}
|
||||
constexpr auto Roster::Parse(xmlpp::Element* element) -> Roster {
|
||||
return S::Parse<Roster>(element);
|
||||
}
|
||||
} // namespace larra::xmpp::iq
|
||||
|
|
|
@ -302,11 +302,11 @@ concept LengthCalculatable = requires(const T& obj) {
|
|||
|
||||
template <typename T>
|
||||
auto AccumulateFieldLength(const T& obj) -> std::size_t {
|
||||
std::size_t total_length = 0;
|
||||
std::size_t totalLength = 0;
|
||||
boost::pfr::for_each_field(obj, [&](const LengthCalculatable auto& field) {
|
||||
total_length += field.length(); // Accumulate length of each field
|
||||
totalLength += field.length(); // Accumulate length of each field
|
||||
});
|
||||
return total_length;
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp::utils
|
||||
|
|
31
library/src/bind.cpp
Normal file
31
library/src/bind.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include <larra/bind.hpp>
|
||||
|
||||
namespace larra::xmpp::iq {
|
||||
auto operator<<(xmlpp::Element* element, const Bind& bind) -> void {
|
||||
element->set_attribute("xmlns", Bind::kDefaultNamespace);
|
||||
|
||||
if(bind.jid) {
|
||||
auto* jid_el = element->add_child_element("jid");
|
||||
jid_el->add_child_text(ToString(*bind.jid));
|
||||
}
|
||||
}
|
||||
[[nodiscard]] auto Bind::Parse(xmlpp::Element* element) -> Bind {
|
||||
const auto* jid_node = element->get_first_child("jid");
|
||||
if(!jid_node) {
|
||||
SPDLOG_DEBUG("No Jid Node at Iq::Bind");
|
||||
return {};
|
||||
}
|
||||
|
||||
auto* jid_el = dynamic_cast<const xmlpp::Element*>(jid_node);
|
||||
if(!jid_el) {
|
||||
throw std::runtime_error("dynamic_cast to const xmlpp::Element* failed");
|
||||
}
|
||||
|
||||
const auto* text = jid_el->get_first_child_text();
|
||||
if(!jid_el) {
|
||||
throw std::runtime_error("No text at Iq::Bind jid child");
|
||||
}
|
||||
|
||||
return {.jid = (jid_node ? std::optional{FullJid::Parse(text->get_content())} : std::nullopt)};
|
||||
}
|
||||
} // namespace larra::xmpp::iq
|
30
library/src/roster.cpp
Normal file
30
library/src/roster.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <larra/roster.hpp>
|
||||
#include <larra/serialization/auto.hpp>
|
||||
|
||||
namespace larra::xmpp::serialization {
|
||||
namespace iq = larra::xmpp::iq;
|
||||
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<iq::RosterItem> = SerializationConfig<iq::RosterItem>{};
|
||||
template <>
|
||||
constexpr auto kSerializationConfig<iq::Roster> = SerializationConfig<iq::Roster>{}.With<"items">({Config<std::vector<iq::RosterItem>>{}});
|
||||
} // namespace larra::xmpp::serialization
|
||||
|
||||
namespace larra::xmpp::iq {
|
||||
namespace S = larra::xmpp::serialization;
|
||||
|
||||
auto operator<<(xmlpp::Element* element, const RosterItem& self) -> void {
|
||||
S::Serialize(element, self);
|
||||
}
|
||||
auto RosterItem::Parse(xmlpp::Element* element) -> RosterItem {
|
||||
return S::Parse<RosterItem>(element);
|
||||
}
|
||||
|
||||
auto operator<<(xmlpp::Element* element, const Roster& self) -> void {
|
||||
element->set_attribute("xmlns", Roster::kDefaultNamespace);
|
||||
S::Serialize(element, self);
|
||||
}
|
||||
auto Roster::Parse(xmlpp::Element* element) -> Roster {
|
||||
return S::Parse<Roster>(element);
|
||||
}
|
||||
} // namespace larra::xmpp::iq
|
|
@ -13,11 +13,11 @@ TEST(Roster, SerializeAndParse) {
|
|||
auto node = doc.create_root_node("iq");
|
||||
node << roster;
|
||||
|
||||
auto parse_res = decltype(roster)::Parse(node);
|
||||
auto parseRes = decltype(roster)::Parse(node);
|
||||
|
||||
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;
|
||||
ASSERT_EQ(roster.payload.items.size(), parseRes.payload.items.size());
|
||||
for(const auto& [idx, expectEl, parsedEl] : std::views::zip(std::views::iota(0), roster.payload.items, parseRes.payload.items)) {
|
||||
EXPECT_EQ(expectEl, parsedEl) << "Mismatched on idx: " << idx;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,9 +27,9 @@ TEST(Roster, Print) {
|
|||
auto roster = iq::GetRoster{.id = "1", .from = jid, .payload = iq::Roster{.items = {{"u1", "s1"}, {"u2", "s2"}, {"u3", "s3"}}}};
|
||||
|
||||
EXPECT_NO_THROW({
|
||||
auto roster_str = ToString(roster.payload);
|
||||
EXPECT_EQ(kRosterPrintExpectedData.length(), roster_str.capacity());
|
||||
EXPECT_EQ(kRosterPrintExpectedData, roster_str);
|
||||
auto rosterStr = ToString(roster.payload);
|
||||
EXPECT_EQ(kRosterPrintExpectedData.length(), rosterStr.capacity());
|
||||
EXPECT_EQ(kRosterPrintExpectedData, rosterStr);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue