.
Some checks failed
PR Check / on-push-commit-check (push) Has been cancelled

This commit is contained in:
sha512sum 2024-11-08 10:48:33 +00:00
parent 285ee84d09
commit 69ed523a4a
3 changed files with 116 additions and 7 deletions

View file

@ -27,6 +27,13 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T
template <typename T> template <typename T>
constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T; constexpr auto Parse(xmlpp::Element* element, Tag<T> = {}) -> T;
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;
struct AttributeConfig {}; struct AttributeConfig {};
template <typename T> template <typename T>
@ -111,10 +118,23 @@ struct ElementSerializer {
} }
try { try {
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{}); return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
} catch(const ParsingError& error) { } catch(const std::exception& error) {
throw ElementParsingError(std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what())); 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()));
}
}
}; };
namespace impl { namespace impl {
@ -143,6 +163,27 @@ auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
} }
} }
template <auto& Config, typename Info, typename T>
auto SerializeField(xmlpp::Element* main, const T& obj) {
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
auto node = main->set_attribute(Info::kName, [&] -> decltype(auto) {
if constexpr(requires {
{ ToString(obj) } -> std::convertible_to<const std::string&>;
}) {
return ToString(obj);
} else {
return obj;
}
}());
if(!node) {
throw AttributeSerializationError(
std::format("[{}: {}] parsing error: [ node creation failed ]", Info::kName, nameof::nameof_full_type<T>()));
}
} else {
ElementSerializer<T, Config, Info>::Serialize(main, obj);
}
}
} // namespace impl } // namespace impl
template <typename T> template <typename T>
@ -173,7 +214,7 @@ template <typename T>
constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T
requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>) requires(!std::same_as<std::decay_t<decltype(kDeserializationConfig<T>)>, std::monostate>)
{ {
static constexpr SerializationConfig config = kSerializationConfig<T>; static constexpr SerializationConfig config = kDeserializationConfig<T>;
constexpr auto tuple = utempl::Map(config.tuple, [](auto& ref) { constexpr auto tuple = utempl::Map(config.tuple, [](auto& ref) {
return &ref; return &ref;
}); });
@ -186,4 +227,27 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T
}); });
} }
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 } // namespace larra::xmpp::serialization

View file

@ -15,4 +15,16 @@ struct ElementParsingError : ParsingError {
using ParsingError::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 } // namespace larra::xmpp::serialization

View file

@ -36,12 +36,14 @@ struct SomeStruct {
static constexpr auto kDefaultName = "some"; static constexpr auto kDefaultName = "some";
std::string value; std::string value;
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct; [[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct;
friend auto operator<<(xmlpp::Element* element, const SomeStruct& self);
}; };
struct SomeStruct2 { struct SomeStruct2 {
static constexpr auto kDefaultName = "some2"; static constexpr auto kDefaultName = "some2";
SomeStruct value; SomeStruct value;
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2; [[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct2;
friend auto operator<<(xmlpp::Element* element, const SomeStruct2& self);
}; };
struct SomeStruct3 { struct SomeStruct3 {
@ -60,6 +62,7 @@ struct SomeStruct5 {
static constexpr auto kDefaultName = "some5"; static constexpr auto kDefaultName = "some5";
BareJid value; BareJid value;
[[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct5; [[nodiscard]] static auto Parse(xmlpp::Element* element) -> SomeStruct5;
friend auto operator<<(xmlpp::Element* element, const SomeStruct5& self);
}; };
} // namespace tests::serialization } // namespace tests::serialization
@ -80,26 +83,42 @@ constexpr auto kSerializationConfig<tests::serialization::SomeStruct5> = Seriali
} // namespace serialization } // namespace serialization
auto tests::serialization::SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct { namespace tests::serialization {
auto SomeStruct::Parse(xmlpp::Element* element) -> SomeStruct {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element); return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct>(element);
} }
auto tests::serialization::SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 { auto SomeStruct2::Parse(xmlpp::Element* element) -> SomeStruct2 {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element); return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct2>(element);
} }
auto tests::serialization::SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 { auto SomeStruct3::Parse(xmlpp::Element*) -> SomeStruct3 {
return {.value = 42}; // NOLINT return {.value = 42}; // NOLINT
} }
auto tests::serialization::SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 { auto SomeStruct4::Parse(xmlpp::Element* element) -> SomeStruct4 {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element); return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct4>(element);
} }
auto tests::serialization::SomeStruct5::Parse(xmlpp::Element* element) -> SomeStruct5 { auto SomeStruct5::Parse(xmlpp::Element* element) -> SomeStruct5 {
return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct5>(element); return ::larra::xmpp::serialization::Parse<tests::serialization::SomeStruct5>(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) { TEST(AutoParse, Basic) {
xmlpp::Document doc; xmlpp::Document doc;
auto node = doc.create_root_node("some2"); auto node = doc.create_root_node("some2");
@ -125,4 +144,18 @@ TEST(AutoParse, Attribute) {
EXPECT_EQ(a.value.username, "user"sv); EXPECT_EQ(a.value.username, "user"sv);
} }
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