use std::path::PathBuf;
pub(crate) fn run_funify(
input: Option<PathBuf>,
seed: Option<String>,
pretty: bool,
) -> Result<(), String> {
use std::io::Read;
let from_stdin = match input.as_deref() {
None => true,
Some(p) => p.as_os_str() == "-",
};
let raw = if from_stdin {
let mut s = String::new();
std::io::stdin()
.read_to_string(&mut s)
.map_err(|e| format!("read stdin: {e}"))?;
s
} else {
let p = input.as_deref().unwrap();
std::fs::read_to_string(p).map_err(|e| format!("read {}: {e}", p.display()))?
};
let value: serde_json::Value =
serde_json::from_str(&raw).map_err(|e| format!("parse JSON input: {e}"))?;
let funifier = match seed {
Some(s) => ktstr::fun::Funifier::with_seed(&s),
None => ktstr::fun::Funifier::ephemeral(),
};
let funified = ktstr::fun::funify_json(value, &funifier);
let out = if pretty {
serde_json::to_string_pretty(&funified)
} else {
serde_json::to_string(&funified)
}
.map_err(|e| format!("serialize funified JSON: {e}"))?;
println!("{out}");
Ok(())
}