This commit is contained in:
parent
00e885a9e6
commit
b3a4757b2f
2 changed files with 55 additions and 7 deletions
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <larra/serialization/auto.hpp>
|
||||
#include <larra/stanza_error.hpp>
|
||||
#include <larra/xml_language.hpp>
|
||||
#include <string>
|
||||
|
||||
|
@ -123,8 +124,6 @@ struct Type : TypeVariant {
|
|||
}
|
||||
};
|
||||
|
||||
} // namespace message
|
||||
|
||||
template <typename From, typename To>
|
||||
struct Message {
|
||||
static constexpr auto kDefaultName = "message";
|
||||
|
@ -144,12 +143,50 @@ struct Message {
|
|||
};
|
||||
|
||||
template <typename From, typename To>
|
||||
struct serialization::SerializationConfigT<Message<From, To>> {
|
||||
static constexpr auto kValue = serialization::SerializationConfig<Message<From, To>>{} //
|
||||
struct Error {
|
||||
static constexpr auto kDefaultName = "message";
|
||||
static auto Parse(xmlpp::Element* element) -> Error {
|
||||
auto attr = element->get_attribute("type");
|
||||
if(!attr || attr->get_value() != "error") {
|
||||
throw std::runtime_error{"attribute type: message::Type invalid"};
|
||||
}
|
||||
return serialization::Parse<Error>(element);
|
||||
}
|
||||
static auto TryParse(xmlpp::Element* element) -> std::optional<Error> {
|
||||
auto attr = element->get_attribute("type");
|
||||
if(!attr || attr->get_value() != "error") {
|
||||
return std::nullopt;
|
||||
}
|
||||
return serialization::TryParse<Error>(element);
|
||||
}
|
||||
friend auto operator<<(xmlpp::Element* element, const Error& message) -> void {
|
||||
element->set_attribute("type", "error");
|
||||
serialization::Serialize(element, message);
|
||||
}
|
||||
constexpr auto operator==(const Error& other) const -> bool = default;
|
||||
From from;
|
||||
To to;
|
||||
stanza::error::StanzaError error;
|
||||
};
|
||||
|
||||
} // namespace message
|
||||
|
||||
template <typename From, typename To>
|
||||
using Message = std::variant<message::Error<From, To>, message::Message<From, To>>;
|
||||
|
||||
template <typename From, typename To>
|
||||
struct serialization::SerializationConfigT<message::Message<From, To>> {
|
||||
static constexpr auto kValue = serialization::SerializationConfig<message::Message<From, To>>{} //
|
||||
.template With<"type">(serialization::AttributeConfig{})
|
||||
.template With<"body">(serialization::Config<std::vector<message::Body>>{});
|
||||
};
|
||||
|
||||
template <typename From, typename To>
|
||||
struct serialization::SerializationConfigT<message::Error<From, To>> {
|
||||
static constexpr auto kValue = serialization::SerializationConfig<message::Error<From, To>>{} //
|
||||
.template With<"error">(serialization::Config<stanza::error::StanzaError>{});
|
||||
};
|
||||
|
||||
template <typename Info>
|
||||
struct serialization::AttributeSerializer<message::Type, Info> : serialization::AttributeSerializer<message::TypeVariant, Info> {};
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ auto CreateTestData() {
|
|||
return doc;
|
||||
}
|
||||
|
||||
const Message<BareJid, BareJid> kMessage{
|
||||
const message::Message<BareJid, BareJid> kMessage{
|
||||
.from = {.username = "user1", .server = "server.i2p"},
|
||||
.to = {.username = "user2", .server = "server.i2p"},
|
||||
.type = message::type::kChat,
|
||||
|
@ -30,12 +30,16 @@ const Message<BareJid, BareJid> kMessage{
|
|||
.body = {{.content = "hello"}} //
|
||||
};
|
||||
|
||||
const message::Error<BareJid, BareJid> kError{.from = {.username = "user1", .server = "server.i2p"}, //
|
||||
.to = {.username = "user2", .server = "server.i2p"},
|
||||
.error = stanza::error::NotAuthorized{}};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(Parse, Message) {
|
||||
auto doc = CreateTestData();
|
||||
auto node = doc->get_root_node();
|
||||
auto message = Serialization<Message<BareJid, BareJid>>::Parse(node);
|
||||
auto message = Serialization<message::Message<BareJid, BareJid>>::Parse(node);
|
||||
|
||||
EXPECT_EQ(message, kMessage);
|
||||
}
|
||||
|
@ -47,8 +51,15 @@ TEST(Serialize, Message) {
|
|||
)";
|
||||
xmlpp::Document doc;
|
||||
auto node = doc.create_root_node("message");
|
||||
Serialization<Message<BareJid, BareJid>>::Serialize(node, kMessage);
|
||||
Serialization<message::Message<BareJid, BareJid>>::Serialize(node, kMessage);
|
||||
EXPECT_EQ(doc.write_to_string(), expected);
|
||||
doc.~Document();
|
||||
new(&doc) xmlpp::Document{};
|
||||
node = doc.create_root_node("message");
|
||||
Serialization<message::Error<BareJid, BareJid>>::Serialize(node, kError);
|
||||
EXPECT_EQ(doc.write_to_string(), R"(<?xml version="1.0" encoding="UTF-8"?>
|
||||
<message type="error" from="user1@server.i2p" to="user2@server.i2p"><error type="auth"><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></message>
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp
|
||||
|
|
Loading…
Reference in a new issue