This commit is contained in:
parent
0dcf9a0ead
commit
285ee84d09
4 changed files with 15 additions and 19 deletions
|
@ -97,8 +97,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 serialization::SerializationError{
|
throw serialization::ParsingError{
|
||||||
std::format("[{}: {}] serialization error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
std::format("[{}: {}] parsing error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||||
}
|
}
|
||||||
return T::Parse(element);
|
return T::Parse(element);
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,19 +103,16 @@ struct ElementSerializer {
|
||||||
static constexpr auto Parse(xmlpp::Element* element) {
|
static constexpr auto Parse(xmlpp::Element* element) {
|
||||||
auto node = element->get_first_child(Info::kName);
|
auto node = element->get_first_child(Info::kName);
|
||||||
if(!node) {
|
if(!node) {
|
||||||
throw ElementSerializationError(
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
std::format("[{}: {}] serialization error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
|
||||||
}
|
}
|
||||||
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||||
if(!node) {
|
if(!node) {
|
||||||
throw ElementSerializationError(
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||||
std::format("[{}: {}] serialization error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
|
return ::larra::xmpp::serialization::Parse(elementNode, Tag<T>{});
|
||||||
} catch(const SerializationError& error) {
|
} catch(const ParsingError& error) {
|
||||||
throw ElementSerializationError(
|
throw ElementParsingError(std::format("[{}: {}] parsing error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
|
||||||
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -134,8 +131,7 @@ auto ParseField(xmlpp::Element* main) -> std::decay_t<decltype(Config)>::type {
|
||||||
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
if constexpr(std::holds_alternative<AttributeConfig>(Config.Base())) {
|
||||||
xmlpp::Attribute* node = main->get_attribute(Info::kName);
|
xmlpp::Attribute* node = main->get_attribute(Info::kName);
|
||||||
if(!node) {
|
if(!node) {
|
||||||
throw AttributeSerializationError(
|
throw AttributeParsingError(std::format("Attribute [{}: {}] parsing error", Info::kName, nameof::nameof_full_type<Type>()));
|
||||||
std::format("Attribute [{}: {}] serialization error", Info::kName, nameof::nameof_full_type<Type>()));
|
|
||||||
}
|
}
|
||||||
if constexpr(requires(std::string_view view) { Type::Parse(view); }) {
|
if constexpr(requires(std::string_view view) { Type::Parse(view); }) {
|
||||||
return Type::Parse(node->get_value());
|
return Type::Parse(node->get_value());
|
||||||
|
@ -184,8 +180,8 @@ constexpr auto Parse(xmlpp::Element* element, Tag<T>) -> T
|
||||||
return utempl::Unpack(utempl::PackConstexprWrapper<utempl::Enumerate(tuple)>(), [&](auto... configs) {
|
return utempl::Unpack(utempl::PackConstexprWrapper<utempl::Enumerate(tuple)>(), [&](auto... configs) {
|
||||||
try {
|
try {
|
||||||
return T{impl::ParseField<*((*configs).second), FieldInfo<T, (*configs).first>>(element)...};
|
return T{impl::ParseField<*((*configs).second), FieldInfo<T, (*configs).first>>(element)...};
|
||||||
} catch(const SerializationError& error) {
|
} catch(const ParsingError& error) {
|
||||||
throw ElementSerializationError(std::format("[{}] serialization error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,16 @@
|
||||||
|
|
||||||
namespace larra::xmpp::serialization {
|
namespace larra::xmpp::serialization {
|
||||||
|
|
||||||
struct SerializationError : std::runtime_error {
|
struct ParsingError : std::runtime_error {
|
||||||
using std::runtime_error::runtime_error;
|
using std::runtime_error::runtime_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AttributeSerializationError : SerializationError {
|
struct AttributeParsingError : ParsingError {
|
||||||
using SerializationError::SerializationError;
|
using ParsingError::ParsingError;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ElementSerializationError : SerializationError {
|
struct ElementParsingError : ParsingError {
|
||||||
using SerializationError::SerializationError;
|
using ParsingError::ParsingError;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace larra::xmpp::serialization
|
} // namespace larra::xmpp::serialization
|
||||||
|
|
|
@ -109,7 +109,7 @@ TEST(AutoParse, Basic) {
|
||||||
EXPECT_EQ(a.value, "Hello"sv);
|
EXPECT_EQ(a.value, "Hello"sv);
|
||||||
auto b = Serialization<tests::serialization::SomeStruct2>::Parse(doc.get_root_node());
|
auto b = Serialization<tests::serialization::SomeStruct2>::Parse(doc.get_root_node());
|
||||||
EXPECT_EQ(b.value.value, "Hello"sv);
|
EXPECT_EQ(b.value.value, "Hello"sv);
|
||||||
EXPECT_THROW(std::ignore = tests::serialization::SomeStruct2::Parse(node), serialization::SerializationError);
|
EXPECT_THROW(std::ignore = tests::serialization::SomeStruct2::Parse(node), serialization::ParsingError);
|
||||||
auto node2 = node->add_child_element("some4");
|
auto node2 = node->add_child_element("some4");
|
||||||
node2->add_child_element("some3");
|
node2->add_child_element("some3");
|
||||||
auto c = Serialization<tests::serialization::SomeStruct4>::Parse(node2);
|
auto c = Serialization<tests::serialization::SomeStruct4>::Parse(node2);
|
||||||
|
|
Loading…
Reference in a new issue