use crate::cli::protocol::*;
mod condition;
mod execution;
mod import;
mod template;
pub(crate) use execution::run_plan_engine;
pub(crate) fn cmd_run_plan(args: &[String]) -> i32 {
let mut plan_path: Option<&str> = None;
let mut plan_inline: Option<&str> = None;
let mut json_mode = false;
let mut dry_run = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--json" => json_mode = true,
"--dry-run" => dry_run = true,
"--plan-json" => {
i += 1;
match args.get(i) {
Some(v) => plan_inline = Some(v.as_str()),
None => {
eprintln!("오류: --plan-json 뒤에 계획 JSON 문자열이 필요합니다.");
return EXIT_USAGE;
}
}
}
other if !other.starts_with("--") && plan_path.is_none() => plan_path = Some(other),
other => {
eprintln!("오류: 알 수 없는 옵션입니다 - {}", other);
return EXIT_USAGE;
}
}
i += 1;
}
let plan_text = match (plan_inline, plan_path) {
(Some(inline), _) => inline.to_string(),
(None, Some(path)) => match fs::read_to_string(path) {
Ok(t) => t,
Err(e) => {
eprintln!("오류: 계획 파일을 읽을 수 없습니다 - {}: {}", path, e);
return EXIT_RUNTIME;
}
},
(None, None) => {
eprintln!("사용법: rhwp run <계획.json> [--json] [--dry-run] (파일 대신 --plan-json '<JSON>')");
return EXIT_USAGE;
}
};
let mut plan: serde_json::Value = match serde_json::from_str(&plan_text) {
Ok(v) => v,
Err(e) => {
eprintln!("오류: 계획 JSON 파싱 실패 - {}", e);
return EXIT_USAGE;
}
};
if dry_run {
if let Some(obj) = plan.as_object_mut() {
obj.insert("dryRun".to_string(), serde_json::Value::Bool(true));
}
}
let (journal, code) = run_plan_engine(&plan);
if json_mode {
println!("{}", journal);
} else if code == EXIT_OK && journal["dryRun"] == true {
let preview_all = journal["preview"].as_array().cloned().unwrap_or_default();
let skipped_count = preview_all.iter().filter(|s| s["skipped"] == true).count();
println!(
"검사 통과: {} step 실행 가능{} (디스크 무변경, 산출 예정 {})",
preview_all.len() - skipped_count,
if skipped_count == 0 {
String::new()
} else {
format!(" · {} step 건너뜀 예정", skipped_count)
},
journal["output"].as_str().unwrap_or("-")
);
for step in &preview_all {
println!(" - {}", preview_line(step));
}
} else if code == EXIT_OK {
let skipped: Vec<&serde_json::Value> = journal["steps"]
.as_array()
.map(|steps| steps.iter().filter(|s| s["skipped"] == true).collect())
.unwrap_or_default();
let total = journal["steps"].as_array().map(|s| s.len()).unwrap_or(0);
println!(
"완료: {} step 적용{}, 산출 {}",
total - skipped.len(),
if skipped.is_empty() {
String::new()
} else {
format!(" · {} step 건너뜀", skipped.len())
},
journal["output"].as_str().unwrap_or("-")
);
for step in &skipped {
println!(
" - step {} 건너뜀: {}",
step["step"].as_u64().unwrap_or(0),
step["reason"].as_str().unwrap_or("")
);
}
if let Some(steps) = journal["steps"].as_array() {
for step in steps {
if let Some(confusable) = step["confusable"].as_array() {
for item in confusable {
eprintln!(
"경고: '{}' 과(와) 화면상 구별되지 않는 이름의 누름틀이 문서에 함께 있습니다 — 채운 칸이 의도한 칸인지 확인하세요.",
item["name"].as_str().unwrap_or("")
);
}
}
}
}
} else {
eprintln!("{}", journal);
}
code
}
fn preview_line(step: &serde_json::Value) -> String {
let idx = step["step"].as_u64().unwrap_or(0);
if step["skipped"] == true {
return format!(
"step {} 건너뜀 예정: {}",
idx,
step["reason"].as_str().unwrap_or("")
);
}
match step["action"].as_str().unwrap_or("") {
"fill_fields" => format!(
"step {}: 누름틀 {}칸 채움",
idx,
step["targets"].as_array().map(|a| a.len()).unwrap_or(0)
),
"replace_text" => format!(
"step {}: '{}' {}건 중 {}건 치환",
idx,
step["find"].as_str().unwrap_or(""),
step["matches"].as_u64().unwrap_or(0),
step["willReplace"].as_u64().unwrap_or(0)
),
"set_checkbox" => format!(
"step {}: 빈 체크박스 {}개 중 {}번째 표시",
idx,
step["available"].as_u64().unwrap_or(0),
step["occurrence"].as_u64().unwrap_or(0)
),
"set_cell" => format!(
"step {}: 표 {} ({},{}) 기록 — 현재값 {:?}",
idx,
step["table"].as_u64().unwrap_or(0),
step["row"].as_u64().unwrap_or(0),
step["col"].as_u64().unwrap_or(0),
step["currentText"].as_str().unwrap_or("")
),
other => format!("step {}: {}", idx, other),
}
}