pub const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
pub const XML: &str = "http://www.w3.org/XML/1998/namespace";
pub const ADOBE_META: &str = "adobe:ns:meta/";
pub const DC: &str = "http://purl.org/dc/elements/1.1/";
pub const XMP: &str = "http://ns.adobe.com/xap/1.0/";
pub const PDF: &str = "http://ns.adobe.com/pdf/1.3/";
pub const XMP_MM: &str = "http://ns.adobe.com/xap/1.0/mm/";
pub const PDFA_ID: &str = "http://www.aiim.org/pdfa/ns/id/";
pub const PDFUA_ID: &str = "http://www.aiim.org/pdfua/ns/id/";
pub const STANDARD_PREFIXES: [(&str, &str); 6] = [
("dc", DC),
("xmp", XMP),
("pdf", PDF),
("xmpMM", XMP_MM),
("pdfaid", PDFA_ID),
("pdfuaid", PDFUA_ID),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum MappedField {
Title,
Authors,
Subject,
Keywords,
Producer,
Creator,
Created,
Modified,
MetadataDate,
DocumentId,
InstanceId,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ValueKind {
LangAlt,
Seq,
Text,
Date,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Mapping {
pub field: MappedField,
pub info_key: Option<&'static str>,
pub namespace: &'static str,
pub prefix: &'static str,
pub name: &'static str,
pub kind: ValueKind,
}
const fn row(
field: MappedField,
info_key: Option<&'static str>,
(prefix, namespace): (&'static str, &'static str),
name: &'static str,
kind: ValueKind,
) -> Mapping {
Mapping {
field,
info_key,
namespace,
prefix,
name,
kind,
}
}
const DC_NS: (&str, &str) = ("dc", DC);
const XMP_NS: (&str, &str) = ("xmp", XMP);
const PDF_NS: (&str, &str) = ("pdf", PDF);
const MM_NS: (&str, &str) = ("xmpMM", XMP_MM);
pub const MAPPINGS: [Mapping; 11] = [
row(MappedField::Title, Some("Title"), DC_NS, "title", ValueKind::LangAlt),
row(MappedField::Authors, Some("Author"), DC_NS, "creator", ValueKind::Seq),
row(
MappedField::Subject,
Some("Subject"),
DC_NS,
"description",
ValueKind::LangAlt,
),
row(
MappedField::Keywords,
Some("Keywords"),
PDF_NS,
"Keywords",
ValueKind::Text,
),
row(
MappedField::Producer,
Some("Producer"),
PDF_NS,
"Producer",
ValueKind::Text,
),
row(
MappedField::Creator,
Some("Creator"),
XMP_NS,
"CreatorTool",
ValueKind::Text,
),
row(
MappedField::Created,
Some("CreationDate"),
XMP_NS,
"CreateDate",
ValueKind::Date,
),
row(
MappedField::Modified,
Some("ModDate"),
XMP_NS,
"ModifyDate",
ValueKind::Date,
),
row(MappedField::MetadataDate, None, XMP_NS, "MetadataDate", ValueKind::Date),
row(MappedField::DocumentId, None, MM_NS, "DocumentID", ValueKind::Text),
row(MappedField::InstanceId, None, MM_NS, "InstanceID", ValueKind::Text),
];
impl MappedField {
#[must_use]
pub const fn mapping(self) -> &'static Mapping {
match self {
Self::Title => &MAPPINGS[0],
Self::Authors => &MAPPINGS[1],
Self::Subject => &MAPPINGS[2],
Self::Keywords => &MAPPINGS[3],
Self::Producer => &MAPPINGS[4],
Self::Creator => &MAPPINGS[5],
Self::Created => &MAPPINGS[6],
Self::Modified => &MAPPINGS[7],
Self::MetadataDate => &MAPPINGS[8],
Self::DocumentId => &MAPPINGS[9],
Self::InstanceId => &MAPPINGS[10],
}
}
#[must_use]
pub fn from_property(namespace: &str, name: &str) -> Option<Self> {
MAPPINGS
.iter()
.find(|m| m.namespace == namespace && m.name == name)
.map(|m| m.field)
}
#[must_use]
pub fn from_info_key(key: &str) -> Option<Self> {
MAPPINGS.iter().find(|m| m.info_key == Some(key)).map(|m| m.field)
}
}
#[cfg(test)]
fn standard_prefix(namespace: &str) -> Option<&'static str> {
STANDARD_PREFIXES
.iter()
.find(|(_, ns)| *ns == namespace)
.map(|(p, _)| *p)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_field_maps_to_its_own_row() {
assert!(MAPPINGS.iter().all(|m| m.field.mapping() == m));
}
#[test]
fn info_keys_find_their_field() {
assert_eq!(
["Title", "Author", "ModDate", "Trapped"].map(MappedField::from_info_key),
[
Some(MappedField::Title),
Some(MappedField::Authors),
Some(MappedField::Modified),
None
]
);
}
#[test]
fn each_row_uses_its_namespaces_standard_prefix() {
assert!(MAPPINGS.iter().all(|m| standard_prefix(m.namespace) == Some(m.prefix)));
}
}