use crate::gldf::general_definitions::files::File as GldfFile;
use crate::GldfProduct;
use anyhow::{Context, Result};
use std::collections::HashMap;
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Tm33MergeOutcome {
pub merged: Vec<(String, String)>,
pub files_removed: Vec<String>,
pub spectrums_removed: Vec<String>,
}
impl Tm33MergeOutcome {
pub fn is_noop(&self) -> bool {
self.merged.is_empty()
}
}
pub fn apply_tm33_merge(
product: &mut GldfProduct,
embedded_files: &mut HashMap<String, Vec<u8>>,
) -> Result<Tm33MergeOutcome> {
let mut outcome = Tm33MergeOutcome::default();
let candidates = enumerate_merge_candidates(product);
for cand in &candidates {
let tm33_xml = crate::export::lightsource::export_tm33_xml(
product,
embedded_files,
&cand.light_source_id,
)
.with_context(|| format!("build TM-33 XML for light source {}", cand.light_source_id))?;
let original_phot = product
.general_definitions
.files
.file
.iter()
.find(|f| f.id == cand.phot_file_id)
.cloned();
let tm33_file_name = original_phot
.as_ref()
.map(|f| swap_extension(&f.file_name, "xml"))
.unwrap_or_else(|| format!("{}.xml", cand.light_source_id));
let tm33_file_id = format!("{}_tm33", cand.phot_file_id);
if let Some(phot_block) = product.general_definitions.photometries.as_mut() {
for phot in phot_block.photometry.iter_mut() {
if phot.id == cand.photometry_id {
if let Some(ref_mut) = phot.photometry_file_reference.as_mut() {
ref_mut.file_id = tm33_file_id.clone();
}
}
}
}
if let Some(lss) = product.general_definitions.light_sources.as_mut() {
for fls in lss.fixed_light_source.iter_mut() {
if fls.id == cand.light_source_id {
fls.spectrum_reference = None;
}
}
}
if let Some(spectrums) = product.general_definitions.spectrums.as_mut() {
spectrums.spectrum.retain(|s| s.id != cand.spectrum_id);
}
outcome.spectrums_removed.push(cand.spectrum_id.clone());
product
.general_definitions
.files
.file
.retain(|f| f.id != cand.phot_file_id && f.id != cand.spd_file_id);
embedded_files.remove(&cand.phot_file_id);
embedded_files.remove(&cand.spd_file_id);
outcome.files_removed.push(cand.phot_file_id.clone());
outcome.files_removed.push(cand.spd_file_id.clone());
product.general_definitions.files.file.push(GldfFile {
id: tm33_file_id.clone(),
content_type: "ldc/iesxml".to_string(),
type_attr: "localFileName".to_string(),
language: String::new(),
file_name: tm33_file_name,
});
embedded_files.insert(tm33_file_id.clone(), tm33_xml.into_bytes());
outcome
.merged
.push((cand.light_source_id.clone(), tm33_file_id));
}
if let Some(spectrums) = product.general_definitions.spectrums.as_ref() {
if spectrums.spectrum.is_empty() {
product.general_definitions.spectrums = None;
}
}
Ok(outcome)
}
struct MergeCandidate {
light_source_id: String,
photometry_id: String,
phot_file_id: String,
spectrum_id: String,
spd_file_id: String,
}
fn enumerate_merge_candidates(product: &GldfProduct) -> Vec<MergeCandidate> {
let mut out = Vec::new();
let Some(lss) = product.general_definitions.light_sources.as_ref() else {
return out;
};
for fls in &lss.fixed_light_source {
let Some(spec_ref) = fls.spectrum_reference.as_ref() else {
continue;
};
let Some(phot_id) = find_photometry_for_fixed_ls(product, &fls.id) else {
continue;
};
let Some(phot_file_id) = photometry_file_id(product, &phot_id) else {
continue;
};
let Some(spd_file_id) = spectrum_file_id(product, &spec_ref.spectrum_id) else {
continue;
};
out.push(MergeCandidate {
light_source_id: fls.id.clone(),
photometry_id: phot_id,
phot_file_id,
spectrum_id: spec_ref.spectrum_id.clone(),
spd_file_id,
});
}
out
}
fn find_photometry_for_fixed_ls(product: &GldfProduct, ls_id: &str) -> Option<String> {
let emitters = product.general_definitions.emitters.as_ref()?;
for em in &emitters.emitter {
for fle in &em.fixed_light_emitter {
if fle.light_source_reference.fixed_light_source_id.as_deref() == Some(ls_id) {
return Some(fle.photometry_reference.photometry_id.clone());
}
}
}
None
}
fn photometry_file_id(product: &GldfProduct, phot_id: &str) -> Option<String> {
let p = product.general_definitions.photometries.as_ref()?;
p.photometry
.iter()
.find(|p| p.id == phot_id)
.and_then(|p| p.photometry_file_reference.as_ref())
.map(|r| r.file_id.clone())
}
fn spectrum_file_id(product: &GldfProduct, spec_id: &str) -> Option<String> {
let s = product.general_definitions.spectrums.as_ref()?;
s.spectrum
.iter()
.find(|s| s.id == spec_id)
.and_then(|s| s.spectrum_file_reference.as_ref())
.map(|r| r.file_id.clone())
}
fn swap_extension(file_name: &str, new_ext: &str) -> String {
if let Some(dot) = file_name.rfind('.') {
format!("{}.{}", &file_name[..dot], new_ext)
} else {
format!("{}.{}", file_name, new_ext)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_spectra_is_noop() {
let mut prod = GldfProduct::default();
let mut em = HashMap::new();
let outcome = apply_tm33_merge(&mut prod, &mut em).unwrap();
assert!(outcome.is_noop());
}
#[test]
fn bare_photometry_left_alone() {
use crate::gldf::general_definitions::lightsources::{FixedLightSource, LightSources};
let mut prod = GldfProduct::default();
prod.general_definitions.light_sources = Some(LightSources {
fixed_light_source: vec![FixedLightSource {
id: "ls1".into(),
..FixedLightSource::default()
}],
..LightSources::default()
});
let mut em = HashMap::new();
let outcome = apply_tm33_merge(&mut prod, &mut em).unwrap();
assert!(outcome.is_noop());
}
#[test]
fn swap_extension_basic() {
assert_eq!(swap_extension("foo.ldt", "xml"), "foo.xml");
assert_eq!(swap_extension("a.b.ies", "xml"), "a.b.xml");
assert_eq!(swap_extension("noext", "xml"), "noext.xml");
}
}