use crate::model::control::Control;
use crate::model::document::Document;
#[derive(Debug, Clone, PartialEq)]
pub struct IrFieldDiff {
pub area: &'static str,
pub location: String,
pub hwpx_value: String,
pub hwp_value: String,
}
#[derive(Debug, Default, Clone)]
pub struct DiffSummary {
pub items: Vec<IrFieldDiff>,
}
impl DiffSummary {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, item: IrFieldDiff) {
self.items.push(item);
}
pub fn counts_by_area(&self) -> Vec<(&'static str, usize)> {
let mut map: std::collections::BTreeMap<&'static str, usize> =
std::collections::BTreeMap::new();
for it in &self.items {
*map.entry(it.area).or_insert(0) += 1;
}
map.into_iter().collect()
}
pub fn human_report(&self) -> String {
use std::fmt::Write;
let mut s = String::new();
let _ = writeln!(s, "[IR diff summary] total={}", self.items.len());
for (area, count) in self.counts_by_area() {
let _ = writeln!(s, " {}: {}", area, count);
}
let _ = writeln!(s, "[details, first 20]");
for item in self.items.iter().take(20) {
let _ = writeln!(
s,
" {} @ {}: hwpx={:?} hwp={:?}",
item.area, item.location, item.hwpx_value, item.hwp_value
);
}
s
}
}
pub fn diff_hwpx_vs_serializer_assumptions(hwpx: &Document) -> DiffSummary {
let mut summary = DiffSummary::new();
for (sec_idx, section) in hwpx.sections.iter().enumerate() {
if section.raw_stream.is_some() {
summary.push(IrFieldDiff {
area: "section.raw_stream",
location: format!("sec={}", sec_idx),
hwpx_value: "Some(...)".into(),
hwp_value: "(직렬화기는 raw_stream 우선 — 빠른 경로)".into(),
});
} else {
}
for (para_idx, para) in section.paragraphs.iter().enumerate() {
check_paragraph(sec_idx, para_idx, para, &mut summary, "");
}
}
summary
}
fn check_paragraph(
sec_idx: usize,
para_idx: usize,
para: &crate::model::paragraph::Paragraph,
summary: &mut DiffSummary,
path_prefix: &str,
) {
for (li, ls) in para.line_segs.iter().enumerate().skip(1) {
if ls.vertical_pos == 0 {
summary.push(IrFieldDiff {
area: "paragraph.line_seg.vertical_pos",
location: format!("{}sec={},para={},ls={}", path_prefix, sec_idx, para_idx, li),
hwpx_value: "0".into(),
hwp_value: "(절대 좌표 — 직렬화기는 그대로 기록)".into(),
});
}
}
for (ctrl_idx, ctrl) in para.controls.iter().enumerate() {
if let Control::Table(t) = ctrl {
if t.raw_ctrl_data.is_empty() {
summary.push(IrFieldDiff {
area: "table.raw_ctrl_data",
location: format!(
"{}sec={},para={},ctrl={}",
path_prefix, sec_idx, para_idx, ctrl_idx
),
hwpx_value: "(empty)".into(),
hwp_value: "(CommonObjAttr 직렬화 필요)".into(),
});
}
if t.raw_table_record_attr == 0
&& (t.attr != 0
|| t.repeat_header
|| !matches!(t.page_break, crate::model::table::TablePageBreak::None))
{
summary.push(IrFieldDiff {
area: "table.raw_table_record_attr",
location: format!(
"{}sec={},para={},ctrl={}",
path_prefix, sec_idx, para_idx, ctrl_idx
),
hwpx_value: "0".into(),
hwp_value: format!(
"(재구성 필요: page_break={:?}, repeat={})",
t.page_break, t.repeat_header
),
});
}
for (cell_idx, cell) in t.cells.iter().enumerate() {
if cell.apply_inner_margin && cell.list_header_width_ref & 0x0001 == 0 {
summary.push(IrFieldDiff {
area: "cell.list_header_width_ref.bit0",
location: format!(
"{}sec={},para={},ctrl={},cell={}",
path_prefix, sec_idx, para_idx, ctrl_idx, cell_idx
),
hwpx_value: "apply_inner_margin=true, width_ref bit0=0".into(),
hwp_value: "width_ref bit0=1 (셀 안 여백 지정)".into(),
});
}
let cell_prefix = format!(
"{}cell[{},{},{},{}]/",
path_prefix, sec_idx, para_idx, ctrl_idx, cell_idx
);
for (cp_idx, cpara) in cell.paragraphs.iter().enumerate() {
check_paragraph(sec_idx, cp_idx, cpara, summary, &cell_prefix);
}
}
}
}
}
pub fn diff_hwpx_vs_hwp(hwpx: &Document, hwp: &Document) -> DiffSummary {
let mut summary = DiffSummary::new();
if hwpx.sections.len() != hwp.sections.len() {
summary.push(IrFieldDiff {
area: "sections.len",
location: "doc".into(),
hwpx_value: format!("{}", hwpx.sections.len()),
hwp_value: format!("{}", hwp.sections.len()),
});
}
let single = diff_hwpx_vs_serializer_assumptions(hwpx);
for it in single.items {
summary.push(it);
}
summary
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_doc_no_diff_items_for_critical_areas() {
let doc = Document::default();
let summary = diff_hwpx_vs_serializer_assumptions(&doc);
let counts = summary.counts_by_area();
assert!(
counts.iter().all(|(a, _)| *a != "table.raw_ctrl_data"
&& *a != "cell.list_header_width_ref.bit0"
&& *a != "paragraph.line_seg.vertical_pos"),
"empty doc should have no critical-area diffs, got: {:?}",
counts
);
}
#[test]
fn human_report_includes_total() {
let summary = DiffSummary::new();
let report = summary.human_report();
assert!(report.contains("total=0"));
}
}