use crate::date::PdfDate;
#[derive(Debug, Clone, Copy)]
pub struct XmpReal(f64);
impl XmpReal {
#[must_use]
pub fn new(value: f64) -> Option<Self> {
value.is_finite().then_some(Self(value))
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
}
impl PartialEq for XmpReal {
fn eq(&self, other: &Self) -> bool {
self.0.to_bits() == other.0.to_bits()
}
}
impl Eq for XmpReal {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct LangText {
pub lang: String,
pub text: String,
}
impl LangText {
#[must_use]
pub fn new(lang: impl Into<String>, text: impl Into<String>) -> Self {
Self {
lang: lang.into(),
text: text.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum XmpValue {
Text(String),
Uri(String),
Integer(i64),
Real(XmpReal),
Boolean(bool),
Date(PdfDate),
LangAlt(Vec<LangText>),
Seq(Vec<Self>),
Bag(Vec<Self>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ValueError {
#[error("the array is empty")]
Empty,
#[error("an array holds only simple values")]
Nested,
#[error("an array's items differ in type")]
Mixed,
#[error("a language is empty or repeated")]
Language,
}
impl XmpValue {
#[must_use]
pub const fn is_simple(&self) -> bool {
!matches!(self, Self::LangAlt(_) | Self::Seq(_) | Self::Bag(_))
}
#[must_use]
pub fn value_type(&self) -> String {
match self {
Self::Text(_) => "Text".to_owned(),
Self::Uri(_) => "URI".to_owned(),
Self::Integer(_) => "Integer".to_owned(),
Self::Real(_) => "Real".to_owned(),
Self::Boolean(_) => "Boolean".to_owned(),
Self::Date(_) => "Date".to_owned(),
Self::LangAlt(_) => "Lang Alt".to_owned(),
Self::Seq(items) | Self::Bag(items) => {
let array = if matches!(self, Self::Seq(_)) { "Seq" } else { "Bag" };
format!(
"{array} {}",
items.first().map_or_else(|| "Text".to_owned(), Self::value_type)
)
}
}
}
pub fn check(&self) -> Result<(), ValueError> {
match self {
Self::LangAlt(entries) => {
if entries.is_empty() {
return Err(ValueError::Empty);
}
for (i, e) in entries.iter().enumerate() {
let repeated = entries.iter().take(i).any(|o| o.lang.eq_ignore_ascii_case(&e.lang));
if e.lang.trim().is_empty() || repeated {
return Err(ValueError::Language);
}
}
Ok(())
}
Self::Seq(items) | Self::Bag(items) => {
let first = items.first().ok_or(ValueError::Empty)?;
if !items.iter().all(Self::is_simple) {
return Err(ValueError::Nested);
}
if items
.iter()
.any(|i| std::mem::discriminant(i) != std::mem::discriminant(first))
{
return Err(ValueError::Mixed);
}
Ok(())
}
_ => Ok(()),
}
}
pub(super) fn simple_text(&self) -> Option<String> {
Some(match self {
Self::Text(s) | Self::Uri(s) => s.clone(),
Self::Integer(n) => n.to_string(),
Self::Real(r) => r.get().to_string(),
Self::Boolean(true) => "True".to_owned(),
Self::Boolean(false) => "False".to_owned(),
Self::Date(d) => d.to_iso8601(),
Self::LangAlt(_) | Self::Seq(_) | Self::Bag(_) => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reals_must_be_finite() {
assert!(XmpReal::new(f64::NAN).is_none() && XmpReal::new(f64::INFINITY).is_none());
assert_eq!(XmpReal::new(1.5).map(XmpReal::get), Some(1.5));
}
#[test]
fn arrays_hold_simple_values_of_one_type() {
let text = || XmpValue::Text("a".into());
assert_eq!(XmpValue::Seq(vec![]).check(), Err(ValueError::Empty));
assert_eq!(
XmpValue::Bag(vec![XmpValue::Seq(vec![text()])]).check(),
Err(ValueError::Nested)
);
assert_eq!(
XmpValue::Seq(vec![text(), XmpValue::Integer(1)]).check(),
Err(ValueError::Mixed)
);
assert_eq!(XmpValue::Bag(vec![text(), text()]).check(), Ok(()));
}
#[test]
fn languages_are_given_once() {
let alt = |langs: &[&str]| XmpValue::LangAlt(langs.iter().map(|l| LangText::new(*l, "t")).collect());
assert_eq!(alt(&[]).check(), Err(ValueError::Empty));
assert_eq!(alt(&["en", "EN"]).check(), Err(ValueError::Language));
assert_eq!(alt(&[" "]).check(), Err(ValueError::Language));
assert_eq!(alt(&["x-default", "en"]).check(), Ok(()));
}
#[test]
fn simple_values_are_written_as_xmp_spells_them() {
assert_eq!(XmpValue::Boolean(true).simple_text().as_deref(), Some("True"));
assert_eq!(XmpValue::Integer(-3).simple_text().as_deref(), Some("-3"));
assert_eq!(
XmpValue::Real(XmpReal::new(0.25).unwrap()).simple_text().as_deref(),
Some("0.25")
);
assert_eq!(XmpValue::Seq(vec![]).simple_text(), None);
}
}