mod check;
mod contract;
mod mutate;
use std::path::Path;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "cargo impish", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Check {
#[arg(long)]
write: bool,
},
Mutate {
primitive: String,
},
}
fn root(cwd: &Path) -> anyhow::Result<&Path> {
cwd.ancestors()
.find(|dir| dir.join("contract").is_dir())
.ok_or_else(|| anyhow::anyhow!("no contract/ in {} or above it", cwd.display()))
}
fn main() -> ExitCode {
run().unwrap_or_else(|e| {
eprintln!("Error: {e:?}");
ExitCode::from(2)
})
}
fn run() -> anyhow::Result<ExitCode> {
let mut args: Vec<String> = std::env::args().collect();
if args.get(1).map(String::as_str) == Some("impish") {
args.remove(1);
}
let cwd = std::env::current_dir()?;
match Cli::parse_from(args).command {
Command::Check { write } => check::run(root(&cwd)?, write),
Command::Mutate { primitive } => mutate::run(root(&cwd)?, &primitive),
}
}