Better nested key-value html serialization.

This commit is contained in:
John Preston 2018-06-28 18:52:57 +01:00
parent e21c354428
commit 49ea9434f2
1 changed files with 39 additions and 28 deletions

View File

@ -172,6 +172,13 @@ QByteArray SerializeKeyValue(
return result; return result;
} }
QByteArray SerializeBlockquote(
std::vector<std::pair<QByteArray, QByteArray>> &&values) {
return "<blockquote>"
+ SerializeKeyValue(std::move(values))
+ "</blockquote>";
}
Data::Utf8String FormatUsername(const Data::Utf8String &username) { Data::Utf8String FormatUsername(const Data::Utf8String &username) {
return username.isEmpty() ? username : ('@' + username); return username.isEmpty() ? username : ('@' + username);
} }
@ -283,33 +290,37 @@ QByteArray SerializeMessage(
}; };
using SkipReason = Data::File::SkipReason; using SkipReason = Data::File::SkipReason;
const auto pushPath = [&]( const auto formatPath = [&](
const Data::File &file, const Data::File &file,
const QByteArray &label, const QByteArray &label,
const QByteArray &name = QByteArray()) { const QByteArray &name = QByteArray()) {
Expects(!file.relativePath.isEmpty() Expects(!file.relativePath.isEmpty()
|| file.skipReason != SkipReason::None); || file.skipReason != SkipReason::None);
pushBare(label, [&]() -> QByteArray { const auto pre = name.isEmpty()
const auto pre = name.isEmpty() ? QByteArray()
? QByteArray() : SerializeString(name + ' ');
: SerializeString(name + ' '); switch (file.skipReason) {
switch (file.skipReason) { case SkipReason::Unavailable:
case SkipReason::Unavailable: return pre + "(" + label + " unavailable, "
return pre + "(" + label + " unavailable, " "please try again later)";
"please try again later)"; case SkipReason::FileSize:
case SkipReason::FileSize: return pre + "(" + label + " exceeds maximum size. "
return pre + "(" + label + " exceeds maximum size. " "Change data exporting settings to download.)";
"Change data exporting settings to download.)"; case SkipReason::FileType:
case SkipReason::FileType: return pre + "(" + label + " not included. "
return pre + "(" + label + " not included. " "Change data exporting settings to download.)";
"Change data exporting settings to download.)"; case SkipReason::None: return SerializeLink(
case SkipReason::None: return SerializeLink( FormatFilePath(file),
FormatFilePath(file), relativePath(file.relativePath));
relativePath(file.relativePath)); }
} Unexpected("Skip reason while writing file path.");
Unexpected("Skip reason while writing file path."); };
}()); const auto pushPath = [&](
const Data::File &file,
const QByteArray &label,
const QByteArray &name = QByteArray()) {
pushBare(label, formatPath(file, label, name));
}; };
const auto pushPhoto = [&](const Image &image) { const auto pushPhoto = [&](const Image &image) {
pushPath(image.file, "Photo"); pushPath(image.file, "Photo");
@ -486,16 +497,16 @@ QByteArray SerializeMessage(
} }
pushTTL(); pushTTL();
}, [&](const SharedContact &data) { }, [&](const SharedContact &data) {
pushBare("Contact information", SerializeKeyValue({ pushBare("Contact information", SerializeBlockquote({
{ "First name", data.info.firstName }, { "First name", data.info.firstName },
{ "Last name", data.info.lastName }, { "Last name", data.info.lastName },
{ "Phone number", FormatPhoneNumber(data.info.phoneNumber) }, { "Phone number", FormatPhoneNumber(data.info.phoneNumber) },
{ "vCard", (data.vcard.content.isEmpty()
? QByteArray()
: formatPath(data.vcard, "vCard")) }
})); }));
if (!data.vcard.content.isEmpty()) {
pushPath(data.vcard, "Contact vcard");
}
}, [&](const GeoPoint &data) { }, [&](const GeoPoint &data) {
pushBare("Location", data.valid ? SerializeKeyValue({ pushBare("Location", data.valid ? SerializeBlockquote({
{ "Latitude", NumberToString(data.latitude) }, { "Latitude", NumberToString(data.latitude) },
{ "Longitude", NumberToString(data.longitude) }, { "Longitude", NumberToString(data.longitude) },
}) : QByteArray("(empty value)")); }) : QByteArray("(empty value)"));
@ -504,7 +515,7 @@ QByteArray SerializeMessage(
push("Place name", data.title); push("Place name", data.title);
push("Address", data.address); push("Address", data.address);
if (data.point.valid) { if (data.point.valid) {
pushBare("Location", SerializeKeyValue({ pushBare("Location", SerializeBlockquote({
{ "Latitude", NumberToString(data.point.latitude) }, { "Latitude", NumberToString(data.point.latitude) },
{ "Longitude", NumberToString(data.point.longitude) }, { "Longitude", NumberToString(data.point.longitude) },
})); }));
@ -522,7 +533,7 @@ QByteArray SerializeMessage(
} }
} }
}, [&](const Invoice &data) { }, [&](const Invoice &data) {
pushBare("Invoice", SerializeKeyValue({ pushBare("Invoice", SerializeBlockquote({
{ "Title", data.title }, { "Title", data.title },
{ "Description", data.description }, { "Description", data.description },
{ {