use crate::error::Result;
use crate::formats::{lsf, lsj, lsx};
use std::path::Path;
pub fn convert_lsf_to_lsj<P: AsRef<Path>>(source: P, dest: P) -> Result<()> {
convert_lsf_to_lsj_with_progress(source, dest, &|_| {})
}
pub fn convert_lsf_to_lsj_with_progress<P: AsRef<Path>>(
source: P,
dest: P,
progress: crate::converter::ConvertProgressCallback,
) -> Result<()> {
use crate::converter::{ConvertPhase, ConvertProgress};
tracing::info!(
"Converting LSF→LSJ: {:?} → {:?}",
source.as_ref(),
dest.as_ref()
);
progress(&ConvertProgress::with_file(
ConvertPhase::ReadingSource,
1,
5,
"Reading LSF binary...",
));
let lsf_doc = lsf::read_lsf(&source)?;
let node_count = lsf_doc.nodes.len();
progress(&ConvertProgress::with_file(
ConvertPhase::Converting,
2,
5,
format!("Converting {node_count} nodes to XML..."),
));
let lsx_xml = super::lsf_to_lsx::to_lsx(&lsf_doc)?;
progress(&ConvertProgress::with_file(
ConvertPhase::Parsing,
3,
5,
"Parsing XML structure...",
));
let lsx_doc = lsx::parse_lsx(&lsx_xml)?;
let region_count = lsx_doc.regions.len();
progress(&ConvertProgress::with_file(
ConvertPhase::Converting,
4,
5,
format!("Converting {region_count} regions to JSON..."),
));
let lsj_doc = super::lsx_to_lsj::to_lsj(&lsx_doc)?;
progress(&ConvertProgress::with_file(
ConvertPhase::WritingOutput,
5,
5,
"Writing LSJ file...",
));
lsj::write_lsj(&lsj_doc, dest)?;
tracing::info!("Conversion complete");
Ok(())
}