larra/library/include/larra/jid.hpp

112 lines
4.1 KiB
C++

#pragma once
#include <format>
#include <larra/utils.hpp>
#include <string>
#include <utility>
#include <variant>
namespace larra::xmpp {
struct BareJid {
std::string username;
std::string server;
[[nodiscard]] static auto Parse(std::string_view jid) -> BareJid;
friend auto ToString(const BareJid& jid) -> std::string;
constexpr auto operator==(const BareJid&) const -> bool = default;
template <typename Self>
[[nodiscard]] constexpr auto Username(this Self&& self, std::string username) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"username", BareJid>(std::forward<Self>(self), std::move(username));
}
template <typename Self>
[[nodiscard]] constexpr auto Server(this Self&& self, std::string server) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"server", BareJid>(std::forward<Self>(self), std::move(server));
}
};
struct BareResourceJid {
std::string server;
std::string resource;
[[nodiscard]] static auto Parse(std::string_view jid) -> BareResourceJid;
friend auto ToString(const BareResourceJid& jid) -> std::string;
constexpr auto operator==(const BareResourceJid&) const -> bool = default;
template <typename Self>
[[nodiscard]] constexpr auto Server(this Self&& self, std::string server) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"server", BareResourceJid>(std::forward<Self>(self), std::move(server));
}
template <typename Self>
[[nodiscard]] constexpr auto Resource(this Self&& self, std::string resource) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"resource", BareResourceJid>(std::forward<Self>(self), std::move(resource));
}
};
struct FullJid {
std::string username;
std::string server;
std::string resource;
[[nodiscard]] static auto Parse(std::string_view jid) -> FullJid;
friend auto ToString(const FullJid& jid) -> std::string;
constexpr auto operator==(const FullJid&) const -> bool = default;
template <typename Self>
[[nodiscard]] constexpr auto Username(this Self&& self, std::string username) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"username", FullJid>(std::forward<Self>(self), std::move(username));
}
template <typename Self>
[[nodiscard]] constexpr auto Server(this Self&& self, std::string server) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"server", FullJid>(std::forward<Self>(self), std::move(server));
}
template <typename Self>
[[nodiscard]] constexpr auto Resource(this Self&& self, std::string resource) -> std::decay_t<Self> {
return utils::FieldSetHelper::With<"resource", FullJid>(std::forward<Self>(self), std::move(resource));
}
};
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
struct Jid : JidVariant {
using JidVariant::variant;
[[nodiscard]] static auto Parse(std::string_view jid) -> Jid;
friend auto ToString(const Jid& jid) -> std::string;
};
} // namespace larra::xmpp
template <>
struct std::formatter<larra::xmpp::Jid> : std::formatter<std::string> {
template <typename FormatContext>
constexpr auto format(const larra::xmpp::Jid& arg, FormatContext& ctx) const -> FormatContext::iterator {
return std::formatter<std::string>::format(ToString(arg), ctx);
};
};
template <>
struct std::formatter<larra::xmpp::FullJid> : std::formatter<std::string> {
template <typename FormatContext>
constexpr auto format(const larra::xmpp::FullJid& arg, FormatContext& ctx) const -> FormatContext::iterator {
return std::formatter<std::string>::format(ToString(arg), ctx);
};
};
template <>
struct std::formatter<larra::xmpp::BareJid> : std::formatter<std::string> {
template <typename FormatContext>
constexpr auto format(const larra::xmpp::BareJid& arg, FormatContext& ctx) const -> FormatContext::iterator {
return std::formatter<std::string>::format(ToString(arg), ctx);
};
};
template <>
struct std::formatter<larra::xmpp::BareResourceJid> : std::formatter<std::string> {
template <typename FormatContext>
constexpr auto format(const larra::xmpp::BareResourceJid& arg, FormatContext& ctx) const -> FormatContext::iterator {
return std::formatter<std::string>::format(ToString(arg), ctx);
};
};