use std::fs;
use std::path::Path;
use rhwp::provenance;
use rhwp::schema_registry::ENVELOPE_SCHEMA_VERSION;
use crate::{EXIT_OK, EXIT_RUNTIME, EXIT_USAGE};
#[path = "ingest_media.rs"]
mod ingest_media;
pub(crate) fn build_from_ingest(args: &[String]) -> i32 {
if args.is_empty() {
eprintln!("사용법: rhwp build-from-ingest <ingest.json> [--media-dir <dir>] -o <out.hwpx>");
return EXIT_USAGE;
}
let mut input_path: Option<&str> = None;
let mut output_path: Option<&str> = None;
let mut media_dir: Option<&str> = None;
let mut image_limits = rhwp::document_core::builders::exam_paper::IngestImageLimits::default();
let mut json_mode = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"-o" | "--output" => {
if i + 1 >= args.len() {
eprintln!("오류: -o 옵션에 값이 필요합니다");
return EXIT_USAGE;
}
output_path = Some(&args[i + 1]);
i += 2;
}
"--media-dir" => {
if i + 1 >= args.len() {
eprintln!("오류: --media-dir 옵션에 값이 필요합니다");
return EXIT_USAGE;
}
media_dir = Some(&args[i + 1]);
i += 2;
}
flag @ ("--max-image-bytes"
| "--max-total-image-bytes"
| "--max-image-pixels"
| "--max-total-image-pixels"
| "--max-image-instances") => {
let value = args.get(i + 1).and_then(|v| v.parse::<u64>().ok());
let Some(value) = value.filter(|v| *v > 0 && *v <= usize::MAX as u64) else {
eprintln!("오류: {flag}에는 양의 정수가 필요합니다");
return EXIT_USAGE;
};
match flag {
"--max-image-bytes" => image_limits.max_image_bytes = value as usize,
"--max-total-image-bytes" => {
image_limits.max_total_image_bytes = value as usize
}
"--max-image-pixels" => image_limits.max_image_pixels = value,
"--max-total-image-pixels" => image_limits.max_total_image_pixels = value,
"--max-image-instances" => image_limits.max_image_instances = value as usize,
_ => unreachable!(),
}
i += 2;
}
"--json" => {
json_mode = true;
i += 1;
}
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if input_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다: {other}");
return EXIT_USAGE;
}
i += 1;
}
}
}
let input = match input_path {
Some(p) => p,
None => {
eprintln!("오류: 입력 ingest JSON 경로가 누락되었습니다");
return EXIT_USAGE;
}
};
let output = match output_path {
Some(p) => p,
None => {
eprintln!("오류: -o <출력 경로> 가 누락되었습니다");
return EXIT_USAGE;
}
};
let bytes = match fs::read(input) {
Ok(b) => b,
Err(e) => {
eprintln!("오류: 입력 파일 읽기 실패 - {}: {}", input, e);
return EXIT_RUNTIME;
}
};
let ingest = match rhwp::parser::ingest::parse_ingest_bytes(&bytes) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: ingest JSON 파싱 실패 - {}", e);
return EXIT_RUNTIME;
}
};
let input_media_dir = Path::new(input)
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let input_media_dir = input_media_dir.to_string_lossy();
let images = match ingest_media::load_images(
&ingest,
media_dir.or(Some(input_media_dir.as_ref())),
image_limits,
) {
Ok(images) => images,
Err(e) => {
eprintln!("오류: {e}");
return EXIT_RUNTIME;
}
};
let doc = if images.is_empty() {
rhwp::document_core::builders::exam_paper::build_exam_paper(&ingest)
} else {
match rhwp::document_core::builders::exam_paper::build_exam_paper_with_images(
&ingest,
&images,
image_limits,
) {
Ok(doc) => doc,
Err(e) => {
eprintln!("오류: {e}");
return EXIT_RUNTIME;
}
}
};
let hwpx_bytes = match rhwp::serializer::serialize_hwpx(&doc) {
Ok(b) => b,
Err(e) => {
eprintln!("오류: HWPX 직렬화 실패 - {}", e);
return EXIT_RUNTIME;
}
};
match fs::write(output, &hwpx_bytes) {
Ok(_) => {
let paragraph_count: usize = doc.sections.iter().map(|s| s.paragraphs.len()).sum();
if json_mode {
println!(
"{}",
provenance::marked(
serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": input,
"output": output,
"format": "hwpx",
"bytes": hwpx_bytes.len(),
"questionCount": ingest.questions.len(),
"paragraphCount": paragraph_count,
}),
"build-from-ingest",
)
);
} else {
println!(
"저장 완료: {} ({}바이트, 문제 {}개, 문단 {}개)",
output,
hwpx_bytes.len(),
ingest.questions.len(),
paragraph_count
);
}
EXIT_OK
}
Err(e) => {
eprintln!("오류: 파일 저장 실패 - {}: {}", output, e);
EXIT_RUNTIME
}
}
}
pub(crate) fn run_scaffold(args: &[String]) -> i32 {
if args.is_empty() {
eprintln!("사용법: rhwp scaffold <spec.json> [--format hwpx] -o <out.hwpx> [--json]");
return EXIT_USAGE;
}
let mut input_path: Option<&str> = None;
let mut output_path: Option<&str> = None;
let mut format: &str = "hwpx";
let mut json_mode = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"-o" | "--output" => {
if i + 1 >= args.len() {
eprintln!("오류: -o 옵션에 값이 필요합니다");
return EXIT_USAGE;
}
output_path = Some(&args[i + 1]);
i += 2;
}
"--format" => {
if i + 1 >= args.len() {
eprintln!("오류: --format 옵션에 값이 필요합니다");
return EXIT_USAGE;
}
format = &args[i + 1];
i += 2;
}
"--json" => {
json_mode = true;
i += 1;
}
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if input_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다: {other}");
return EXIT_USAGE;
}
i += 1;
}
}
}
if !format.eq_ignore_ascii_case("hwpx") {
eprintln!("오류: 지원하는 --format 은 hwpx 뿐입니다 (받음: {format})");
return EXIT_USAGE;
}
let input = match input_path {
Some(p) => p,
None => {
eprintln!("오류: 입력 spec JSON 경로가 누락되었습니다");
return EXIT_USAGE;
}
};
let output = match output_path {
Some(p) => p,
None => {
eprintln!("오류: -o <출력 경로> 가 누락되었습니다");
return EXIT_USAGE;
}
};
let bytes = match fs::read(input) {
Ok(b) => b,
Err(e) => {
eprintln!("오류: 입력 파일 읽기 실패 - {}: {}", input, e);
return EXIT_RUNTIME;
}
};
let spec = match rhwp::scaffold::parse_scaffold_bytes(&bytes) {
Ok(s) => s,
Err(e) => {
eprintln!("오류: scaffold JSON 파싱 실패 - {}", e);
return EXIT_RUNTIME;
}
};
let doc = rhwp::scaffold::build_scaffold(&spec);
let hwpx_bytes = match rhwp::serializer::serialize_hwpx(&doc) {
Ok(b) => b,
Err(e) => {
eprintln!("오류: HWPX 직렬화 실패 - {}", e);
return EXIT_RUNTIME;
}
};
match fs::write(output, &hwpx_bytes) {
Ok(_) => {
let paragraph_count: usize = doc.sections.iter().map(|s| s.paragraphs.len()).sum();
let table_count = spec
.blocks
.iter()
.filter(|b| matches!(b, rhwp::scaffold::Block::Table { .. }))
.count();
if json_mode {
println!(
"{}",
provenance::marked(
serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": input,
"output": output,
"format": "hwpx",
"bytes": hwpx_bytes.len(),
"blockCount": spec.blocks.len(),
"paragraphCount": paragraph_count,
"tableCount": table_count,
}),
"scaffold",
)
);
} else {
println!(
"저장 완료: {} ({}바이트, 블록 {}개, 문단 {}개, 표 {}개)",
output,
hwpx_bytes.len(),
spec.blocks.len(),
paragraph_count,
table_count
);
}
EXIT_OK
}
Err(e) => {
eprintln!("오류: 파일 저장 실패 - {}: {}", output, e);
EXIT_RUNTIME
}
}
}