use clap::{Parser, Subcommand};
mod context;
mod generator;
use context::Context;
use generator::dispatch::{dispatch, generate_all, GenKind};
#[derive(Parser)]
#[command(name = "rvy")]
#[command(about = "Rust code generator CLI", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(long, global = true)]
dry_run: bool,
#[arg(long, global = true)]
force: bool,
}
#[derive(Subcommand)]
enum Commands {
New {
#[command(subcommand)]
command: NewCommands,
},
#[command(name = "new-all")]
NewAll {
name: String,
},
Gen {
#[command(subcommand)]
command: GenCommands,
},
}
#[derive(Subcommand)]
enum NewCommands {
Project {
name: String,
},
}
#[derive(Subcommand)]
enum GenCommands {
Service {
name: String,
},
Usecase {
name: String,
},
Repository {
name: String,
},
Data {
name: String,
},
Handler {
name: String,
},
Swagger {
name: String,
},
Adapter {
name: String,
#[arg(short, long, default_value = "all")]
db_type: String,
},
Config {
name: String,
},
Factory {
name: String,
},
Example {
name: String,
},
}
fn main() {
let cli = Cli::parse();
let mut ctx = Context {
dry_run: cli.dry_run,
force: cli.force,
is_new_all: false,
};
match cli.command {
Commands::New { command } => match command {
NewCommands::Project { name } => {
generator::project::generate(&ctx, &name);
}
},
Commands::NewAll { name } => {
ctx.is_new_all = true; generate_all(&ctx, &name);
}
Commands::Gen { command } => match command {
GenCommands::Service { name } => dispatch(GenKind::Service, &ctx, &name),
GenCommands::Usecase { name } => dispatch(GenKind::Usecase, &ctx, &name),
GenCommands::Repository { name } => dispatch(GenKind::Repository, &ctx, &name),
GenCommands::Data { name } => dispatch(GenKind::Data, &ctx, &name),
GenCommands::Handler { name } => dispatch(GenKind::Handler, &ctx, &name),
GenCommands::Swagger { name } => {
let force_ctx = Context {
dry_run: ctx.dry_run,
force: true, is_new_all: ctx.is_new_all,
};
dispatch(GenKind::Handler, &force_ctx, &name);
}
GenCommands::Adapter { name, db_type } => {
if db_type.to_lowercase() == "all" {
dispatch(GenKind::AdapterAll, &ctx, &name);
} else {
dispatch(GenKind::Adapter(db_type), &ctx, &name);
}
}
GenCommands::Config { name } => dispatch(GenKind::Config, &ctx, &name),
GenCommands::Factory { name } => dispatch(GenKind::Factory, &ctx, &name),
GenCommands::Example { name } => dispatch(GenKind::Example, &ctx, &name),
},
}
}