use std::fs;
use std::io::{self, Read, Write};
use std::path::PathBuf;
use clap::Args;
use crate::Error;
#[derive(Debug, Clone, Args)]
pub struct RonixArgs {
pub input: PathBuf,
#[arg(short, long)]
pub output: Option<PathBuf>,
#[arg(short, long)]
pub attr_path: Option<String>,
}
impl RonixArgs {
pub fn execute(&self) -> Result<(), Error> {
let ron_input = if self.input.as_os_str() == "-" {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf)?;
buf
} else {
fs::read_to_string(&self.input)?
};
let nix_output = match &self.attr_path {
Some(path) => crate::ron_to_nix_module(&ron_input, path)?,
None => crate::ron_to_nix(&ron_input)?,
};
let nix_output = if nix_output.ends_with('\n') {
nix_output
} else {
format!("{nix_output}\n")
};
match &self.output {
Some(path) => fs::write(path, &nix_output)?,
None => io::stdout().write_all(nix_output.as_bytes())?,
}
Ok(())
}
}