use std::fs;
use rhwp::provenance;
use rhwp::schema_registry::ENVELOPE_SCHEMA_VERSION;
use crate::{
collect_field_records, fields_json_value, load_document, EXIT_OK, EXIT_RUNTIME, EXIT_USAGE,
};
pub(crate) fn word_count(args: &[String]) -> i32 {
let mut file_path: Option<&str> = None;
let mut json_mode = false;
for a in args {
match a.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 word-count <파일.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 doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let mut paragraph_count = 0usize;
let mut char_count = 0usize;
let mut word_count = 0usize;
for section in &doc.document().sections {
paragraph_count += section.paragraphs.len();
for para in §ion.paragraphs {
char_count += para.text.chars().count();
word_count += para.text.split_whitespace().count();
}
}
let section_count = doc.document().sections.len();
let page_count = doc.page_count();
if json_mode {
let envelope = serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"sectionCount": section_count,
"paragraphCount": paragraph_count,
"charCount": char_count,
"wordCount": word_count,
"pageCount": page_count,
});
println!("{}", provenance::marked(envelope, "word-count"));
return EXIT_OK;
}
println!(
"{file_path}: 구역 {section_count} · 문단 {paragraph_count} · 글자 {char_count} · 어절 {word_count} · 쪽 {page_count}"
);
EXIT_OK
}
pub(crate) fn bookmarks(args: &[String]) -> i32 {
let mut file_path: Option<&str> = None;
let mut json_mode = false;
for a in args {
match a.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 bookmarks <파일.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 doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let raw = match doc.get_bookmarks_native() {
Ok(s) => s,
Err(e) => {
eprintln!("오류: 책갈피 조회 실패 - {e}");
return EXIT_RUNTIME;
}
};
let items: serde_json::Value = match serde_json::from_str(&raw) {
Ok(v) => v,
Err(e) => {
eprintln!("오류: 책갈피 JSON 파싱 실패 - {e}");
return EXIT_RUNTIME;
}
};
let count = items.as_array().map(|a| a.len()).unwrap_or(0);
if json_mode {
let envelope = serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"count": count,
"bookmarks": items,
});
println!("{}", provenance::marked(envelope, "bookmarks"));
return EXIT_OK;
}
println!("{file_path}: 책갈피 {count}개");
if let Some(arr) = items.as_array() {
for b in arr {
let name = b["name"].as_str().unwrap_or("");
let sec = b["sec"].as_u64().unwrap_or(0);
let para = b["para"].as_u64().unwrap_or(0);
let ctrl = b["ctrlIdx"].as_u64().unwrap_or(0);
println!(" {name} 구역 {sec} 문단 {para} 컨트롤 {ctrl}");
}
}
EXIT_OK
}
pub(crate) fn charts(args: &[String]) -> i32 {
let mut file_path: Option<&str> = None;
let mut json_mode = false;
for a in args {
match a.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 charts <파일.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 doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let raw = match doc.list_charts_native() {
Ok(s) => s,
Err(e) => {
eprintln!("오류: 차트 조회 실패 - {e}");
return EXIT_RUNTIME;
}
};
let items: serde_json::Value = match serde_json::from_str(&raw) {
Ok(v) => v,
Err(e) => {
eprintln!("오류: 차트 JSON 파싱 실패 - {e}");
return EXIT_RUNTIME;
}
};
let count = items.as_array().map(|a| a.len()).unwrap_or(0);
if json_mode {
let envelope = serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"count": count,
"charts": items,
});
println!("{}", provenance::marked(envelope, "charts"));
return EXIT_OK;
}
println!("{file_path}: 차트 {count}개");
if let Some(arr) = items.as_array() {
for c in arr {
let idx = c["index"].as_u64().unwrap_or(0);
let sec = c["section"].as_u64().unwrap_or(0);
let para = c["paragraph"].as_u64().unwrap_or(0);
let ctrl = c["control"].as_u64().unwrap_or(0);
println!(" {idx} 구역 {sec} 문단 {para} 컨트롤 {ctrl}");
}
}
EXIT_OK
}
pub(crate) fn show_fields(args: &[String]) -> i32 {
let mut file_path: Option<&str> = None;
let mut json_mode = false;
for a in args {
match a.as_str() {
"--json" => json_mode = true,
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => file_path = Some(other),
}
}
let Some(file_path) = file_path else {
eprintln!("사용법: rhwp fields <파일.hwp|파일.hwpx> [--json]");
return EXIT_USAGE;
};
let data = match fs::read(file_path) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: 파일을 읽을 수 없습니다 - {}: {}", file_path, e);
return EXIT_RUNTIME;
}
};
let doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let fields = collect_field_records(&doc);
if json_mode {
let envelope = fields_json_value(file_path, &fields);
println!("{envelope}");
return EXIT_OK;
}
println!("문서 로드: {} (필드 {}개)", file_path, fields.len());
for f in &fields {
let name = f["name"].as_str().unwrap_or("");
let label = if name.is_empty() {
"(이름 없음)"
} else {
name
};
println!(
" [{}] {} = {:?}{}",
f["fieldType"].as_str().unwrap_or("?"),
label,
f["value"].as_str().unwrap_or(""),
if f["editableInForm"] == true {
""
} else {
" (서식 편집 불가)"
}
);
}
EXIT_OK
}