dodo-bindgen-cli 2.0.0

A CLI tool for generating TypeScript bindings for libraries built with the `dodo_bindgen` crate
use std::path::PathBuf;

use clap::Parser;
use dodo_bindgen_codegen::CodegenOpts;

#[derive(Debug, Parser)]
#[command(
    name = "deno-bindgen2",
    version,
    about = "A Rust FFI bindings generator for Deno. Use this CLI tool with the `deno_bindgen2` library crate",
    long_about = None
)]
pub struct Cli {
    /// Path to output file. Defaults to `<pkg_root>/lib/mod.ts` if unspecified
    #[arg(short = 'o', long, value_hint = clap::ValueHint::FilePath)]
    pub output: Option<PathBuf>,

    /// Cargo profile to build the library with
    #[arg(short = 'p', long, default_value = "release", conflicts_with = "debug")]
    pub profile: String,

    /// Build with the `dev` profile, shorthand for `--profile dev`
    #[arg(short = 'd', long)]
    pub debug: bool,

    /// Target triple to build for, may be repeated. Defaults to the host
    /// target. Each library is copied next to the generated module as
    /// `<lib>.<target>.<so|dylib|dll>`, matching `Deno.build.target`
    #[arg(short = 't', long = "target")]
    pub targets: Vec<String>,

    /// Lazily load the generated module. Useful when the dylib will be
    /// fetched/downloaded remotely.
    #[arg(short = 'l', long)]
    lazy: bool,

    /// Whether to print the rust type declarations in the same typescript
    /// module or to write it on a separate file. Useful when linking together
    /// multiple ffi libraries to use the same rust types
    #[arg(short = 'i', long)]
    pub inline: bool,

    /// Disables generation/linking of utility modules
    #[arg(short = 'N', long)]
    no_modules: bool,
}

impl Cli {
    pub fn profile(&self) -> &str {
        if self.debug { "dev" } else { &self.profile }
    }

    pub fn to_codegen_opts(&self, file_name: String, lib_name: String) -> CodegenOpts {
        CodegenOpts {
            file_name,
            lib_name,
            lazy: self.lazy,
        }
    }
}