#![allow(clippy::multiple_crate_versions)]
#[cfg(feature = "cli")]
pub mod inner {
use facet::Facet;
use sapling::Grammar;
use std::fs;
use std::io::{self, Read};
#[derive(Facet)]
struct Args {
#[facet(positional, default)]
patch_file: Option<String>,
#[facet(named, short = 'n')]
dry_run: bool,
#[facet(named, short = 'v')]
verbose: bool,
#[facet(named, short = 'h')]
help: bool,
}
fn print_usage() {
println!("Usage: sapling [OPTIONS] [PATCH_FILE]");
println!();
println!("Apply syntactic patches to source files with char-level granularity.");
println!();
println!("Arguments:");
println!(" [PATCH_FILE] Path to JSON file containing patches (reads from stdin if not provided)");
println!();
println!("Options:");
println!(" -n, --dry-run Preview changes without writing to disk");
println!(" -v, --verbose Show verbose output");
println!(" -h, --help Show this help message");
}
#[cfg(feature = "cli")]
pub fn main() -> io::Result<()> {
let args: Args = facet_args::from_std_args()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, format!("{e}")))?;
if args.help {
print_usage();
std::process::exit(0);
}
let input = if let Some(path) = args.patch_file {
fs::read_to_string(&path)?
} else {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf)?;
buf
};
let _grammar: Grammar = facet_json::from_str(&input)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, format!("{e:?}")))?;
if args.verbose {
eprintln!("Loaded grammar");
}
Ok(())
}
}
#[cfg(not(feature = "cli"))]
pub mod inner {
#[cfg(not(feature = "cli"))]
pub fn main() {
eprintln!("Please build with the cli feature to run the CLI");
std::process::exit(1);
}
}
pub use inner::main;