use std::{
io::{BufReader, Read},
path::PathBuf,
};
use clap::{Parser, Subcommand, ValueEnum};
pub mod compile;
pub mod cryptography;
pub mod inspect;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[command(subcommand)]
command: Command,
}
impl Args {
pub fn execute(self) {
match self.command {
Command::Compile(cmd) => compile::execute_compile_command(cmd),
Command::Inspect(cmd) => inspect::execute_inspect_command(cmd),
Command::Crypt(cmd) => {
cryptography::execute_cryptography_command(cmd);
}
}
}
}
#[derive(Subcommand, Debug, Clone)]
#[command()]
pub enum Command {
Compile(CompileCommand),
Inspect(InspectCommand),
#[command(subcommand)]
Crypt(CryptographyCommand),
}
#[derive(clap::Args, Debug, Clone)]
pub struct CompileCommand {
#[arg(short, long)]
source: Option<PathBuf>,
#[arg(short, long, default_value_os_t = get_working_directory().join("target"))]
target: PathBuf,
#[arg(short, long)]
lang: Option<Lang>,
}
#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Lang {
Rust,
Python,
Typescript,
OpenApi,
Duckdb,
Sqlite,
}
#[derive(clap::Args, Debug, Clone)]
pub struct InspectCommand {
#[arg(short, long)]
source: Option<PathBuf>,
}
#[derive(Subcommand, Debug, Clone)]
#[command()]
pub enum CryptographyCommand {
Hash {
#[arg(short, long)]
source: Option<PathBuf>,
},
Keygen {
#[arg(short, long)]
passphrase: String,
},
Sign {
#[arg(short, long)]
keypair: PathBuf,
#[arg(short, long)]
passphrase: String,
#[arg(short, long)]
source: Option<PathBuf>,
},
}
fn get_working_directory() -> PathBuf {
std::env::current_dir().unwrap()
}
fn open_file_or_stdin(path: Option<PathBuf>) -> std::io::Result<Box<dyn Read>> {
match path {
Some(path) => Ok(Box::new(BufReader::new(std::fs::File::open(path)?))),
None => Ok(Box::new(BufReader::new(std::io::stdin()))),
}
}