use std::fs;
use crate::{
load_document, mcp_tool_name_registry, provenance, ENVELOPE_SCHEMA_VERSION, EXIT_OK,
EXIT_RUNTIME, EXIT_USAGE,
};
pub(crate) fn explore_document(args: &[String]) -> i32 {
use rhwp::document_core::queries::chart_extract::collect_charts;
use rhwp::document_core::queries::explore::{build_menu, DocFacts, HONESTY_NOTE};
use rhwp::document_core::queries::hidden_text::HiddenTextOptions;
use rhwp::document_core::queries::injection_scan as scan;
use rhwp::document_core::queries::structure::{build_structure, StructureMode};
use rhwp::document_core::queries::table_extract::extract_tables;
let mut json_mode = false;
let mut file_path: Option<&str> = None;
for arg in args {
match arg.as_str() {
"--json" => json_mode = true,
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if file_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다: {other}");
return EXIT_USAGE;
}
}
}
}
let Some(file_path) = file_path else {
eprintln!("사용법: rhwp explore <파일.hwp|파일.hwpx|파일.hml> [--json]");
return EXIT_USAGE;
};
let data = match fs::read(file_path) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: 파일을 읽을 수 없습니다 - {file_path}: {e}");
return EXIT_RUNTIME;
}
};
let detected_format = rhwp::parser::detect_format(&data);
let doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let document = doc.document();
let format_label = match detected_format {
rhwp::parser::FileFormat::Hwp => "HWP5",
rhwp::parser::FileFormat::Hwpx => "HWPX",
rhwp::parser::FileFormat::Hwp3 => "HWP3",
rhwp::parser::FileFormat::Hml => "HML",
rhwp::parser::FileFormat::Pdf => "PDF",
rhwp::parser::FileFormat::DrmProtected => "DRM",
rhwp::parser::FileFormat::Empty => "빈 파일",
rhwp::parser::FileFormat::Unknown => "알 수 없음",
};
let tables = extract_tables(document);
let merged_table_count = tables
.iter()
.filter(|g| g.cells.iter().any(|c| c.row_span > 1 || c.col_span > 1))
.count();
let structure = build_structure(document, StructureMode::Auto);
let notes = rhwp::document_core::queries::explain::count_notes(document);
let injection_options = scan::InjectionScanOptions {
min_confidence: scan::Confidence::Low,
include_fields: true,
tool_names: mcp_tool_name_registry(),
};
let injection_signal_count = doc.scan_injection(&injection_options).len();
let hidden = doc.detect_hidden_text(&HiddenTextOptions::default());
let facts = DocFacts {
format_label: format_label.to_string(),
page_count: doc.page_count(),
para_count: document.sections.iter().map(|s| s.paragraphs.len()).sum(),
table_count: tables.len(),
merged_table_count,
field_count: doc.collect_all_fields().len(),
chart_count: collect_charts(document).len(),
structure_node_count: structure.node_count,
footnote_count: notes.footnote_count,
endnote_count: notes.endnote_count,
injection_signal_count,
hidden_text_count: hidden.hidden_text.len(),
encrypted: document.header.encrypted,
};
let menu = build_menu(&facts);
if json_mode {
let menu_json: Vec<serde_json::Value> = menu
.iter()
.map(|a| {
serde_json::json!({
"affordance": a.affordance,
"why": a.why,
"command": a.command,
"skill": a.skill,
"confidence": a.confidence,
})
})
.collect();
let envelope = provenance::marked(
serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"format": format_label,
"pageCount": facts.page_count,
"encrypted": facts.encrypted,
"affordanceCount": menu.len(),
"menu": menu_json,
"note": HONESTY_NOTE,
}),
"explore",
);
println!("{envelope}");
return EXIT_OK;
}
println!(
"이 문서로 해 볼 수 있는 행동 ({format_label} 형식·{}쪽·어포던스 {}개):",
facts.page_count,
menu.len()
);
for (i, a) in menu.iter().enumerate() {
println!(
" {}. [{}] {} — 스킬 {}",
i + 1,
a.confidence,
a.affordance,
a.skill
);
println!(" 이유: {}", a.why);
println!(" 명령: {}", a.command);
}
println!();
println!("{HONESTY_NOTE}");
EXIT_OK
}