use rhwp::document_core::DocumentCore;
use serde_json::{json, Value};
use std::io::Write;
const EXIT_OK: i32 = 0;
const EXIT_RUNTIME: i32 = 1;
const EXIT_USAGE: i32 = 2;
pub(super) const USAGE: &str = "rhwp-q objects <파일> [--json]";
const UNTRUSTED: &[&str] = &["source", "controls"];
fn envelope(command: &str, payload: Value, untrusted: &[&str]) -> Value {
let mut out = json!({
"schemaVersion": rhwp::schema_registry::ENVELOPE_SCHEMA_VERSION,
"tool": "rhwp-q-objects",
"command": command,
"version": rhwp::version(),
"untrustedContent": !untrusted.is_empty(),
"untrustedFields": untrusted,
});
if let (Some(dst), Some(src)) = (out.as_object_mut(), payload.as_object()) {
for (key, value) in src {
dst.insert(key.clone(), value.clone());
}
}
out
}
fn write_stdout(text: &str) -> i32 {
let stdout = std::io::stdout();
let mut lock = stdout.lock();
if let Err(e) = writeln!(lock, "{text}") {
eprintln!("오류: stdout 쓰기 실패 - {e}");
return EXIT_RUNTIME;
}
EXIT_OK
}
fn parse_args(args: &[String]) -> Result<(bool, String), i32> {
let mut json = false;
let mut path: Option<String> = None;
for arg in args {
match arg.as_str() {
"--json" => json = true,
other if other.starts_with('-') => {
eprintln!("오류: 알 수 없는 옵션입니다 - {other}");
eprintln!("사용법: {USAGE}");
return Err(EXIT_USAGE);
}
other => {
if path.is_some() {
eprintln!("오류: 파일이 너무 많습니다 - {other}");
eprintln!("사용법: {USAGE}");
return Err(EXIT_USAGE);
}
path = Some(other.to_string());
}
}
}
let Some(path) = path else {
eprintln!("오류: 파일 경로가 필요합니다.");
eprintln!("사용법: {USAGE}");
return Err(EXIT_USAGE);
};
Ok((json, path))
}
fn inspect(path: &str) -> Result<Value, i32> {
let data = std::fs::read(path).map_err(|e| {
eprintln!("오류: 파일을 읽을 수 없습니다 - {path}: {e}");
EXIT_RUNTIME
})?;
let core = DocumentCore::from_bytes(&data).map_err(|e| {
eprintln!("오류: 문서를 열 수 없습니다 - {path}: {e}");
EXIT_RUNTIME
})?;
let controls: Value = serde_json::from_str(&core.controls_json()).map_err(|e| {
eprintln!("오류: 컨트롤 JSON 이 깨졌습니다 - {e}");
EXIT_RUNTIME
})?;
let objects: Value = serde_json::from_str(&core.objects_json()).map_err(|e| {
eprintln!("오류: 개체 JSON 이 깨졌습니다 - {e}");
EXIT_RUNTIME
})?;
Ok(envelope(
"objects",
json!({
"source": path,
"controlCount": controls.as_array().map(Vec::len).unwrap_or(0),
"objectCount": objects.as_array().map(Vec::len).unwrap_or(0),
"controls": controls,
"objects": objects,
}),
UNTRUSTED,
))
}
fn print_text(report: &Value) -> i32 {
let source = report["source"].as_str().unwrap_or("");
let controls = report["controls"].as_array();
let objects = report["objects"].as_array();
let mut lines = Vec::new();
lines.push(source.to_string());
lines.push(format!("controls\t{}", controls.map(Vec::len).unwrap_or(0)));
if let Some(items) = controls {
for (i, item) in items.iter().enumerate() {
lines.push(format!(
"{i}\t{}\t{}\t{}",
item["ctrlId"].as_str().unwrap_or(""),
item["userDesc"].as_str().unwrap_or(""),
item["ctrlCh"]
));
}
}
lines.push(format!("objects\t{}", objects.map(Vec::len).unwrap_or(0)));
if let Some(items) = objects {
for (i, item) in items.iter().enumerate() {
lines.push(format!(
"{i}\t{}\tpara={}\tctrl={}",
item["kind"].as_str().unwrap_or(""),
item["para"],
item["controlIndex"]
));
}
}
write_stdout(&lines.join("\n"))
}
fn run(args: Vec<String>) -> i32 {
if args.iter().any(|arg| arg == "--help" || arg == "-h") {
return write_stdout(USAGE);
}
let (json, path) = match parse_args(&args) {
Ok(v) => v,
Err(code) => return code,
};
let report = match inspect(&path) {
Ok(v) => v,
Err(code) => return code,
};
if json {
match serde_json::to_string_pretty(&report) {
Ok(text) => write_stdout(&text),
Err(e) => {
eprintln!("오류: JSON 직렬화 실패 - {e}");
EXIT_RUNTIME
}
}
} else {
print_text(&report)
}
}
pub(super) fn run_cli(args: Vec<String>) {
std::process::exit(run(args));
}