#![forbid(unsafe_code)]
#![forbid(unused_lifetimes)]
#![forbid(elided_lifetimes_in_paths)]
use std::path::PathBuf;
use anyhow::Context;
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(short, long, value_name = "FILE")]
schema: PathBuf,
#[arg(short, long, value_name = "DIR")]
target: PathBuf,
}
fn main() -> Result<(), anyhow::Error> {
let cli = Cli::parse();
let schema_is_file = cli.schema.is_file();
let target_is_file = cli.target.is_file();
if !schema_is_file && target_is_file {
anyhow::bail!(
"you might have reversed the arguments: schema path {} is not a file and target path {} is a file",
cli.schema.display(), cli.target.display(),
);
}
if !schema_is_file {
anyhow::bail!("schema path {} does not point to a file", cli.schema.display());
}
if target_is_file {
anyhow::bail!(
"target path {} points to a file but should be a directory",
cli.target.display()
);
}
let schema_text =
std::fs::read_to_string(cli.schema).context("failed to read the schema file")?;
let target = &cli.target;
std::fs::create_dir_all(target).context("failed to create target directory")?;
trustfall_stubgen::generate_rust_stub(&schema_text, target)?;
println!("Successfully created stub! Don't forget to:");
println!(" - add `trustfall` to your dependencies");
println!(" - add `mod adapter;` to your lib.rs");
Ok(())
}