csv-codegen 0.2.3

A Rust procedural macro that transforms CSV data into safe, zero-cost code. Generate match arms, loops, and nested queries directly from CSV files, ensuring type safety and deterministic code generation.
Documentation
use criterion::{Criterion, criterion_group, criterion_main};
use std::fs;
use std::time::Instant;

fn generate_large_csv(rows: usize) -> String {
    let mut csv_content = String::with_capacity(rows * 100);

    // Header with unique row_id field
    csv_content.push_str("row_id,region,department,sales_amount,employee_count\n");

    let regions = ["north", "south", "east", "west", "central"];
    let departments = ["sales", "engineering", "marketing", "support", "hr"];

    for i in 0..rows {
        let region = regions[i % regions.len()];
        let department = departments[i % departments.len()];
        let sales_amount = 1000.0 + ((i * 31) % 49000) as f64;
        let employee_count = (i % 50) + 1;

        csv_content.push_str(&format!(
            "row_{i},{region},{department},{sales_amount:.2},{employee_count}\n"
        ));
    }

    csv_content
}

fn benchmark_compile_time(c: &mut Criterion) {
    let mut group = c.benchmark_group("csv_macro_compile_time");

    // Test different CSV sizes to see how compile time scales
    let sizes = [500_000];

    for &size in &sizes {
        group.bench_function(format!("csv_rows_{size}"), |b| {
            b.iter_custom(|iters| {
                let mut total_duration = std::time::Duration::new(0, 0);

                for _i in 0..iters {
                    // Generate CSV file with the target size
                    let csv_content = generate_large_csv(size);
                    fs::write("benchmark_data.csv", &csv_content).unwrap();

                    // Measure compilation time using trybuild
                    let start = Instant::now();

                    let t = trybuild::TestCases::new();
                    // Compile the static test file that references the CSV
                    t.pass("benches/build-sample/benchmark_test.rs");

                    let duration = start.elapsed();
                    total_duration += duration;
                }

                // Clean up
                let _ = fs::remove_file("benchmark_data.csv");

                total_duration
            });
        });
    }

    group.finish();
}

criterion_group!(benches, benchmark_compile_time);
criterion_main!(benches);