use serde::Serialize;
use hwpforge_smithy_hwpx::HwpxDecoder;
use crate::output::{read_file_bytes, ToolErrorInfo};
#[derive(Debug, Serialize)]
pub struct SectionDetail {
pub index: usize,
pub paragraphs: usize,
pub tables: usize,
pub images: usize,
pub charts: usize,
pub has_header: bool,
pub has_footer: bool,
pub has_page_number: bool,
}
#[derive(Debug, Serialize)]
pub struct MetadataInfo {
pub title: String,
pub author: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub subject: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modified: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub keywords: Vec<String>,
}
#[derive(Debug, Serialize)]
pub struct InspectData {
pub metadata: MetadataInfo,
pub sections: usize,
pub total_paragraphs: usize,
pub total_tables: usize,
pub total_images: usize,
pub total_charts: usize,
pub section_details: Vec<SectionDetail>,
}
pub fn run_inspect(file_path: &str, _show_styles: bool) -> Result<InspectData, ToolErrorInfo> {
let bytes = read_file_bytes(file_path)?;
let hwpx_doc = HwpxDecoder::decode(&bytes).map_err(|e| {
ToolErrorInfo::new(
"DECODE_ERROR",
format!("HWPX decode failed: {e}"),
"Check that the file is a valid HWPX document.",
)
})?;
let doc = &hwpx_doc.document;
let meta = doc.metadata();
let metadata = MetadataInfo {
title: meta.title.clone().unwrap_or_default(),
author: meta.author.clone().unwrap_or_default(),
subject: meta.subject.clone().unwrap_or_default(),
created: meta.created.clone(),
modified: meta.modified.clone(),
keywords: meta.keywords.clone(),
};
let mut total_tables: usize = 0;
let mut total_images: usize = 0;
let mut total_charts: usize = 0;
let mut total_paragraphs: usize = 0;
let section_details: Vec<SectionDetail> = doc
.sections()
.iter()
.enumerate()
.map(|(i, sec)| {
let counts = sec.content_counts();
total_tables += counts.tables;
total_images += counts.images;
total_charts += counts.charts;
total_paragraphs += sec.paragraphs.len();
SectionDetail {
index: i,
paragraphs: sec.paragraphs.len(),
tables: counts.tables,
images: counts.images,
charts: counts.charts,
has_header: sec.header.is_some(),
has_footer: sec.footer.is_some(),
has_page_number: sec.page_number.is_some(),
}
})
.collect();
Ok(InspectData {
metadata,
sections: section_details.len(),
total_paragraphs,
total_tables,
total_images,
total_charts,
section_details,
})
}