Book metadata extraction previously handled author parsing naively by taking only the first raw dc:creator string, ignoring MARC relator roles, multi-creator ordering, deduplication, and structured author fallbacks. Define the formal author attribution model in specs/book-author-parsing.allium and propagate the specification into a comprehensive test suite (AuthorAttributionTest) covering all 23 obligations across value equality, lifecycle state transitions, contract invariants, and surface exposure. Implement AuthorAttribution, AuthorAttributionService, and BookMetadataSurface to fulfill: - Dublin Core precedence over structured author elements - MARC role code resolution (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized) - Grouping creators by resolved role in order of appearance with deduplication - Fallback formatting for structured author fields (first, middle, last, nickname) - Complete status lifecycle enforcement (unparsed to parsed) Register AuthorAttributionTest in CMakeLists.txt and update AGENTS.md documentation.
128 lines
3.5 KiB
C++
128 lines
3.5 KiB
C++
#ifndef AUTHORATTRIBUTION_H
|
|
#define AUTHORATTRIBUTION_H
|
|
|
|
#include <QList>
|
|
#include <QString>
|
|
#include <optional>
|
|
|
|
struct StructuredAuthor {
|
|
std::optional<QString> firstName;
|
|
std::optional<QString> middleName;
|
|
std::optional<QString> lastName;
|
|
std::optional<QString> nickname;
|
|
|
|
bool operator==(const StructuredAuthor &other) const
|
|
{
|
|
return firstName == other.firstName &&
|
|
middleName == other.middleName &&
|
|
lastName == other.lastName &&
|
|
nickname == other.nickname;
|
|
}
|
|
bool operator!=(const StructuredAuthor &other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
};
|
|
|
|
struct CreatorItem {
|
|
std::optional<QString> roleCode;
|
|
QString name;
|
|
|
|
bool operator==(const CreatorItem &other) const
|
|
{
|
|
return roleCode == other.roleCode && name == other.name;
|
|
}
|
|
bool operator!=(const CreatorItem &other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
};
|
|
|
|
enum class MetadataStatus {
|
|
Unparsed,
|
|
Parsed
|
|
};
|
|
|
|
struct BookEntity {
|
|
QString filePath;
|
|
bool isActive = true;
|
|
|
|
bool operator==(const BookEntity &other) const
|
|
{
|
|
return filePath == other.filePath && isActive == other.isActive;
|
|
}
|
|
bool operator!=(const BookEntity &other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
};
|
|
|
|
struct BookMetadata {
|
|
BookEntity book;
|
|
MetadataStatus status = MetadataStatus::Unparsed;
|
|
QList<CreatorItem> rawDcCreators;
|
|
QList<StructuredAuthor> rawStructuredAuthors;
|
|
std::optional<QString> authorAttribution;
|
|
|
|
bool operator==(const BookMetadata &other) const
|
|
{
|
|
return book == other.book &&
|
|
status == other.status &&
|
|
rawDcCreators == other.rawDcCreators &&
|
|
rawStructuredAuthors == other.rawStructuredAuthors &&
|
|
authorAttribution == other.authorAttribution;
|
|
}
|
|
bool operator!=(const BookMetadata &other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
};
|
|
|
|
class AuthorAttributionService {
|
|
public:
|
|
virtual ~AuthorAttributionService() = default;
|
|
virtual QString resolveAttribution(const QString &filePath) = 0;
|
|
};
|
|
|
|
class AuthorAttribution {
|
|
public:
|
|
static QString mapRole(const std::optional<QString> &roleCode);
|
|
static QString formatCreators(const QList<CreatorItem> &creators);
|
|
static QString formatStructuredAuthors(const QList<StructuredAuthor> &authors);
|
|
|
|
static BookMetadata loadMetadata(const BookEntity &book,
|
|
const QList<CreatorItem> &creators,
|
|
const QList<StructuredAuthor> &authors);
|
|
|
|
static bool parseMetadataWithDcCreators(BookMetadata &metadata);
|
|
static bool parseMetadataFallbackToStructuredAuthors(BookMetadata &metadata);
|
|
static bool parseMetadata(BookMetadata &metadata);
|
|
};
|
|
|
|
class BookMetadataSurface : public AuthorAttributionService {
|
|
public:
|
|
explicit BookMetadataSurface(const BookMetadata &metadata, bool engineActive = true);
|
|
|
|
bool isAccessible() const;
|
|
QString filePath() const;
|
|
std::optional<QString> authorAttribution() const;
|
|
|
|
bool canProvideLoadMetadata() const;
|
|
bool canProvideParseMetadata() const;
|
|
|
|
void loadMetadata(const QList<CreatorItem> &creators, const QList<StructuredAuthor> &authors);
|
|
bool parseMetadata();
|
|
|
|
QString resolveAttribution(const QString &filePath) override;
|
|
|
|
const BookMetadata &metadata() const;
|
|
void setMetadata(const BookMetadata &metadata);
|
|
void setEngineActive(bool active);
|
|
|
|
private:
|
|
BookMetadata m_metadata;
|
|
bool m_engineActive;
|
|
};
|
|
|
|
#endif // AUTHORATTRIBUTION_H
|