slimwasm 0.1.0

A WASM size profiler for wasm32-unknown-unknown and wasm32-wasip2 binaries
Documentation
//! slimwasm - A WASM size profiler for wasm32-unknown-unknown and wasm32-wasip2 binaries

use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

mod analysis;
mod commands;
mod parser;

pub use analysis::*;
pub use parser::*;

#[derive(Parser)]
#[command(name = "slimwasm")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Show the largest items in a .wasm file (functions, data, sections) ranked by size
    Top {
        /// Path to the .wasm file
        path: PathBuf,

        /// Number of items to show (default: 20)
        #[arg(short = 'n', long, default_value = "20")]
        limit: usize,

        /// Show retained size (items + everything they keep alive)
        #[arg(short, long)]
        retained: bool,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Explain why a named item is retained in the binary (who keeps it alive)
    Why {
        /// Path to the .wasm file
        path: PathBuf,

        /// Name of the item to trace (function name, partial match supported)
        name: String,

        /// Maximum depth of the retention path
        #[arg(short, long, default_value = "10")]
        depth: usize,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Compare two .wasm files and show what grew/shrank
    Diff {
        /// Path to the old .wasm file
        old: PathBuf,

        /// Path to the new .wasm file
        new: PathBuf,

        /// Number of items to show (default: 20)
        #[arg(short = 'n', long, default_value = "20")]
        limit: usize,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },

    /// Dump component export sizes (for wasm32-wasip2 / WIT component model)
    Exports {
        /// Path to the .wasm component file
        path: PathBuf,

        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Top {
            path,
            limit,
            retained,
            json,
        } => commands::top::run(&path, limit, retained, json),
        Commands::Why {
            path,
            name,
            depth,
            json,
        } => commands::why::run(&path, &name, depth, json),
        Commands::Diff {
            old,
            new,
            limit,
            json,
        } => commands::diff::run(&old, &new, limit, json),
        Commands::Exports { path, json } => commands::exports::run(&path, json),
    }
}