From 5d991d287ce0488e35576846a232715cd625b68a Mon Sep 17 00:00:00 2001 From: sha512sum Date: Mon, 14 Oct 2024 05:54:35 +0000 Subject: [PATCH] Move Presence functions to presence.cpp --- library/include/larra/presence.hpp | 74 ++++----------------------- library/src/presence.cpp | 82 ++++++++++++++++++++++++++++++ library/src/xml_stream.cpp | 13 ++--- 3 files changed, 99 insertions(+), 70 deletions(-) create mode 100644 library/src/presence.cpp diff --git a/library/include/larra/presence.hpp b/library/include/larra/presence.hpp index abe61a7..d55ccf3 100644 --- a/library/include/larra/presence.hpp +++ b/library/include/larra/presence.hpp @@ -11,32 +11,16 @@ namespace c2s { struct Unavailable { static constexpr auto kDefaultName = "presence"; - friend constexpr auto operator<<(xmlpp::Element* element, const Unavailable&) { - element->set_attribute("type", "unavailable"); - } - [[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional { - 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(); - } + friend auto operator<<(xmlpp::Element* element, const Unavailable&) -> void; + [[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional; + [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Unavailable; }; struct Available { static constexpr auto kDefaultName = "presence"; - friend constexpr auto operator<<(xmlpp::Element*, const Available&) { - } - [[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional { - 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(); - } + friend auto operator<<(xmlpp::Element*, const Available&) -> void; + [[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional; + [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Available; }; } // namespace c2s @@ -55,21 +39,8 @@ struct Initial { [[nodiscard]] constexpr auto To(this Self&& self, BareJid value) -> std::decay_t { return utils::FieldSetHelper::With<"to", Initial>(std::forward(self), std::move(value)); } - friend constexpr auto operator<<(xmlpp::Element* element, const Initial& presence) { - element->set_attribute("from", ToString(presence.from)); - 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())}; - } + friend auto operator<<(xmlpp::Element* element, const Initial& presence) -> void; + [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Initial; }; struct Probe { @@ -89,33 +60,8 @@ struct Probe { [[nodiscard]] constexpr auto Id(this Self&& self, std::string value) -> std::decay_t { return utils::FieldSetHelper::With<"id", Probe>(std::forward(self), std::move(value)); } - friend constexpr auto operator<<(xmlpp::Element* element, const Probe& presence) { - element->set_attribute("from", ToString(presence.from)); - 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()}; - } + friend auto operator<<(xmlpp::Element* element, const Probe& presence) -> void; + [[nodiscard]] static auto Parse(xmlpp::Element* element) -> Probe; }; } // namespace larra::xmpp::presence diff --git a/library/src/presence.cpp b/library/src/presence.cpp new file mode 100644 index 0000000..18779a1 --- /dev/null +++ b/library/src/presence.cpp @@ -0,0 +1,82 @@ +#include + +namespace larra::xmpp::presence { + +namespace c2s { + +auto Unavailable::TryParse(xmlpp::Element* element) -> std::optional { + 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 { + 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 diff --git a/library/src/xml_stream.cpp b/library/src/xml_stream.cpp index 897916e..522f2a8 100644 --- a/library/src/xml_stream.cpp +++ b/library/src/xml_stream.cpp @@ -112,12 +112,13 @@ auto CountLines(std::string_view buf) -> std::size_t { 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 { - return std::ranges::fold_left( - GetLines(buf) | std::views::take(error->line - alreadyCountedLines) | std::views::transform([](auto&& line) -> std::size_t { - return std::ranges::distance(line) + 1; - }), - error->int2 - 1, // columns - std::plus<>{}); + return std::ranges::fold_left(GetLines(buf) // + | std::views::take(error->line - alreadyCountedLines) // + | std::views::transform([](auto&& line) -> std::size_t { // + return std::ranges::distance(line) + 1; + }), + error->int2 - 1, // columns + std::plus<>{}); } auto IsExtraContentAtTheDocument(const xmlError* error) -> bool {