address review: text with no rdf:RDF element is backed up and replaced, not kept as readable

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 14:55:41 +00:00
1 parent 1be0a11ff8
commit fabad167aa
1 file changed
+19 -1
+19 -1
View File
@@ -790,9 +790,12 @@ fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result<
// a readable sidecar whose merge fails (e.g. the merged packet exceeds the XMP limits), or one
// that is valid but over the parser's complexity limits: fail the save and keep the file
// as it is, rather than replace metadata that could be read
// (text with no `rdf:RDF` element parses as empty metadata but holds nothing to keep: it is replaced
// after a backup, as any sidecar that is not XMP)
Err(why)
if why == lightcraft_meta::XmpError::TooComplex
|| lightcraft_meta::parse_xmp(old.strip_prefix('\u{feff}').unwrap_or(old)).is_ok() =>
|| (lightcraft_meta::parse_xmp(old.strip_prefix('\u{feff}').unwrap_or(old)).is_ok()
&& !matches!(&why, lightcraft_meta::XmpError::Xml(m) if m.contains("no rdf:RDF element"))) =>
{
return Err(std::io::Error::other(format!("the sidecar is readable but could not be merged ({why}); it was left unchanged")));
}
@@ -1621,6 +1624,21 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
/// Text that is not XMP at all (it parses as empty metadata, but has no `rdf:RDF` element) is backed up and replaced.
#[test]
fn a_sidecar_with_no_rdf_element_is_backed_up_and_replaced() {
let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-plain-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("a.xmp");
std::fs::write(&path, "not xml at all").unwrap();
let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]);
let (data, merged, backup) = super::sidecar_contents(&path, fresh.clone(), "t").unwrap();
assert!(!merged && data == fresh);
assert!(backup.is_some_and(|b| std::fs::read_to_string(b).unwrap() == "not xml at all"));
std::fs::remove_dir_all(dir).unwrap();
}
/// A sidecar over the parser's namespace-declaration limit cannot be read or merged: the save fails and the file stays.
#[test]
fn a_sidecar_over_the_complexity_limit_is_kept() {