Commit e5a80a1e authored by Volker Krause's avatar Volker Krause
Browse files

Refactor GenericExtractor::Result a bit to avoid construction boilerplate

parent eb71aff0
......@@ -24,6 +24,7 @@ set(kitinerary_lib_srcs
datatypes/rentalcar.cpp
datatypes/visit.cpp
generic/genericextractor.cpp
generic/genericpdfextractor.cpp
generic/genericpkpassextractor.cpp
generic/genericuic918extractor.cpp
......
......@@ -478,25 +478,25 @@ void ExtractorEnginePrivate::extractGeneric()
{
if (m_htmlDoc) {
const auto res = StructuredDataExtractor::extract(m_htmlDoc.get());
m_genericResults.emplace_back(GenericExtractor::Result{res, {}, -1});
m_genericResults.emplace_back(GenericExtractor::Result{res});
}
else if (m_pdfDoc) {
m_genericResults = m_genericPdfExtractor.extract(m_pdfDoc.get());
}
else if (m_pass) {
m_genericResults.emplace_back(GenericExtractor::Result{{GenericPkPassExtractor::extract(m_pass.get(), m_context->m_senderDate)}, {}, -1});
m_genericResults.emplace_back(GenericExtractor::Result{{GenericPkPassExtractor::extract(m_pass.get(), m_context->m_senderDate)}});
}
else if (!m_text.isEmpty()) {
if (IataBcbpParser::maybeIataBcbp(m_text)) {
const auto res = IataBcbpParser::parse(m_text, m_context->m_senderDate.date());
m_genericResults.emplace_back(GenericExtractor::Result{JsonLdDocument::toJson(res), m_text, -1});
m_genericResults.emplace_back(GenericExtractor::Result{JsonLdDocument::toJson(res), m_text});
}
}
else if (!m_data.isEmpty()) {
if (Uic9183Parser::maybeUic9183(m_data)) {
QJsonArray res;
GenericUic918Extractor::extract(m_data, res, m_context->m_senderDate);
m_genericResults.emplace_back(GenericExtractor::Result{res, m_data, -1});
m_genericResults.emplace_back(GenericExtractor::Result{res, m_data});
return;
}
// try again as text
......
......@@ -18,3 +18,18 @@
#include "genericextractor_p.h"
using namespace KItinerary;
GenericExtractor::Result::Result() = default;
GenericExtractor::Result::Result(const QJsonArray &result, const QVariant &barcode)
: result(result)
, barcode(barcode)
{
}
GenericExtractor::Result::~Result() = default;
bool KItinerary::GenericExtractor::Result::isEmpty() const
{
return result.isEmpty() && barcode.isNull();
}
......@@ -26,12 +26,24 @@ namespace KItinerary {
/** Shared bits between all generic extractors. */
namespace GenericExtractor
{
/** Generic extraction result. */
struct Result {
QJsonArray result; // JSON-LD data extracted from this document or page
QVariant barcode; // unrecognized barcode for further processing
int pageNum = -1; // page number, if result is from a single PDF page
};
/** Generic extraction result. */
class Result
{
public:
Result();
explicit Result(const QJsonArray &result, const QVariant &barcode = {});
~Result();
/** Checks if there is any relevant result set in here. */
bool isEmpty() const;
public: // TODO
QJsonArray result; // JSON-LD data extracted from this document or page
QVariant barcode; // unrecognized barcode for further processing
int pageNum = -1; // page number, if result is from a single PDF page
};
}
}
......
......@@ -83,7 +83,7 @@ std::vector<GenericExtractor::Result> GenericPdfExtractor::extract(PdfDocument *
}
auto r = extractImage(img, result);
if (!r.barcode.isNull() || !r.result.isEmpty()) {
if (!r.isEmpty()) {
r.pageNum = i;
result.push_back(r);
}
......@@ -122,7 +122,7 @@ GenericExtractor::Result GenericPdfExtractor::extractImage(const PdfImage &img,
QJsonArray result;
GenericUic918Extractor::extract(b, result, m_contextDate);
if (!result.isEmpty()) {
return GenericExtractor::Result{result, b, -1};
return GenericExtractor::Result{result, b};
}
return {};
}
......@@ -130,7 +130,7 @@ GenericExtractor::Result GenericPdfExtractor::extractImage(const PdfImage &img,
if (VdvTicketParser::maybeVdvTicket(b)) {
const auto result = GenericVdvExtractor::extract(b);
if (!result.isEmpty()) {
return GenericExtractor::Result{result, b, -1};
return GenericExtractor::Result{result, b};
}
}
}
......@@ -145,11 +145,11 @@ GenericExtractor::Result GenericPdfExtractor::extractImage(const PdfImage &img,
if (IataBcbpParser::maybeIataBcbp(s)) {
const auto res = IataBcbpParser::parse(s, m_contextDate.date());
const auto jsonLd = JsonLdDocument::toJson(res);
return {jsonLd, s, -1};
return GenericExtractor::Result{jsonLd, s};
}
}
return {{}, s.isEmpty() ? b.isEmpty() ? QVariant() : QVariant(b) : QVariant(s), -1};
return GenericExtractor::Result{{}, s.isEmpty() ? b.isEmpty() ? QVariant() : QVariant(b) : QVariant(s)};
}
bool GenericPdfExtractor::maybeBarcode(const PdfImage &img, BarcodeDecoder::BarcodeTypes hint)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment