Move Presence functions to presence.cpp
This commit is contained in:
parent
2f8e5ff76e
commit
5d991d287c
3 changed files with 99 additions and 70 deletions
|
@ -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<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();
|
||||
}
|
||||
friend auto operator<<(xmlpp::Element* element, const Unavailable&) -> void;
|
||||
[[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional<Unavailable>;
|
||||
[[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<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();
|
||||
}
|
||||
friend auto operator<<(xmlpp::Element*, const Available&) -> void;
|
||||
[[nodiscard]] static auto TryParse(xmlpp::Element* element) -> std::optional<Available>;
|
||||
[[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<Self> {
|
||||
return utils::FieldSetHelper::With<"to", Initial>(std::forward<Self>(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<Self> {
|
||||
return utils::FieldSetHelper::With<"id", Probe>(std::forward<Self>(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
|
||||
|
|
82
library/src/presence.cpp
Normal file
82
library/src/presence.cpp
Normal 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
|
|
@ -112,8 +112,9 @@ 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::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
|
||||
|
|
Loading…
Reference in a new issue