//! BDMV disc-library metadata (`/BDMV/META/DL/bdmt_.xml`). //! //! Every commercial Blu-ray carries a disc-library metadata directory //! with one XML file per shipped language. The schema is the Blu-ray //! "disc library metadata" namespace (`urn:BDA:bdmv;disclibmeta`), //! conventionally prefixed `di:`. Fields commonly present: //! //! - `` or `` — the title string. Vendor practice //! varies (Paramount discs tend to use ``). //! - `` — optional synopsis (often absent on retail //! discs; common on box sets and special editions). //! - `` / `` (or ``) — //! set position for multi-disc releases. //! //! This module is intentionally separate from the BD-J `StreamLabel` //! parsers under `labels/*.rs`. The XML here is disc-level (title, //! description, set position), not per-stream — wiring into the main //! parser registry happens elsewhere. //! //! Real-world XML is irregular: missing description elements, multiple //! title elements (first one wins), and occasional malformed content. //! Extraction is best-effort — a malformed file is treated as "no //! metadata" (returns `None` from the helper), and the caller can //! still get metadata from sibling-language XML files. // The module wiring (registry hook + public re-export) is added // separately. Until then the parse/detect entry points have no use super::xml; use crate::sector::SectorSource; use crate::udf::UdfFs; use std::collections::BTreeMap; /// Disc-level metadata extracted from `/BDMV/META/DL/bdmt_*.xml`. /// /// All maps are keyed by 3-char ISO 639-2 language code (e.g. /// `"eng"`, `"fra"`, `"jpn"`) — the same key segment used in the /// `bdmt_.xml` filename. #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)] pub struct DiscMetadata { /// Localized titles, keyed by 3-char ISO 639-2 lang code /// (e.g. "eng" → "Aurora Drift") pub titles: BTreeMap, /// First-line / short description, per lang pub descriptions: BTreeMap, /// Disc N of M for box sets (None if not a box set) pub disc_number: Option<(u32, u32)>, } /// True if `/BDMV/META/DL/` exists and contains at least one /// `bdmt_*.xml` file. pub fn detect(udf: &UdfFs) -> bool { let Some(dir) = udf.find_dir("/BDMV/META/DL") else { return false; }; dir.entries .iter() .any(|e| !e.is_dir && is_bdmt_filename(&e.name)) } /// Read every `bdmt_.xml` under `/BDMV/META/DL/` and return the /// aggregated [`DiscMetadata`]. Returns `None` if no titles could be /// extracted from any file. pub fn parse(reader: &mut dyn SectorSource, udf: &UdfFs) -> Option { let dir = udf.find_dir("/BDMV/META/DL")?; let mut out = DiscMetadata::default(); for entry in &dir.entries { if entry.is_dir { continue; } let Some(lang) = lang_code_from_filename(&entry.name) else { continue; }; let path = format!("/BDMV/META/DL/{}", entry.name); let Ok(bytes) = udf.read_file(reader, &path) else { continue; }; let Ok(text) = std::str::from_utf8(&bytes) else { continue; }; let Some((title, description, disc_set)) = parse_bdmt_xml(&lang, text) else { continue; }; out.titles.insert(lang.clone(), title); if let Some(desc) = description { out.descriptions.insert(lang.clone(), desc); } // Disc-set position is disc-global; first one we successfully // read wins. (All bdmt_*.xml on a given disc carry the same // value in practice.) if out.disc_number.is_none() { if let Some(ds) = disc_set { out.disc_number = Some(ds); } } } if out.titles.is_empty() { None } else { Some(out) } } /// True if `name` matches the `bdmt_.xml` convention with a /// 3-character ISO 639-2 lang code segment. Case-insensitive. fn is_bdmt_filename(name: &str) -> bool { lang_code_from_filename(name).is_some() } /// Extract the 3-char language code from a `bdmt_.xml` filename. /// Returns `None` if the filename doesn't match. Lang code is /// lowercased so callers always see e.g. `"eng"` not `"ENG"`. fn lang_code_from_filename(name: &str) -> Option { let lower = name.to_ascii_lowercase(); let stem = lower.strip_suffix(".xml")?; let lang = stem.strip_prefix("bdmt_")?; // ISO 639-2 codes are exactly 3 ASCII letters. Be strict — keeps // us from picking up unrelated `bdmt_foo.xml` siblings. if lang.len() != 3 || !lang.chars().all(|c| c.is_ascii_alphabetic()) { return None; } Some(lang.to_string()) } /// Tuple returned by [`parse_bdmt_xml`]: `(title, description?, disc_set?)`. /// Aliased so the function signature isn't a clippy::type-complexity offender. pub(crate) type BdmtFields = (String, Option, Option<(u32, u32)>); /// Parse one `bdmt_.xml` document and return /// `(title, description?, disc_set?)`. Returns `None` if no title /// could be located — the caller treats this as "skip this file". /// /// Title-element preference: `` → `` → /// `/` (first match wins, per the /// authoring-tool conventions documented at the module level). pub(crate) fn parse_bdmt_xml(_lang_code: &str, xml_text: &str) -> Option { let title = extract_title(xml_text)?; let description = xml::text(xml_text, "description") .filter(|s| !s.is_empty()) .filter(|s| !looks_like_xml(s)) .map(|s| s.trim().to_string()) .filter(|s| !s.is_empty()); let disc_set = extract_disc_set(xml_text); Some((title, description, disc_set)) } /// Reject candidate description strings that are themselves XML /// fragments — observed on a captured disc, where /// `` contained `` child /// elements and no actual prose. Surfacing that raw to the JSON /// output is worse than dropping the field entirely. fn looks_like_xml(s: &str) -> bool { let t = s.trim_start(); t.starts_with('<') } /// Try title-bearing element variants in priority order. The `xml` /// helpers are case- and namespace-insensitive, so callers pass the /// bare local name (no `di:` prefix). fn extract_title(xml_text: &str) -> Option { // Order matches the module-level convention: first // (Paramount-style), then , then the nested // tableOfContents/titleName form. for tag in ["name", "title"] { if let Some(s) = xml::text(xml_text, tag) { let trimmed = s.trim(); if !trimmed.is_empty() { return Some(trimmed.to_string()); } } } // tableOfContents/titleName: search inside the toc block so we // don't accidentally pick a stray from elsewhere. if let Some((s, e)) = xml::find_element(xml_text, "tableOfContents", 0) { let block = &xml_text[s..e]; if let Some(t) = xml::text(block, "titleName") { let trimmed = t.trim(); if !trimmed.is_empty() { return Some(trimmed.to_string()); } } } None } /// Extract `(discNumber, numSets)` if both are present and parse as /// `u32`. Accepts either `` or `` for /// the denominator (both forms appear in the wild). fn extract_disc_set(xml_text: &str) -> Option<(u32, u32)> { let n = xml::text(xml_text, "discNumber")? .trim() .parse::() .ok()?; let total = xml::text(xml_text, "numSets") .or_else(|| xml::text(xml_text, "numberOfSets"))? .trim() .parse::() .ok()?; Some((n, total)) } #[cfg(test)] mod tests { use super::*; #[test] fn extract_simple_title() { // Minimal Paramount-style document: as the title // carrier inside a root. let xml = r#" Aurora Drift "#; let (title, desc, set) = parse_bdmt_xml("eng", xml).expect("title should parse"); assert_eq!(title, "Aurora Drift"); assert_eq!(desc, None); assert_eq!(set, None); } #[test] fn extract_title_element_variant() { // is the alternate carrier; should be picked up // when is absent. let xml = r#" Echo Chamber A film about machines. "#; let (title, desc, _) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(title, "Echo Chamber"); assert_eq!(desc.as_deref(), Some("A film about machines.")); } #[test] fn extract_title_from_table_of_contents_fallback() { // Some authoring tools nest the title under tableOfContents. // No or at top level → fall back to // titleName inside tableOfContents. let xml = r#" Feelings Two "#; let (title, _, _) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(title, "Feelings Two"); } #[test] fn extract_box_set_position() { let xml = r#" Box Set Disc 2 2 5 "#; let (_, _, set) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(set, Some((2, 5))); } #[test] fn extract_box_set_position_alternate_total_tag() { // is an alternate spelling we've seen. let xml = r#" X 3 6 "#; let (_, _, set) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(set, Some((3, 6))); } #[test] fn extract_box_set_requires_both_fields() { // discNumber alone (no total) yields None — we don't fabricate // a denominator. let xml = r#" X 1 "#; let (_, _, set) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(set, None); } #[test] fn multiple_languages_keyed_correctly() { // Simulate driving parse_bdmt_xml from two synthetic XML // blobs and aggregating into DiscMetadata the same way parse() // would. This exercises the BTreeMap key handling without // needing a UdfFs. let eng_xml = r#" Aurora Drift "#; let fra_xml = r#" Aurora Drift (Partie Deux) Suite du film fictif. "#; let mut meta = DiscMetadata::default(); for (lang, blob) in [("eng", eng_xml), ("fra", fra_xml)] { let (title, desc, ds) = parse_bdmt_xml(lang, blob).unwrap(); meta.titles.insert(lang.to_string(), title); if let Some(d) = desc { meta.descriptions.insert(lang.to_string(), d); } if meta.disc_number.is_none() { if let Some(d) = ds { meta.disc_number = Some(d); } } } assert_eq!( meta.titles.get("eng").map(String::as_str), Some("Aurora Drift") ); assert_eq!( meta.titles.get("fra").map(String::as_str), Some("Aurora Drift (Partie Deux)") ); assert!(meta.descriptions.get("eng").is_none()); assert_eq!( meta.descriptions.get("fra").map(String::as_str), Some("Suite du film fictif.") ); assert_eq!(meta.disc_number, None); } #[test] fn malformed_xml_returns_none() { // Random gibberish has no recognizable title element. We // document the contract: parse_bdmt_xml returns None, and // parse() (the caller) skips the file. Aggregating across // zero files leaves DiscMetadata::default() — which parse() // surfaces as None to its caller. Either is documented as // acceptable per the module spec. let bad = "this is not xml &&& <<< nope"; assert!(parse_bdmt_xml("eng", bad).is_none()); // Half-open tag, no body, no close: also yields no title. let truncated = ""; assert!(parse_bdmt_xml("eng", truncated).is_none()); } #[test] fn description_with_only_child_xml_is_dropped() { // Real-world bug from a captured disc (2026-05-11 // capture): contained only // child elements with no actual prose. The previous parser // surfaced the raw XML fragment as the description string. // Now we reject candidates that begin with `<`. let xml = r#" Skyline Run "#; let (title, description, _) = parse_bdmt_xml("eng", xml).expect("title is present so parse must succeed"); assert_eq!(title, "Skyline Run"); assert!( description.is_none(), "description containing only XML children must be dropped, got {description:?}" ); } #[test] fn description_with_plain_text_passes_through() { // The legitimate case still works: a description with actual // prose survives the looks_like_xml filter. let xml = r#" Some Movie An epic tale of one man's quest for tea. "#; let (_, description, _) = parse_bdmt_xml("eng", xml).expect("must parse"); assert_eq!( description.as_deref(), Some("An epic tale of one man's quest for tea.") ); } #[test] fn whitespace_in_title_is_trimmed() { let xml = r#" Aurora Drift "#; let (title, _, _) = parse_bdmt_xml("eng", xml).unwrap(); assert_eq!(title, "Aurora Drift"); } #[test] fn lang_code_extraction() { assert_eq!(lang_code_from_filename("bdmt_eng.xml"), Some("eng".into())); assert_eq!(lang_code_from_filename("BDMT_FRA.XML"), Some("fra".into())); assert_eq!(lang_code_from_filename("bdmt_jpn.xml"), Some("jpn".into())); // Non-matching cases: assert_eq!(lang_code_from_filename("bdmt_.xml"), None); assert_eq!(lang_code_from_filename("bdmt_engl.xml"), None); assert_eq!(lang_code_from_filename("bdmt_e1g.xml"), None); assert_eq!(lang_code_from_filename("bdmt_eng.txt"), None); assert_eq!(lang_code_from_filename("foo.xml"), None); } }