use std::io::Read;
use std::path::PathBuf;
use std::{fs, io};
use anyhow::{Error, Result, anyhow};
use clap::Parser;
use ron_edit::File;
#[derive(Parser)]
struct Opts {
#[arg(long, default_value_t = false, help = "Read from stdin")]
stdin: bool,
#[arg(help = "Read one or multiple files")]
files: Option<Vec<PathBuf>>,
}
fn main() -> Result<()> {
let opts = Opts::parse();
let Opts { stdin, files } = opts;
if let Some(files) = files {
for file in &files {
let ron = fs::read_to_string(file)?;
let ron = File::try_from(ron.as_str())
.map_err(|err| anyhow!("Error while reading `{}`: {err}", file.display()))?;
let mut ret = String::new();
if let Err(err) = rondafk::format(&mut ret, &ron) {
eprintln!("Formatting error: {err}")
};
print!("{ret}");
}
}
if stdin {
let mut ron = String::new();
io::stdin().read_to_string(&mut ron)?;
let ron = File::try_from(ron.as_str()).map_err(Error::msg)?;
let mut ret = String::new();
if let Err(err) = rondafk::format(&mut ret, &ron) {
eprintln!("Formatting error: {err}")
};
print!("{ret}");
}
Ok(())
}