use crate::cli::protocol::*;
pub(super) fn evaluate_step_condition(
condition: &serde_json::Value,
doc: &rhwp::wasm_api::HwpDocument,
name_counts: &std::collections::HashMap<String, usize>,
name_values: &std::collections::HashMap<String, Vec<String>>,
) -> Result<Option<String>, String> {
let Some(map) = condition.as_object() else {
return Err(
"if 는 { fieldExists | fieldEquals | textFound } 중 하나를 담은 객체여야 합니다"
.to_string(),
);
};
if map.len() != 1 {
return Err(format!(
"if 는 조건을 정확히 하나만 담아야 합니다 (현재 {}개: {}) — 둘 이상은 and/or 가 정의돼 있지 않습니다",
map.len(),
map.keys().cloned().collect::<Vec<_>>().join(", ")
));
}
let (key, value) = map.iter().next().expect("길이 1");
match key.as_str() {
"fieldExists" => {
let Some(spec) = value.as_str().filter(|s| !s.is_empty()) else {
return Err(
"if.fieldExists 는 비어 있지 않은 필드 이름 문자열이어야 합니다".to_string(),
);
};
let (name, occurrence) = parse_field_key(spec);
let total = name_counts.get(name).copied().unwrap_or(0);
if occurrence < total {
Ok(None)
} else {
Ok(Some(format!(
"조건 fieldExists '{}' 불충족 — 문서의 동명 누름틀 {}개",
spec, total
)))
}
}
"fieldEquals" => {
let Some(operand) = value.as_object() else {
return Err(
"if.fieldEquals 는 {\"name\":<필드 이름>, \"value\":<비교값>} 객체여야 합니다"
.to_string(),
);
};
if let Some(unknown) = operand
.keys()
.find(|k| k.as_str() != "name" && k.as_str() != "value")
{
return Err(format!(
"if.fieldEquals 에 알 수 없는 키: {} (name·value 만 받습니다)",
unknown
));
}
let (Some(spec), Some(expected)) = (
operand.get("name").and_then(|v| v.as_str()),
operand.get("value").and_then(|v| v.as_str()),
) else {
return Err("if.fieldEquals 의 name·value 는 둘 다 문자열이어야 합니다".to_string());
};
if spec.is_empty() {
return Err("if.fieldEquals 의 name 이 비어 있습니다".to_string());
}
let (name, occurrence) = parse_field_key(spec);
match name_values.get(name).and_then(|v| v.get(occurrence)) {
Some(actual) if actual == expected => Ok(None),
Some(actual) => Ok(Some(format!(
"조건 fieldEquals '{}' == '{}' 불충족 — 현재값 '{}'",
spec, expected, actual
))),
None => Ok(Some(format!(
"조건 fieldEquals '{}' == '{}' 불충족 — 해당 누름틀이 없습니다",
spec, expected
))),
}
}
"textFound" => {
let Some(needle) = value.as_str().filter(|s| !s.is_empty()) else {
return Err("if.textFound 는 비어 있지 않은 문자열이어야 합니다".to_string());
};
if doc.grep(needle, true, Some(1)).is_empty() {
Ok(Some(format!(
"조건 textFound '{}' 불충족 — 본문에서 찾지 못했습니다",
needle
)))
} else {
Ok(None)
}
}
other => Err(format!(
"알 수 없는 조건: {} (fieldExists·fieldEquals·textFound)",
other
)),
}
}