Compare commits
11 commits
bd80c9684d
...
7685385559
Author | SHA1 | Date | |
---|---|---|---|
7685385559 | |||
77640de2e9 | |||
96e65dd2c6 | |||
662392509f | |||
9273c473f8 | |||
13d063915e | |||
76ebf081c2 | |||
d1dc6f8e24 | |||
0d471b579a | |||
2c0111f9d7 | |||
ab3fc26003 |
17 changed files with 964 additions and 71 deletions
|
@ -10,7 +10,7 @@ FROM archlinux@sha256:a10e51dd0694d6c4142754e9d06cbce7baf91ace8031a30df37064d109
|
||||||
RUN pacman -Syyu --noconfirm \
|
RUN pacman -Syyu --noconfirm \
|
||||||
&& pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \
|
&& pacman -Syyu --noconfirm git less vim sudo python-pip wget which pkgconf \
|
||||||
&& pacman -Syyu --noconfirm cmake make gcc ninja meson shellcheck \
|
&& pacman -Syyu --noconfirm cmake make gcc ninja meson shellcheck \
|
||||||
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
|
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0 gtest
|
||||||
|
|
||||||
# Create a non-root user 'dev'
|
# Create a non-root user 'dev'
|
||||||
RUN useradd -ms /bin/bash dev \
|
RUN useradd -ms /bin/bash dev \
|
||||||
|
@ -38,6 +38,7 @@ ADD https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/L
|
||||||
# Create the LLVM directory and extract only binaries into it
|
# Create the LLVM directory and extract only binaries into it
|
||||||
RUN sudo mkdir -p /home/LLVM-${LLVM_VER}
|
RUN sudo mkdir -p /home/LLVM-${LLVM_VER}
|
||||||
RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1 \
|
RUN sudo tar -xJf /home/artifacts/LLVM-${LLVM_VER}-Linux-X64.tar.xz -C /home/LLVM-${LLVM_VER} --strip-components=1 \
|
||||||
|
LLVM-${LLVM_VER}-Linux-X64/bin/clangd \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \
|
LLVM-${LLVM_VER}-Linux-X64/bin/clang-19 \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang \
|
LLVM-${LLVM_VER}-Linux-X64/bin/clang \
|
||||||
LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \
|
LLVM-${LLVM_VER}-Linux-X64/bin/clang++ \
|
||||||
|
|
|
@ -11,11 +11,11 @@ jobs:
|
||||||
pacman -Syyu --noconfirm
|
pacman -Syyu --noconfirm
|
||||||
pacman -S --noconfirm git less vim sudo python-pip wget which pkgconf nodejs-lts-iron
|
pacman -S --noconfirm git less vim sudo python-pip wget which pkgconf nodejs-lts-iron
|
||||||
pacman -S --noconfirm cmake make gcc ninja meson
|
pacman -S --noconfirm cmake make gcc ninja meson
|
||||||
pacman -S --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
|
pacman -S --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0 gtest
|
||||||
|
|
||||||
- name: Setup environment - Install LLVM-19.1.1
|
- name: Setup environment - Install LLVM-19.1.3
|
||||||
run: |
|
run: |
|
||||||
export LLVM_VER=19.1.1
|
export LLVM_VER=19.1.3
|
||||||
|
|
||||||
echo "::group::Download LLVM-${LLVM_VER}"
|
echo "::group::Download LLVM-${LLVM_VER}"
|
||||||
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/LLVM-${LLVM_VER}-Linux-X64.tar.xz -O /LLVM-${LLVM_VER}-Linux-X64.tar.xz
|
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VER}/LLVM-${LLVM_VER}-Linux-X64.tar.xz -O /LLVM-${LLVM_VER}-Linux-X64.tar.xz
|
||||||
|
@ -105,8 +105,10 @@ jobs:
|
||||||
# key: LLVM-${LLVM_VER}-Linux-X64-small
|
# key: LLVM-${LLVM_VER}-Linux-X64-small
|
||||||
|
|
||||||
- name: Check clang-format
|
- name: Check clang-format
|
||||||
|
id: clang_format_check
|
||||||
|
continue-on-error: true
|
||||||
run: |
|
run: |
|
||||||
export LLVM_VER=19.1.1
|
export LLVM_VER=19.1.3
|
||||||
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||||
cd ${{ github.workspace }}
|
cd ${{ github.workspace }}
|
||||||
export REPO_FILES=$(cat repo_files_to_check.txt)
|
export REPO_FILES=$(cat repo_files_to_check.txt)
|
||||||
|
@ -120,6 +122,8 @@ jobs:
|
||||||
echo "No clang-format violations detected!"
|
echo "No clang-format violations detected!"
|
||||||
|
|
||||||
- name: GCC build
|
- name: GCC build
|
||||||
|
id: gcc_build
|
||||||
|
continue-on-error: true
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ${{ github.workspace }}/build_gcc
|
mkdir -p ${{ github.workspace }}/build_gcc
|
||||||
cmake -Wno-dev \
|
cmake -Wno-dev \
|
||||||
|
@ -129,32 +133,56 @@ jobs:
|
||||||
-B ${{ github.workspace }}/build_gcc \
|
-B ${{ github.workspace }}/build_gcc \
|
||||||
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||||
-DENABLE_EXAMPLES=ON \
|
-DENABLE_EXAMPLES=ON \
|
||||||
-DENABLE_TESTS=ON
|
-DENABLE_TESTS=ON \
|
||||||
|
-DCMAKE_CXX_FLAGS="-ftemplate-backtrace-limit=0"
|
||||||
cmake --build ${{ github.workspace }}/build_gcc --parallel `nproc`
|
cmake --build ${{ github.workspace }}/build_gcc --parallel `nproc`
|
||||||
|
|
||||||
- name: GCC unit tests
|
- name: GCC unit tests
|
||||||
|
id: gcc_unit_tests
|
||||||
|
continue-on-error: true
|
||||||
run: |
|
run: |
|
||||||
cd ${{ github.workspace }}/build_gcc
|
cd ${{ github.workspace }}/build_gcc
|
||||||
./larra_xmpp_tests
|
./larra_xmpp_tests
|
||||||
|
|
||||||
- name: Check clang-tidy
|
- name: Clang build (only configuring)
|
||||||
|
id: clang_build
|
||||||
|
continue-on-error: true
|
||||||
run: |
|
run: |
|
||||||
export LLVM_VER=19.1.1
|
export LLVM_VER=19.1.3
|
||||||
|
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||||
|
mkdir -p ${{ github.workspace }}/build_clang
|
||||||
|
|
||||||
|
cmake -Wno-dev \
|
||||||
|
-DCMAKE_C_COMPILER=clang \
|
||||||
|
-DCMAKE_CXX_COMPILER=clang++ \
|
||||||
|
-S ${{ github.workspace }} \
|
||||||
|
-B ${{ github.workspace }}/build_clang \
|
||||||
|
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||||
|
-DENABLE_EXAMPLES=ON \
|
||||||
|
-DENABLE_TESTS=ON \
|
||||||
|
-DCMAKE_CXX_FLAGS="-stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1 -fno-modules"
|
||||||
|
|
||||||
|
echo "::group::compile_commands.json content"
|
||||||
|
cat ${{ github.workspace }}/build_clang/compile_commands.json
|
||||||
|
echo "::endgroup::"
|
||||||
|
|
||||||
|
sed -i 's|@.*\.modmap||' ${{ github.workspace }}/build_clang/compile_commands.json
|
||||||
|
|
||||||
|
- name: Check clang-tidy
|
||||||
|
id: clang_tidy
|
||||||
|
continue-on-error: true
|
||||||
|
run: |
|
||||||
|
export LLVM_VER=19.1.3
|
||||||
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||||
cd ${{ github.workspace }}
|
cd ${{ github.workspace }}
|
||||||
|
|
||||||
sed -i 's|/usr/sbin/g++|/home/LLVM-19.1.1/bin/clang++ -stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1|' build_gcc/compile_commands.json
|
|
||||||
sed -i 's|-fdeps-format=p1689r5||' build_gcc/compile_commands.json
|
|
||||||
sed -i 's|-fmodules-ts||' build_gcc/compile_commands.json
|
|
||||||
sed -i 's|-fmodule-mapper=.*\.modmap||' build_gcc/compile_commands.json
|
|
||||||
|
|
||||||
wget https://raw.githubusercontent.com/llvm/llvm-project/refs/heads/main/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
|
wget https://raw.githubusercontent.com/llvm/llvm-project/refs/heads/main/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
|
||||||
python run-clang-tidy.py \
|
python run-clang-tidy.py \
|
||||||
-warnings-as-errors=* \
|
-warnings-as-errors=* \
|
||||||
-use-color \
|
-use-color \
|
||||||
-exclude-header-filter .*build.* \
|
-exclude-header-filter .*build.* \
|
||||||
-header-filter .hpp \
|
-header-filter .hpp \
|
||||||
-p ${{ github.workspace }}/build_gcc/ \
|
-p ${{ github.workspace }}/build_clang/ \
|
||||||
-config-file ${{ github.workspace }}/.clang-tidy \
|
-config-file ${{ github.workspace }}/.clang-tidy \
|
||||||
\
|
\
|
||||||
${{ github.workspace }}/examples \
|
${{ github.workspace }}/examples \
|
||||||
|
@ -163,26 +191,13 @@ jobs:
|
||||||
|
|
||||||
echo "No clang-tidy violations detected!"
|
echo "No clang-tidy violations detected!"
|
||||||
|
|
||||||
#- name: Clang build with -fsanitize=address
|
- name: Check on failures
|
||||||
# run: |
|
if: steps.clang_format_check.outcome != 'success' || steps.gcc_build.outcome != 'success' || steps.gcc_unit_tests.outcome != 'success' || steps.clang_build.outcome != 'success' || steps.clang_tidy.outcome != 'success'
|
||||||
# export LLVM_VER=19.1.1
|
run: exit 1
|
||||||
# export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
|
||||||
# mkdir -p ${{ github.workspace }}/build_clang
|
|
||||||
# cmake -Wno-dev \
|
|
||||||
# -DCMAKE_C_COMPILER=clang \
|
|
||||||
# -DCMAKE_CXX_COMPILER=clang++ \
|
|
||||||
# -S ${{ github.workspace }} \
|
|
||||||
# -B ${{ github.workspace }}/build_clang \
|
|
||||||
# -GNinja -DCMAKE_BUILD_TYPE=Release \
|
|
||||||
# -DENABLE_EXAMPLES=ON \
|
|
||||||
# -DENABLE_TESTS=ON \
|
|
||||||
# -DCMAKE_CXX_FLAGS="-stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1 -fno-omit-frame-pointer -g -fsanitize=address,undefined,leak,function,nullability,vptr" \
|
|
||||||
# -DCMAKE_EXE_LINKER_FLAGS="-L/home/LLVM-${LLVM_VER}/lib/ -Wl,-rpath,/home/LLVM-${LLVM_VER}/lib -lc++ -lc++abi -lm -lc -lgcc_s -lgcc -fno-omit-frame-pointer -g -fsanitize=address,undefined,leak,function,nullability,vptr"
|
|
||||||
# cmake --build ${{ github.workspace }}/build_clang --parallel `nproc`
|
|
||||||
|
|
||||||
#- name: Clang unit tests with -fsanitize=address
|
#- name: Clang unit tests with -fsanitize=address
|
||||||
# run: |
|
# run: |
|
||||||
# export LLVM_VER=19.1.1
|
# export LLVM_VER=19.1.3
|
||||||
# export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
# export PATH="/home/LLVM-${LLVM_VER}/bin:${PATH}"
|
||||||
# cd ${{ github.workspace }}/build_clang
|
# cd ${{ github.workspace }}/build_clang
|
||||||
# ASAN_SYMBOLIZER_PATH=llvm-symbolizer ASAN_OPTIONS=detect_stack_use_after_return=1:check_initialization_order=1:detect_leaks=1:atexit=1:abort_on_error=1 ./larra_xmpp_tests
|
# ASAN_SYMBOLIZER_PATH=llvm-symbolizer ASAN_OPTIONS=detect_stack_use_after_return=1:check_initialization_order=1:detect_leaks=1:atexit=1:abort_on_error=1 ./larra_xmpp_tests
|
||||||
|
|
12
.vscode/launch.json
vendored
12
.vscode/launch.json
vendored
|
@ -12,6 +12,18 @@
|
||||||
"args": [],
|
"args": [],
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"preLaunchTask": "GCC: Build"
|
"preLaunchTask": "GCC: Build"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Debug: tests",
|
||||||
|
"program": "${workspaceFolder}/build/larra_xmpp_tests",
|
||||||
|
"args": [
|
||||||
|
// --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
|
||||||
|
"--gtest_filter=Roster*"
|
||||||
|
],
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"preLaunchTask": "GCC: Build"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
|
@ -104,7 +104,7 @@
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"clear": true
|
"clear": true
|
||||||
},
|
},
|
||||||
"hide": true,
|
"hide": false,
|
||||||
"group": {
|
"group": {
|
||||||
"kind": "build"
|
"kind": "build"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <boost/asio/co_spawn.hpp>
|
#include <boost/asio/co_spawn.hpp>
|
||||||
|
@ -7,6 +8,8 @@
|
||||||
#include <larra/printer_stream.hpp>
|
#include <larra/printer_stream.hpp>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
auto Coroutine() -> boost::asio::awaitable<void> {
|
auto Coroutine() -> boost::asio::awaitable<void> {
|
||||||
SPDLOG_INFO("Connecting client...");
|
SPDLOG_INFO("Connecting client...");
|
||||||
|
|
||||||
|
@ -14,11 +17,32 @@ auto Coroutine() -> boost::asio::awaitable<void> {
|
||||||
auto client = co_await larra::xmpp::client::CreateClient<larra::xmpp::PrintStream<boost::asio::ip::tcp::socket>>(
|
auto client = co_await larra::xmpp::client::CreateClient<larra::xmpp::PrintStream<boost::asio::ip::tcp::socket>>(
|
||||||
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
|
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
|
||||||
{.useTls = larra::xmpp::client::Options::kNever});
|
{.useTls = larra::xmpp::client::Options::kNever});
|
||||||
|
|
||||||
|
// rfc6120 7.1
|
||||||
|
// After a client authenticates with a server,
|
||||||
|
// it MUST bind a specific resource to the stream so that the server can properly address the client.
|
||||||
co_await std::visit(
|
co_await std::visit(
|
||||||
[](auto& client) -> boost::asio::awaitable<void> {
|
[](auto& client) -> boost::asio::awaitable<void> {
|
||||||
|
co_await client.CreateResourceBind();
|
||||||
|
},
|
||||||
|
client);
|
||||||
|
|
||||||
|
co_await std::visit(
|
||||||
|
[](auto& client) -> boost::asio::awaitable<void> {
|
||||||
|
co_await client.UpdateListOfContacts();
|
||||||
|
},
|
||||||
|
client);
|
||||||
|
|
||||||
|
// rfc6120 2.2
|
||||||
|
// Upon authenticating with a server and binding a resource (thus becoming a connected resource as defined in [XMPP‑CORE]),
|
||||||
|
// a client SHOULD request the roster before sending initial presence
|
||||||
|
co_await std::visit(
|
||||||
|
[](auto& client) -> boost::asio::awaitable<void> {
|
||||||
|
SPDLOG_INFO("Send presence: Available");
|
||||||
co_await client.Send(larra::xmpp::presence::c2s::Available{});
|
co_await client.Send(larra::xmpp::presence::c2s::Available{});
|
||||||
},
|
},
|
||||||
client);
|
client);
|
||||||
|
|
||||||
} catch(const std::exception& err) {
|
} catch(const std::exception& err) {
|
||||||
SPDLOG_ERROR("{}", err.what());
|
SPDLOG_ERROR("{}", err.what());
|
||||||
co_return;
|
co_return;
|
||||||
|
|
53
library/include/larra/bind.hpp
Normal file
53
library/include/larra/bind.hpp
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#pragma once
|
||||||
|
#include <libxml++/libxml++.h>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <larra/iq.hpp>
|
||||||
|
#include <larra/jid.hpp>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
namespace larra::xmpp::iq {
|
||||||
|
|
||||||
|
struct Bind {
|
||||||
|
static constexpr auto kDefaultName = "bind";
|
||||||
|
static constexpr auto kDefaultNamespace = "urn:ietf:params:xml:ns:xmpp-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)};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using SetBind = Set<Bind>;
|
||||||
|
using ResultBind = Result<Bind>;
|
||||||
|
using IqBind = Iq<Bind>;
|
||||||
|
|
||||||
|
inline auto MakeSetBind() {
|
||||||
|
return SetBind{.id = "1", .payload = Bind{}};
|
||||||
|
}
|
||||||
|
} // namespace larra::xmpp::iq
|
|
@ -11,16 +11,24 @@
|
||||||
#include <boost/asio/ssl.hpp>
|
#include <boost/asio/ssl.hpp>
|
||||||
#include <boost/asio/use_awaitable.hpp>
|
#include <boost/asio/use_awaitable.hpp>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
|
#include <larra/bind.hpp>
|
||||||
#include <larra/client/challenge_response.hpp>
|
#include <larra/client/challenge_response.hpp>
|
||||||
#include <larra/client/options.hpp>
|
#include <larra/client/options.hpp>
|
||||||
#include <larra/client/starttls_response.hpp>
|
#include <larra/client/starttls_response.hpp>
|
||||||
#include <larra/client/xmpp_client_stream_features.hpp>
|
#include <larra/client/xmpp_client_stream_features.hpp>
|
||||||
#include <larra/encryption.hpp>
|
#include <larra/encryption.hpp>
|
||||||
#include <larra/features.hpp>
|
#include <larra/features.hpp>
|
||||||
|
#include <larra/roster.hpp>
|
||||||
#include <larra/stream.hpp>
|
#include <larra/stream.hpp>
|
||||||
#include <larra/user_account.hpp>
|
#include <larra/user_account.hpp>
|
||||||
#include <larra/xml_stream.hpp>
|
#include <larra/xml_stream.hpp>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace rng = std::ranges;
|
||||||
|
namespace views = std::views;
|
||||||
|
namespace iq = larra::xmpp::iq;
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
|
||||||
constexpr auto kDefaultXmppPort = 5222;
|
constexpr auto kDefaultXmppPort = 5222;
|
||||||
|
@ -29,12 +37,10 @@ constexpr auto kDefaultXmppPort = 5222;
|
||||||
|
|
||||||
namespace larra::xmpp::client {
|
namespace larra::xmpp::client {
|
||||||
|
|
||||||
namespace rng = std::ranges;
|
|
||||||
namespace views = std::views;
|
|
||||||
|
|
||||||
template <typename Connection>
|
template <typename Connection>
|
||||||
struct Client {
|
struct Client {
|
||||||
constexpr Client(BareJid jid, XmlStream<Connection> connection) : jid(std::move(jid)), connection(std::move(connection)) {};
|
constexpr Client(BareJid jid, XmlStream<Connection> connection) : jid(std::move(jid)), connection(std::move(connection)) {
|
||||||
|
}
|
||||||
template <boost::asio::completion_token_for<void()> Token = boost::asio::use_awaitable_t<>>
|
template <boost::asio::completion_token_for<void()> Token = boost::asio::use_awaitable_t<>>
|
||||||
constexpr auto Close(Token token = {}) {
|
constexpr auto Close(Token token = {}) {
|
||||||
this->active = false;
|
this->active = false;
|
||||||
|
@ -68,14 +74,35 @@ struct Client {
|
||||||
auto Send(const T& object) -> boost::asio::awaitable<void> {
|
auto Send(const T& object) -> boost::asio::awaitable<void> {
|
||||||
co_await this->connection.Send(object);
|
co_await this->connection.Send(object);
|
||||||
}
|
}
|
||||||
[[nodiscard]] constexpr auto Jid() const -> const BareJid& {
|
[[nodiscard]] constexpr auto Jid() const -> const FullJid& {
|
||||||
return this->jid;
|
return this->jid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto CreateResourceBind() -> boost::asio::awaitable<void> {
|
||||||
|
SPDLOG_INFO("Send IQ: Set::Bind");
|
||||||
|
co_await this->Send(::iq::MakeSetBind());
|
||||||
|
|
||||||
|
auto bind_result = co_await connection.template Read<::iq::ResultBind>();
|
||||||
|
jid.resource = std::move(bind_result.payload.jid->resource);
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto UpdateListOfContacts() -> boost::asio::awaitable<void> {
|
||||||
|
SPDLOG_INFO("Send IQ: Get::Roster");
|
||||||
|
co_await this->Send(::iq::MakeGetRoster(jid));
|
||||||
|
|
||||||
|
const auto roster_result = co_await connection.template Read<::iq::ResultRoster>();
|
||||||
|
roster = std::move(roster_result.payload);
|
||||||
|
|
||||||
|
SPDLOG_INFO("New roster: {}", ToString(roster));
|
||||||
|
co_return;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool active = true;
|
bool active = true;
|
||||||
XmlStream<Connection> connection{};
|
XmlStream<Connection> connection{};
|
||||||
BareJid jid;
|
FullJid jid;
|
||||||
|
::iq::Roster roster;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct StartTlsNegotiationError : std::runtime_error {
|
struct StartTlsNegotiationError : std::runtime_error {
|
||||||
|
@ -116,15 +143,14 @@ struct Challenge {
|
||||||
throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name()));
|
throw std::runtime_error(std::format("Invalid name {} for challenge", node->get_name()));
|
||||||
}
|
}
|
||||||
std::string decoded = DecodeBase64(node->get_first_child_text()->get_content());
|
std::string decoded = DecodeBase64(node->get_first_child_text()->get_content());
|
||||||
auto params = std::views::split(decoded, ',') //
|
auto params = std::views::split(decoded, ',') | std::views::transform([](auto param) {
|
||||||
| std::views::transform([](auto param) { //
|
return std::string_view{param};
|
||||||
return std::string_view{param}; //
|
}) |
|
||||||
}) //
|
std::views::transform([](std::string_view param) -> std::pair<std::string_view, std::string_view> {
|
||||||
| std::views::transform([](std::string_view param) -> std::pair<std::string_view, std::string_view> { //
|
auto v = param.find("=");
|
||||||
auto v = param.find("="); //
|
return {param.substr(0, v), param.substr(v + 1)};
|
||||||
return {param.substr(0, v), param.substr(v + 1)}; //
|
}) |
|
||||||
}) //
|
std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
|
||||||
| std::ranges::to<std::unordered_map<std::string_view, std::string_view>>();
|
|
||||||
return {.body = std::move(decoded),
|
return {.body = std::move(decoded),
|
||||||
.serverNonce = params.at("r"),
|
.serverNonce = params.at("r"),
|
||||||
.salt = DecodeBase64(params.at("s")),
|
.salt = DecodeBase64(params.at("s")),
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <larra/serialization.hpp>
|
#include <larra/serialization.hpp>
|
||||||
#include <larra/stream_error.hpp>
|
#include <larra/stream_error.hpp>
|
||||||
#include <larra/utils.hpp>
|
#include <larra/utils.hpp>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
@ -11,6 +12,8 @@ namespace iq {
|
||||||
template <auto& Name, typename PayloadType>
|
template <auto& Name, typename PayloadType>
|
||||||
struct BaseImplWithPayload {
|
struct BaseImplWithPayload {
|
||||||
std::string id;
|
std::string id;
|
||||||
|
std::optional<std::string> from{};
|
||||||
|
std::optional<std::string> to{};
|
||||||
PayloadType payload;
|
PayloadType payload;
|
||||||
static const inline std::string kName = Name;
|
static const inline std::string kName = Name;
|
||||||
static constexpr auto kDefaultName = "iq";
|
static constexpr auto kDefaultName = "iq";
|
||||||
|
@ -19,16 +22,32 @@ struct BaseImplWithPayload {
|
||||||
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
|
[[nodiscard]] constexpr auto Id(this Self&& self, std::string id) -> std::decay_t<Self> {
|
||||||
return utils::FieldSetHelper::With<"id", BaseImplWithPayload>(std::forward<Self>(self), std::move(id));
|
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> {
|
||||||
|
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> {
|
||||||
|
return utils::FieldSetHelper::With<"from", BaseImplWithPayload>(std::forward<Self>(self), std::move(from));
|
||||||
|
}
|
||||||
template <typename NewPayloadType, typename Self>
|
template <typename NewPayloadType, typename Self>
|
||||||
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
|
[[nodiscard]] constexpr auto Payload(this Self&& self, NewPayloadType value) {
|
||||||
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
|
return utils::FieldSetHelper::With<"payload", BaseImplWithPayload, false>(std::forward<Self>(self), std::move(value));
|
||||||
}
|
}
|
||||||
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
|
friend constexpr auto operator<<(xmlpp::Element* element, const BaseImplWithPayload& self) {
|
||||||
element->set_attribute("id", self.id);
|
element->set_attribute("id", self.id);
|
||||||
|
|
||||||
|
if (self.to) {
|
||||||
|
element->set_attribute("to", *self.to);
|
||||||
|
}
|
||||||
|
if (self.from) {
|
||||||
|
element->set_attribute("from", *self.from);
|
||||||
|
}
|
||||||
element->set_attribute("type", kName);
|
element->set_attribute("type", kName);
|
||||||
using S = Serialization<PayloadType>;
|
using S = Serialization<PayloadType>;
|
||||||
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
|
S::Serialize(element->add_child_element(S::kDefaultName, S::kPrefix), self.payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> BaseImplWithPayload {
|
||||||
auto node = element->get_attribute("type");
|
auto node = element->get_attribute("type");
|
||||||
if(!node) {
|
if(!node) {
|
||||||
|
@ -41,6 +60,9 @@ struct BaseImplWithPayload {
|
||||||
if(!idNode) {
|
if(!idNode) {
|
||||||
throw std::runtime_error("Not found attribute id for parse Iq");
|
throw std::runtime_error("Not found attribute id for parse Iq");
|
||||||
}
|
}
|
||||||
|
auto from = element->get_attribute("from");
|
||||||
|
auto to = element->get_attribute("to");
|
||||||
|
|
||||||
|
|
||||||
using S = Serialization<PayloadType>;
|
using S = Serialization<PayloadType>;
|
||||||
auto payload = element->get_first_child(S::kDefaultName);
|
auto payload = element->get_first_child(S::kDefaultName);
|
||||||
|
@ -51,7 +73,11 @@ struct BaseImplWithPayload {
|
||||||
if(!payload2) {
|
if(!payload2) {
|
||||||
throw std::runtime_error("Invalid payload for parse Iq");
|
throw std::runtime_error("Invalid payload for parse Iq");
|
||||||
}
|
}
|
||||||
return {.id = idNode->get_value(), .payload = S::Parse(payload2)};
|
return {
|
||||||
|
.id = idNode->get_value(),
|
||||||
|
.from = (from ? std::optional{from->get_value()} : std::nullopt),
|
||||||
|
.to = (to ? std::optional{to->get_value()} : std::nullopt),
|
||||||
|
.payload = S::Parse(payload2)};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
static constexpr auto kGetName = "get";
|
static constexpr auto kGetName = "get";
|
||||||
|
|
|
@ -7,24 +7,6 @@
|
||||||
|
|
||||||
namespace larra::xmpp {
|
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 {
|
struct BareResourceJid {
|
||||||
std::string server;
|
std::string server;
|
||||||
std::string resource;
|
std::string resource;
|
||||||
|
@ -69,6 +51,27 @@ struct FullJid {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BareJid {
|
||||||
|
std::string username;
|
||||||
|
std::string server;
|
||||||
|
constexpr operator FullJid() const {
|
||||||
|
return FullJid{.username = username, .server = server, .resource = ""};
|
||||||
|
}
|
||||||
|
[[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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
|
using JidVariant = std::variant<BareJid, BareResourceJid, FullJid>;
|
||||||
|
|
||||||
struct Jid : JidVariant {
|
struct Jid : JidVariant {
|
||||||
|
|
|
@ -35,6 +35,8 @@ template <typename Socket>
|
||||||
struct PrintStream : Socket {
|
struct PrintStream : Socket {
|
||||||
using Socket::Socket;
|
using Socket::Socket;
|
||||||
PrintStream(PrintStream&&) = default;
|
PrintStream(PrintStream&&) = default;
|
||||||
|
constexpr PrintStream(Socket&& sock) : Socket(std::move(sock)) {
|
||||||
|
}
|
||||||
using Executor = Socket::executor_type;
|
using Executor = Socket::executor_type;
|
||||||
template <typename ConstBufferSequence,
|
template <typename ConstBufferSequence,
|
||||||
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, std::size_t))
|
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, std::size_t))
|
||||||
|
|
76
library/include/larra/roster.hpp
Normal file
76
library/include/larra/roster.hpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#pragma once
|
||||||
|
#include <libxml++/libxml++.h>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <larra/iq.hpp>
|
||||||
|
#include <larra/jid.hpp>
|
||||||
|
#include <larra/utils.hpp>
|
||||||
|
#include <ranges>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace larra::xmpp::iq {
|
||||||
|
|
||||||
|
struct Roster {
|
||||||
|
static constexpr auto kDefaultName = "query";
|
||||||
|
static constexpr auto kDefaultNamespace = "jabber:iq:roster";
|
||||||
|
|
||||||
|
std::vector<BareJid> items;
|
||||||
|
|
||||||
|
friend auto ToString(const Roster& roster) -> std::string {
|
||||||
|
static constexpr std::string_view prefix = "Roster: [\n\t";
|
||||||
|
static constexpr std::string_view suffix = "]";
|
||||||
|
// \n\r\t
|
||||||
|
std::size_t total_length = std::ranges::fold_left(roster.items | std::views::transform([](const auto& el) {
|
||||||
|
return larra::xmpp::utils::AccumulateFieldLength(el) + 3;
|
||||||
|
}),
|
||||||
|
prefix.length() + suffix.length(),
|
||||||
|
std::plus<>{});
|
||||||
|
|
||||||
|
std::string s;
|
||||||
|
s.resize(total_length);
|
||||||
|
s = prefix;
|
||||||
|
for(const auto& el : roster.items) {
|
||||||
|
s += ToString(el);
|
||||||
|
s += "\n\t";
|
||||||
|
}
|
||||||
|
return s += suffix;
|
||||||
|
}
|
||||||
|
friend constexpr auto operator<<(xmlpp::Element* element, const Roster& roster) {
|
||||||
|
element->set_attribute("xmlns", Roster::kDefaultNamespace);
|
||||||
|
std::ranges::for_each(roster.items, [element](const auto& item) {
|
||||||
|
element->add_child_element("item")->set_attribute("jid", ToString(item));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> Roster {
|
||||||
|
const auto& item_nodes = element->get_children("item");
|
||||||
|
if(item_nodes.empty()) {
|
||||||
|
SPDLOG_DEBUG("No items at Iq::Roster");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {.items = item_nodes | std::views::transform([](const xmlpp::Node* node) {
|
||||||
|
auto item_element = dynamic_cast<const xmlpp::Element*>(node);
|
||||||
|
if(!item_element) {
|
||||||
|
throw std::runtime_error("Can't convert xmlpp::Node to xmlpp::Element");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto jid_ptr = item_element->get_attribute("jid");
|
||||||
|
if(!jid_ptr) {
|
||||||
|
throw std::runtime_error("Not found attribute 'jid' for parse Roster item");
|
||||||
|
}
|
||||||
|
|
||||||
|
return BareJid::Parse(jid_ptr->get_value());
|
||||||
|
}) |
|
||||||
|
std::ranges::to<std::vector<BareJid>>()};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using GetRoster = Get<Roster>;
|
||||||
|
using ResultRoster = Result<Roster>;
|
||||||
|
using IqRoster = Iq<Roster>;
|
||||||
|
|
||||||
|
inline auto MakeGetRoster(const FullJid& jid) {
|
||||||
|
return GetRoster{.id = "1", .from = ToString(jid), .payload = Roster{}};
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace larra::xmpp::iq
|
|
@ -2,7 +2,9 @@
|
||||||
#include <libxml++/libxml++.h>
|
#include <libxml++/libxml++.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <larra/serialization/error.hpp>
|
||||||
#include <nameof.hpp>
|
#include <nameof.hpp>
|
||||||
|
#include <ranges>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utempl/utils.hpp>
|
#include <utempl/utils.hpp>
|
||||||
|
|
||||||
|
@ -96,7 +98,8 @@ template <typename T>
|
||||||
struct Serialization : SerializationBase<T> {
|
struct Serialization : SerializationBase<T> {
|
||||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> T {
|
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> T {
|
||||||
if(!Serialization::StartCheck(element)) {
|
if(!Serialization::StartCheck(element)) {
|
||||||
throw std::runtime_error("StartCheck failed");
|
throw serialization::ParsingError{
|
||||||
|
std::format("[{}: {}] parsing error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||||
}
|
}
|
||||||
return T::Parse(element);
|
return T::Parse(element);
|
||||||
}
|
}
|
||||||
|
@ -167,8 +170,8 @@ struct Serialization<std::variant<Ts...>> : SerializationBase<> {
|
||||||
}
|
}
|
||||||
static constexpr auto Serialize(xmlpp::Element* element, const std::variant<Ts...>& object) -> void {
|
static constexpr auto Serialize(xmlpp::Element* element, const std::variant<Ts...>& object) -> void {
|
||||||
std::visit(
|
std::visit(
|
||||||
[&](const auto& object) {
|
[&]<typename T>(const T& object) {
|
||||||
element << object;
|
Serialization<T>::Serialize(element, object);
|
||||||
},
|
},
|
||||||
object);
|
object);
|
||||||
}
|
}
|
||||||
|
@ -189,4 +192,42 @@ struct Serialization<std::monostate> : SerializationBase<> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Serialization<std::vector<T>> : SerializationBase<> {
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) -> std::vector<T> {
|
||||||
|
return element->get_children(Serialization<T>::kDefaultName) | std::views::transform([](xmlpp::Node* node) {
|
||||||
|
auto itemElement = dynamic_cast<xmlpp::Element*>(node);
|
||||||
|
if(!itemElement) {
|
||||||
|
throw serialization::ParsingError{"Can't convert xmlpp::Node to xmlpp::Element ]"};
|
||||||
|
}
|
||||||
|
return Serialization<T>::Parse(itemElement);
|
||||||
|
}) |
|
||||||
|
std::ranges::to<std::vector<T>>();
|
||||||
|
}
|
||||||
|
static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<std::vector<T>> {
|
||||||
|
auto chd = element->get_children(Serialization<T>::kDefaultName);
|
||||||
|
auto range = chd | std::views::transform([](xmlpp::Node* node) -> std::optional<T> {
|
||||||
|
auto itemElement = dynamic_cast<xmlpp::Element*>(node);
|
||||||
|
if(!itemElement) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return Serialization<T>::TryParse(itemElement);
|
||||||
|
});
|
||||||
|
std::vector<T> response;
|
||||||
|
response.reserve(chd.size());
|
||||||
|
for(auto& value : range) {
|
||||||
|
if(!value) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
response.push_back(std::move(*value));
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* node, const std::vector<T>& value) -> void {
|
||||||
|
for(const T& element : value) {
|
||||||
|
Serialization<T>::Serialize(node->add_child_element(Serialization<T>::kDefaultName), element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace larra::xmpp
|
} // namespace larra::xmpp
|
||||||
|
|
346
library/include/larra/serialization/auto.hpp
Normal file
346
library/include/larra/serialization/auto.hpp
Normal file
|
@ -0,0 +1,346 @@
|
||||||
|
#pragma once
|
||||||
|
#include <libxml++/libxml++.h>
|
||||||
|
|
||||||
|
#include <boost/pfr.hpp>
|
||||||
|
#include <larra/serialization.hpp>
|
||||||
|
#include <larra/serialization/error.hpp>
|
||||||
|
#include <ranges>
|
||||||
|
#include <utempl/constexpr_string.hpp>
|
||||||
|
#include <utempl/tuple.hpp>
|
||||||
|
#include <utempl/utils.hpp>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
|
namespace larra::xmpp::serialization {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Tag {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr auto kSerializationConfig = std::monostate{};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline constexpr auto kDeserializationConfig = kSerializationConfig<T>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
|
||||||
|
requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto TryParse(xmlpp::Element* element, Tag<T> = {}) -> std::optional<T> {
|
||||||
|
return Serialization<std::optional<T>>::Parse(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
||||||
|
requires(!std::same_as<std::decay_t<decltype(kSerializationConfig<T>)>, std::monostate>);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct MetaInfo {
|
||||||
|
static constexpr std::size_t kSize = boost::pfr::tuple_size_v<T>;
|
||||||
|
template <std::size_t I>
|
||||||
|
using TupleElement = boost::pfr::tuple_element_t<I, T>;
|
||||||
|
|
||||||
|
template <std::size_t I>
|
||||||
|
static constexpr std::string_view kFieldName = boost::pfr::get_name<I, T>();
|
||||||
|
|
||||||
|
template <typename Self, std::size_t I>
|
||||||
|
static constexpr auto Get(Self&& self) -> decltype(auto) {
|
||||||
|
return boost::pfr::get<I>(std::forward<Self>(self));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename MainT, std::size_t Element>
|
||||||
|
struct FieldInfo {
|
||||||
|
using Main = MainT;
|
||||||
|
using Info = MetaInfo<Main>;
|
||||||
|
static inline const std::string kName = [] {
|
||||||
|
if constexpr(requires { Info::template TupleElement<Element>::kDefaultName; }) {
|
||||||
|
return Info::template TupleElement<Element>::kDefaultName;
|
||||||
|
} else {
|
||||||
|
return static_cast<std::string>(Info::template kFieldName<Element>);
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Config {};
|
||||||
|
|
||||||
|
// GCC workaround: operator==
|
||||||
|
|
||||||
|
struct AttributeConfig {
|
||||||
|
auto operator==(const AttributeConfig&) const -> bool = default;
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto operator==(const Config<T>&) const -> bool {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
friend constexpr auto operator==(const Config<T>&, const AttributeConfig&) -> bool {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto operator==(const Config<T>&, const Config<T>&) -> bool {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Config<std::optional<T>> {
|
||||||
|
std::optional<T> defaultValue = std::nullopt;
|
||||||
|
bool strict = true;
|
||||||
|
std::optional<Config<T>> main = std::nullopt;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace impl {
|
||||||
|
template <typename T>
|
||||||
|
concept HasParse = requires(xmlpp::Element* e) {
|
||||||
|
{ T::Parse(e) } -> std::same_as<T>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename V>
|
||||||
|
struct Config : V {
|
||||||
|
using V::V;
|
||||||
|
constexpr Config()
|
||||||
|
requires HasParse<T>
|
||||||
|
: V(::larra::xmpp::serialization::Config<T>{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Config()
|
||||||
|
requires(!HasParse<T>)
|
||||||
|
: V(AttributeConfig{}) {
|
||||||
|
}
|
||||||
|
constexpr auto Base() const -> const V& {
|
||||||
|
return static_cast<const V&>(*this);
|
||||||
|
}
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace impl
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct ElementConfig {
|
||||||
|
using type = impl::Config<T, std::variant<AttributeConfig, Config<T>>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, auto& Config, typename Info>
|
||||||
|
struct ElementSerializer {
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) {
|
||||||
|
auto node = element->get_first_child(Info::kName);
|
||||||
|
if(!node) {
|
||||||
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
|
}
|
||||||
|
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||||
|
if(!elementNode) {
|
||||||
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
|
||||||
|
} catch(const std::exception& error) {
|
||||||
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* node, const T& element) {
|
||||||
|
auto created = node->add_child_element(Info::kName);
|
||||||
|
if(!node) {
|
||||||
|
throw ElementSerializaionError(
|
||||||
|
std::format("[{}: {}] serialization error: [ node creation failed ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
::larra::xmpp::serialization::Serialize(created, element);
|
||||||
|
} catch(const std::exception& err) {
|
||||||
|
throw ElementSerializaionError(
|
||||||
|
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), err.what()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, auto& Config, typename Info>
|
||||||
|
struct ElementSerializer<std::optional<T>, Config, Info> {
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) -> std::optional<T> {
|
||||||
|
return [&] {
|
||||||
|
auto node = element->get_first_child(Info::kName);
|
||||||
|
if(!node) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||||
|
if(!elementNode) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
if constexpr(Config.strict) {
|
||||||
|
return ElementSerializer<T, Config.main, Info>::Parse(element);
|
||||||
|
} else {
|
||||||
|
return ElementSerializer<T, Config.main, Info>::TryParse(element);
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
.or_else([] {
|
||||||
|
return Config.defaultValue;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* node, const std::optional<T>& element) {
|
||||||
|
if(element) {
|
||||||
|
ElementSerializer<T, Config.main, Info>::Serialize(node, *element);
|
||||||
|
} else if(Config.defaultValue) {
|
||||||
|
Serialize(node, Config.defaultValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, auto& Config, typename Info>
|
||||||
|
struct ElementSerializer<std::vector<T>, Config, Info> {
|
||||||
|
// TODO(sha512sum): Add Config and main options instead use Serialization<std::vector<T>>
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) {
|
||||||
|
try {
|
||||||
|
return Serialization<std::vector<T>>::Parse(element);
|
||||||
|
} catch(const std::exception& error) {
|
||||||
|
throw ElementParsingError(
|
||||||
|
std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<std::vector<T>>(), error.what()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* node, const std::vector<T>& element) {
|
||||||
|
try {
|
||||||
|
return Serialization<std::vector<T>>::Serialize(node, element);
|
||||||
|
} catch(const std::exception& error) {
|
||||||
|
throw ElementSerializaionError(
|
||||||
|
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<std::vector<T>>(), error.what()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename Info>
|
||||||
|
struct AttributeSerializer {
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) -> T {
|
||||||
|
auto node = element->get_attribute(Info::kName);
|
||||||
|
if(!node) {
|
||||||
|
throw AttributeParsingError(std::format("Attribute [{}: {}] parsing error", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
|
}
|
||||||
|
if constexpr(requires(std::string_view view) { T::Parse(view); }) {
|
||||||
|
return T::Parse(node->get_value());
|
||||||
|
} else {
|
||||||
|
return node->get_value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* element, const T& obj) {
|
||||||
|
if constexpr(requires {
|
||||||
|
{ ToString(obj) } -> std::convertible_to<const std::string&>;
|
||||||
|
}) {
|
||||||
|
element->set_attribute(Info::kName, ToString(obj));
|
||||||
|
} else {
|
||||||
|
element->set_attribute(Info::kName, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename Info>
|
||||||
|
struct AttributeSerializer<std::optional<T>, Info> {
|
||||||
|
static constexpr auto Parse(xmlpp::Element* element) -> std::optional<T> {
|
||||||
|
auto node = element->get_attribute(Info::kName);
|
||||||
|
return node ? std::optional{AttributeSerializer<T, Info>::Parse(element)} : std::nullopt;
|
||||||
|
}
|
||||||
|
static constexpr auto Serialize(xmlpp::Element* element, const std::optional<T>& obj) {
|
||||||
|
if(obj) {
|
||||||
|
AttributeSerializer<T, Info>::Serialize(element, *obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace impl {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
consteval auto FindElement(std::string_view field, utempl::TypeList<T> = {}) {
|
||||||
|
auto fields = boost::pfr::names_as_array<T>();
|
||||||
|
return std::ranges::find(fields, field) - fields.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <auto& Config, typename Info>
|
||||||
|
auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
|
||||||
|
using Type = std::decay_t<decltype(Config)>::type;
|
||||||
|
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
||||||
|
return AttributeSerializer<Type, Info>::Parse(main);
|
||||||
|
} else {
|
||||||
|
return ElementSerializer<Type, Config, Info>::Parse(main);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <auto& Config, typename Info, typename T>
|
||||||
|
auto SerializeField(xmlpp::Element* main, const T& obj) {
|
||||||
|
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
||||||
|
AttributeSerializer<T, Info>::Serialize(main, obj);
|
||||||
|
} else {
|
||||||
|
ElementSerializer<T, Config, Info>::Serialize(main, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace impl
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct SerializationConfig {
|
||||||
|
decltype([] {
|
||||||
|
return [](auto... is) {
|
||||||
|
return utempl::Tuple<typename ElementConfig<boost::pfr::tuple_element_t<*is, T>>::type...>{};
|
||||||
|
} | utempl::kSeq<boost::pfr::tuple_size_v<T>>;
|
||||||
|
}()) tuple{};
|
||||||
|
template <std::size_t I, typename Self> // NOLINTNEXTLINE
|
||||||
|
consteval auto With(this Self&& self, ElementConfig<boost::pfr::tuple_element_t<I, T>>::type config) -> SerializationConfig {
|
||||||
|
auto tuple = std::forward_like<Self>(self.tuple);
|
||||||
|
Get<I>(tuple) = std::move(config);
|
||||||
|
return {std::move(tuple)};
|
||||||
|
}
|
||||||
|
template <utempl::ConstexprString Name, typename Self>
|
||||||
|
constexpr auto With(this Self&& self, ElementConfig<boost::pfr::tuple_element_t<impl::FindElement<T>(Name), T>>::type config)
|
||||||
|
-> SerializationConfig {
|
||||||
|
return std::forward<Self>(self).template With<impl::FindElement<T>(Name)>(std::move(config));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T {
|
||||||
|
return Serialization<T>::Parse(element);
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T
|
||||||
|
requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>)
|
||||||
|
{
|
||||||
|
static constexpr SerializationConfig config = kDeserializationConfig<T>;
|
||||||
|
constexpr auto tuple = utempl::Map(config.tuple, [](auto& ref) {
|
||||||
|
return &ref;
|
||||||
|
});
|
||||||
|
return utempl::Unpack(utempl::PackConstexprWrapper<utempl::Enumerate(tuple)>(), [&](auto... configs) {
|
||||||
|
try {
|
||||||
|
return T{impl::ParseField<*((*configs).second), FieldInfo<T, (*configs).first>>(element)...};
|
||||||
|
} catch(const ParsingError& error) {
|
||||||
|
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void
|
||||||
|
requires(!std::same_as<std::decay_t<decltype(kSerializationConfig<T>)>, std::monostate>)
|
||||||
|
{
|
||||||
|
static constexpr SerializationConfig config = kSerializationConfig<T>;
|
||||||
|
constexpr auto tuple = utempl::Map(config.tuple, [](auto& ref) {
|
||||||
|
return &ref;
|
||||||
|
});
|
||||||
|
|
||||||
|
return utempl::Unpack(utempl::PackConstexprWrapper<utempl::Enumerate(tuple)>(), [&](auto... configs) {
|
||||||
|
try {
|
||||||
|
(impl::SerializeField<*((*configs).second), FieldInfo<T, (*configs).first>>(node, boost::pfr::get<(*configs).first>(element)), ...);
|
||||||
|
} catch(const ParsingError& error) {
|
||||||
|
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto Serialize(xmlpp::Element* node, const T& element) -> void {
|
||||||
|
Serialization<T>::Serialize(node, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace larra::xmpp::serialization
|
30
library/include/larra/serialization/error.hpp
Normal file
30
library/include/larra/serialization/error.hpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace larra::xmpp::serialization {
|
||||||
|
|
||||||
|
struct ParsingError : std::runtime_error {
|
||||||
|
using std::runtime_error::runtime_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AttributeParsingError : ParsingError {
|
||||||
|
using ParsingError::ParsingError;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ElementParsingError : ParsingError {
|
||||||
|
using ParsingError::ParsingError;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SerializationError : std::runtime_error {
|
||||||
|
using std::runtime_error::runtime_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AttributeSerializationError : SerializationError {
|
||||||
|
using SerializationError::SerializationError;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ElementSerializaionError : SerializationError {
|
||||||
|
using SerializationError::SerializationError;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace larra::xmpp::serialization
|
|
@ -295,4 +295,18 @@ struct RangeToWrapper : T {
|
||||||
: T{std::forward<Args>(args)...} {};
|
: T{std::forward<Args>(args)...} {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept LengthCalculatable = requires(const T& obj) {
|
||||||
|
{ obj.length() } -> std::convertible_to<std::size_t>; // Checks if obj has a length() method returning a type convertible to std::size_t
|
||||||
|
} || std::convertible_to<T, std::string>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto AccumulateFieldLength(const T& obj) -> std::size_t {
|
||||||
|
std::size_t total_length = 0;
|
||||||
|
boost::pfr::for_each_field(obj, [&](const LengthCalculatable auto& field) {
|
||||||
|
total_length += field.length(); // Accumulate length of each field
|
||||||
|
});
|
||||||
|
return total_length;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace larra::xmpp::utils
|
} // namespace larra::xmpp::utils
|
||||||
|
|
39
tests/roster.cpp
Normal file
39
tests/roster.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <larra/roster.hpp>
|
||||||
|
|
||||||
|
#include "larra/jid.hpp"
|
||||||
|
|
||||||
|
namespace larra::xmpp {
|
||||||
|
|
||||||
|
TEST(Roster, SerializeAndParse) {
|
||||||
|
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
||||||
|
auto roster = iq::MakeGetRoster(jid);
|
||||||
|
roster.payload.items.emplace_back("u1", "s1");
|
||||||
|
roster.payload.items.emplace_back("u2", "s2");
|
||||||
|
roster.payload.items.emplace_back("u3", "s3");
|
||||||
|
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("iq");
|
||||||
|
node << roster;
|
||||||
|
|
||||||
|
auto parse_res = 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;
|
||||||
|
// std::cerr << " " << "idx: " << idx << "; expect_el: " << expect_el << "; parsed_el: " << parsed_el << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Roster, Print) {
|
||||||
|
FullJid jid{.username = "test", .server = "server", .resource = "res"}; // NOLINT
|
||||||
|
auto roster = iq::MakeGetRoster(jid);
|
||||||
|
roster.payload.items.emplace_back("u1", "s1");
|
||||||
|
roster.payload.items.emplace_back("u2", "s2");
|
||||||
|
roster.payload.items.emplace_back("u3", "s3");
|
||||||
|
|
||||||
|
EXPECT_NO_THROW({ std::cerr << "[ ] Roster payload: " << ToString(roster.payload) << '\n'; });
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace larra::xmpp
|
|
@ -1,8 +1,13 @@
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <larra/jid.hpp>
|
||||||
#include <larra/serialization.hpp>
|
#include <larra/serialization.hpp>
|
||||||
|
#include <larra/serialization/auto.hpp>
|
||||||
|
#include <larra/serialization/error.hpp>
|
||||||
#include <larra/stream_error.hpp>
|
#include <larra/stream_error.hpp>
|
||||||
|
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
namespace larra::xmpp {
|
namespace larra::xmpp {
|
||||||
|
|
||||||
TEST(Parse, Variant) {
|
TEST(Parse, Variant) {
|
||||||
|
@ -21,8 +26,188 @@ TEST(Serialize, Variant) {
|
||||||
auto node = doc.create_root_node("stream:error");
|
auto node = doc.create_root_node("stream:error");
|
||||||
S::Serialize(node, data);
|
S::Serialize(node, data);
|
||||||
EXPECT_EQ(doc.write_to_string(),
|
EXPECT_EQ(doc.write_to_string(),
|
||||||
std::string_view{"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:error><unsupported-stanza-type "
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stream:error><unsupported-stanza-type "
|
||||||
"xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/></stream:error>\n"});
|
"xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/></stream:error>\n"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace tests::serialization {
|
||||||
|
|
||||||
|
struct SomeStruct {
|
||||||
|
static constexpr auto kDefaultName = "some";
|
||||||
|
std::string value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct;
|
||||||
|
friend auto operator<<(xmlpp::Element* element, const SomeStruct& self);
|
||||||
|
constexpr auto operator==(const SomeStruct&) const -> bool = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct2 {
|
||||||
|
static constexpr auto kDefaultName = "some2";
|
||||||
|
SomeStruct value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2;
|
||||||
|
friend auto operator<<(xmlpp::Element* element, const SomeStruct2& self);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct3 {
|
||||||
|
static constexpr auto kDefaultName = "some3";
|
||||||
|
int value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct3;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct4 {
|
||||||
|
static constexpr auto kDefaultName = "some4";
|
||||||
|
SomeStruct3 value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct4;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct5 {
|
||||||
|
static constexpr auto kDefaultName = "some5";
|
||||||
|
BareJid value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct5;
|
||||||
|
friend auto operator<<(xmlpp::Element* element, const SomeStruct5& self);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct6 {
|
||||||
|
static constexpr auto kDefaultName = "some6";
|
||||||
|
std::vector<SomeStruct> some;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct6;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SomeStruct7 {
|
||||||
|
static constexpr auto kDefaultName = "some7";
|
||||||
|
std::optional<FullJid> value;
|
||||||
|
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct7;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace tests::serialization
|
||||||
|
|
||||||
|
namespace serialization {
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct> = SerializationConfig<tests::serialization::SomeStruct>{};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct2> = SerializationConfig<tests::serialization::SomeStruct2>{};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct4> = SerializationConfig<tests::serialization::SomeStruct4>{};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = SerializationConfig<tests::serialization::SomeStruct5>{};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct6> =
|
||||||
|
SerializationConfig<tests::serialization::SomeStruct6>{}.With<"some">({Config<std::vector<tests::serialization::SomeStruct>>{}});
|
||||||
|
|
||||||
|
template <>
|
||||||
|
constexpr auto kSerializationConfig<tests::serialization::SomeStruct7> = SerializationConfig<tests::serialization::SomeStruct7>{};
|
||||||
|
|
||||||
|
} // namespace serialization
|
||||||
|
|
||||||
|
namespace tests::serialization {
|
||||||
|
|
||||||
|
auto SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 {
|
||||||
|
return {.value = 42}; // NOLINT
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct5::Parse(xmlpp::Element* element) -> SomeStruct5 {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct5>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct6::Parse(xmlpp::Element* element) -> SomeStruct6 {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct6>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto SomeStruct7::Parse(xmlpp::Element* element) -> SomeStruct7 {
|
||||||
|
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct7>(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator<<(xmlpp::Element* element, const SomeStruct& self) {
|
||||||
|
::larra::xmpp::serialization::Serialize(element, self);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator<<(xmlpp::Element* element, const SomeStruct2& self) {
|
||||||
|
::larra::xmpp::serialization::Serialize(element, self);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator<<(xmlpp::Element* element, const SomeStruct5& self) {
|
||||||
|
::larra::xmpp::serialization::Serialize(element, self);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace tests::serialization
|
||||||
|
|
||||||
|
TEST(AutoParse, Basic) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some2");
|
||||||
|
node = node->add_child_element("some");
|
||||||
|
node->set_attribute("value", "Hello");
|
||||||
|
auto a = Serialization<tests::serialization::SomeStruct>::Parse(node);
|
||||||
|
EXPECT_EQ(a.value, "Hello"sv);
|
||||||
|
auto b = Serialization<tests::serialization::SomeStruct2>::Parse(doc.get_root_node());
|
||||||
|
EXPECT_EQ(b.value.value, "Hello"sv);
|
||||||
|
EXPECT_THROW(std::ignore = tests::serialization::SomeStruct2::Parse(node), serialization::ParsingError);
|
||||||
|
auto node2 = node->add_child_element("some4");
|
||||||
|
node2->add_child_element("some3");
|
||||||
|
auto c = Serialization<tests::serialization::SomeStruct4>::Parse(node2);
|
||||||
|
EXPECT_EQ(c.value.value, 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AutoParse, Attribute) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some5");
|
||||||
|
node->set_attribute("value", "user@server.i2p");
|
||||||
|
auto a = Serialization<tests::serialization::SomeStruct5>::Parse(node);
|
||||||
|
EXPECT_EQ(a.value.server, "server.i2p"sv);
|
||||||
|
EXPECT_EQ(a.value.username, "user"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AutoParse, Vector) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some6");
|
||||||
|
for(auto i : std::views::iota(0, 10)) {
|
||||||
|
auto child = node->add_child_element("some");
|
||||||
|
child->set_attribute("value", std::format("Hello {}", i));
|
||||||
|
}
|
||||||
|
auto value = Serialization<tests::serialization::SomeStruct6>::Parse(node);
|
||||||
|
EXPECT_EQ(value.some, std::views::iota(0, 10) | std::views::transform([](auto i) -> tests::serialization::SomeStruct {
|
||||||
|
return {.value = std::format("Hello {}", i)};
|
||||||
|
}) | std::ranges::to<std::vector<tests::serialization::SomeStruct>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AutoParse, Optional) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some7");
|
||||||
|
auto value = Serialization<tests::serialization::SomeStruct7>::Parse(node).value;
|
||||||
|
EXPECT_EQ(value, std::nullopt);
|
||||||
|
node->set_attribute("value", "user@server.i2p/resource");
|
||||||
|
auto value2 = Serialization<tests::serialization::SomeStruct7>::Parse(node).value;
|
||||||
|
FullJid expectData{.username = "user", .server = "server.i2p", .resource = "resource"};
|
||||||
|
EXPECT_EQ(value2, expectData);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AutoSerialize, Basic) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some2");
|
||||||
|
node << tests::serialization::SomeStruct2{.value = {.value = "testData"}};
|
||||||
|
EXPECT_EQ(doc.write_to_string(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<some2><some value=\"testData\"/></some2>\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(AutoSerialize, Attribute) {
|
||||||
|
xmlpp::Document doc;
|
||||||
|
auto node = doc.create_root_node("some5");
|
||||||
|
node << tests::serialization::SomeStruct5{.value = {.username = "user", .server = "server.i2p"}};
|
||||||
|
EXPECT_EQ(doc.write_to_string(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<some5 value=\"user@server.i2p\"/>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace larra::xmpp
|
} // namespace larra::xmpp
|
||||||
|
|
Loading…
Reference in a new issue