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> {
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> T {
|
||||
if(!Serialization::StartCheck(element)) {
|
||||
throw serialization::SerializationError{
|
||||
std::format("[{}: {}] serialization error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||
throw serialization::ParsingError{
|
||||
std::format("[{}: {}] parsing error: [ StartCheck failed ]", Serialization::kDefaultName, nameof::nameof_full_type<T>())};
|
||||
}
|
||||
return T::Parse(element);
|
||||
}
|
||||
|
|
|
@ -103,19 +103,16 @@ struct ElementSerializer {
|
|||
static constexpr auto Parse(xmlpp::Element* element) {
|
||||
auto node = element->get_first_child(Info::kName);
|
||||
if(!node) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
throw ElementParsingError(std::format("[{}: {}] parsing error: [ Not found ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
}
|
||||
auto elementNode = dynamic_cast<xmlpp::Element*>(node);
|
||||
if(!node) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ Invalid node ]", Info::kName, nameof::nameof_full_type<T>()));
|
||||
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 SerializationError& error) {
|
||||
throw ElementSerializationError(
|
||||
std::format("[{}: {}] serialization error: [ {} ]", Info::kName, nameof::nameof_full_type<T>(), error.what()));
|
||||
} catch(const ParsingError& error) {
|
||||
throw ElementParsingError(std::format("[{}: {}] parsing 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())) {
|
||||
xmlpp::Attribute* node = main->get_attribute(Info::kName);
|
||||
if(!node) {
|
||||
throw AttributeSerializationError(
|
||||
std::format("Attribute [{}: {}] serialization error", Info::kName, nameof::nameof_full_type<Type>()));
|
||||
throw AttributeParsingError(std::format("Attribute [{}: {}] parsing error", Info::kName, nameof::nameof_full_type<Type>()));
|
||||
}
|
||||
if constexpr(requires(std::string_view view) { Type::Parse(view); }) {
|
||||
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) {
|
||||
try {
|
||||
return T{impl::ParseField<*((*configs).second), FieldInfo<T, (*configs).first>>(element)...};
|
||||
} catch(const SerializationError& error) {
|
||||
throw ElementSerializationError(std::format("[{}] serialization error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||
} catch(const ParsingError& error) {
|
||||
throw ElementParsingError(std::format("[{}] parsing error: [ {} ]", nameof::nameof_full_type<T>(), error.what()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,16 +3,16 @@
|
|||
|
||||
namespace larra::xmpp::serialization {
|
||||
|
||||
struct SerializationError : std::runtime_error {
|
||||
struct ParsingError : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct AttributeSerializationError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
struct AttributeParsingError : ParsingError {
|
||||
using ParsingError::ParsingError;
|
||||
};
|
||||
|
||||
struct ElementSerializationError : SerializationError {
|
||||
using SerializationError::SerializationError;
|
||||
struct ElementParsingError : ParsingError {
|
||||
using ParsingError::ParsingError;
|
||||
};
|
||||
|
||||
} // namespace larra::xmpp::serialization
|
||||
|
|
|
@ -109,7 +109,7 @@ TEST(AutoParse, Basic) {
|
|||
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::SerializationError);
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue