Move Presence functions to presence.cpp

This commit is contained in:
sha512sum 2024-10-14 05:54:35 +00:00
parent 2f8e5ff76e
commit 5d991d287c
3 changed files with 99 additions and 70 deletions

View file

@ -11,32 +11,16 @@ namespace c2s {
struct Unavailable { struct Unavailable {
static constexpr auto kDefaultName = "presence"; static constexpr auto kDefaultName = "presence";
friend constexpr auto operator<<(xmlpp::Element* element, const Unavailable&) { friend auto operator<<(xmlpp::Element* element, const Unavailable&) -> void;
element->set_attribute("type", "unavailable"); [[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional<Unavailable>;
} [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Unavailable;
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<Unavailable> {
if(auto type = element->get_attribute("type")) {
if(type->get_value() == "unavailable") {
return Unavailable{};
};
}
return std::nullopt;
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Unavailable {
return Unavailable::TryParse(element).value();
}
}; };
struct Available { struct Available {
static constexpr auto kDefaultName = "presence"; static constexpr auto kDefaultName = "presence";
friend constexpr auto operator<<(xmlpp::Element*, const Available&) { friend auto operator<<(xmlpp::Element*, const Available&) -> void;
} [[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional<Available>;
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<Available> { [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Available;
return Unavailable::TryParse(element).has_value() ? std::nullopt : std::optional{Available{}};
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Available {
return Available::TryParse(element).value();
}
}; };
} // namespace c2s } // namespace c2s
@ -55,21 +39,8 @@ struct Initial {
[[nodiscard]] constexpr auto To(this Self&& self, BareJid value) -> std::decay_t<Self> { [[nodiscard]] constexpr auto To(this Self&& self, BareJid value) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"to", Initial>(std::forward<Self>(self), std::move(value)); return utils::FieldSetHelper::With<"to", Initial>(std::forward<Self>(self), std::move(value));
} }
friend constexpr auto operator<<(xmlpp::Element* element, const Initial& presence) { friend auto operator<<(xmlpp::Element* element, const Initial& presence) -> void;
element->set_attribute("from", ToString(presence.from)); [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Initial;
element->set_attribute("to", ToString(presence.to));
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Initial {
auto from = element->get_attribute("from");
if(!from) {
throw std::runtime_error("Not found attribute from for Parse presence::Initial");
}
auto to = element->get_attribute("to");
if(!to) {
throw std::runtime_error("Not found attribute to for Parse presence::Initial");
}
return {.from = FullJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value())};
}
}; };
struct Probe { struct Probe {
@ -89,33 +60,8 @@ struct Probe {
[[nodiscard]] constexpr auto Id(this Self&& self, std::string value) -> std::decay_t<Self> { [[nodiscard]] constexpr auto Id(this Self&& self, std::string value) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"id", Probe>(std::forward<Self>(self), std::move(value)); return utils::FieldSetHelper::With<"id", Probe>(std::forward<Self>(self), std::move(value));
} }
friend constexpr auto operator<<(xmlpp::Element* element, const Probe& presence) { friend auto operator<<(xmlpp::Element* element, const Probe& presence) -> void;
element->set_attribute("from", ToString(presence.from)); [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Probe;
element->set_attribute("to", ToString(presence.to));
}
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Probe {
auto type = element->get_attribute("type");
if(!type) {
throw std::runtime_error("Not found attribute type for Parse presence::Probe");
}
if(type->get_value() != "probe") {
throw std::runtime_error("Invalid attribute type value for Parse presence::Probe");
}
auto from = element->get_attribute("from");
if(!from) {
throw std::runtime_error("Not found attribute from for Parse presence::Probe");
}
auto to = element->get_attribute("to");
if(!to) {
throw std::runtime_error("Not found attribute to for Parse presence::Probe");
}
auto id = element->get_attribute("id");
if(!id) {
throw std::runtime_error("Not found attribute id for Parse presence::Probe");
}
return {.from = BareJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value()), .id = id->get_value()};
}
}; };
} // namespace larra::xmpp::presence } // namespace larra::xmpp::presence

82
library/src/presence.cpp Normal file
View file

@ -0,0 +1,82 @@
#include <larra/presence.hpp>
namespace larra::xmpp::presence {
namespace c2s {
auto Unavailable::TryParse(xmlpp::Element* element) -> std::optional<Unavailable> {
if(auto type = element->get_attribute("type")) {
if(type->get_value() == "unavailable") {
return Unavailable{};
}
}
return std::nullopt;
}
auto Unavailable::Parse(xmlpp::Element* element) -> Unavailable {
return Unavailable::TryParse(element).value();
}
auto operator<<(xmlpp::Element* element, const Unavailable&) -> void {
element->set_attribute("type", "unavailable");
}
auto Available::TryParse(xmlpp::Element* element) -> std::optional<Available> {
return Unavailable::TryParse(element).has_value() ? std::nullopt : std::optional{Available{}};
}
auto Available::Parse(xmlpp::Element* element) -> Available {
return Available::TryParse(element).value();
}
auto operator<<(xmlpp::Element* element, const Available&) -> void {
}
} // namespace c2s
auto Initial::Parse(xmlpp::Element* element) -> Initial {
auto from = element->get_attribute("from");
if(!from) {
throw std::runtime_error("Not found attribute from for Parse presence::Initial");
}
auto to = element->get_attribute("to");
if(!to) {
throw std::runtime_error("Not found attribute to for Parse presence::Initial");
}
return {.from = FullJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value())};
}
auto operator<<(xmlpp::Element* element, const Initial& presence) -> void {
element->set_attribute("from", ToString(presence.from));
element->set_attribute("to", ToString(presence.to));
}
auto Probe::Parse(xmlpp::Element* element) -> Probe {
auto type = element->get_attribute("type");
if(!type) {
throw std::runtime_error("Not found attribute type for Parse presence::Probe");
}
if(type->get_value() != "probe") {
throw std::runtime_error("Invalid attribute type value for Parse presence::Probe");
}
auto from = element->get_attribute("from");
if(!from) {
throw std::runtime_error("Not found attribute from for Parse presence::Probe");
}
auto to = element->get_attribute("to");
if(!to) {
throw std::runtime_error("Not found attribute to for Parse presence::Probe");
}
auto id = element->get_attribute("id");
if(!id) {
throw std::runtime_error("Not found attribute id for Parse presence::Probe");
}
return {.from = BareJid::Parse(from->get_value()), .to = BareJid::Parse(to->get_value()), .id = id->get_value()};
}
auto operator<<(xmlpp::Element* element, const Probe& presence) -> void {
element->set_attribute("from", ToString(presence.from));
element->set_attribute("to", ToString(presence.to));
}
} // namespace larra::xmpp::presence

View file

@ -112,8 +112,9 @@ auto CountLines(std::string_view buf) -> std::size_t {
return std::ranges::distance(buf | std::views::split('\n')); return std::ranges::distance(buf | std::views::split('\n'));
} }
auto GetIndex(const boost::asio::streambuf& buf, const xmlError* error, std::size_t alreadyCountedLines) -> std::size_t { auto GetIndex(const boost::asio::streambuf& buf, const xmlError* error, std::size_t alreadyCountedLines) -> std::size_t {
return std::ranges::fold_left( return std::ranges::fold_left(GetLines(buf) //
GetLines(buf) | std::views::take(error->line - alreadyCountedLines) | std::views::transform([](auto&& line) -> std::size_t { | std::views::take(error->line - alreadyCountedLines) //
| std::views::transform([](auto&& line) -> std::size_t { //
return std::ranges::distance(line) + 1; return std::ranges::distance(line) + 1;
}), }),
error->int2 - 1, // columns error->int2 - 1, // columns