#csv #proc-macro #codegen

macro csv-codegen

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.

10 releases

Uses new Rust 2024

0.2.3 Oct 24, 2025
0.2.2 Aug 5, 2025
0.2.1 Jul 4, 2025
0.1.5 Jun 22, 2025

#2768 in Parser implementations

Apache-2.0

96KB
2K SLoC

csv-codegen

A Rust procedural macro for generating code from CSV data at compile time. Transform CSV files into Rust constants, functions, structs, and other code using a flexible templating syntax.

Status: This crate is in development and was built with AI assistance (Claude Code). The API may see changes in future versions. Contributions and code review are especially welcome!

Features

  • Compile-time CSV processing - CSV files are read and processed during compilation
  • Template-based code generation - Use a simple template syntax to generate any Rust code
  • Field transformations - Convert CSV data to valid Rust identifiers, constants, types, and literals
  • Filtering support - Include/exclude rows based on conditions
  • Pivoting - Transform columns into key-value pairs for more flexible data structures
  • Type-safe literals - Generate properly typed numeric literals (42_f64, 10_u32, etc.)

Installation

Add this to your Cargo.toml:

[dependencies]
csv-codegen = "0.2"

Simple example

Given a CSV file products.csv:

name,price,category
apple,1.20,fruit
carrot,0.80,vegetable
banana,0.90,fruit

Generate constants:

use csv_codegen::csv_template;

csv_template!("products.csv", #each {
    pub const #CONST({name}_PRICE): f64 = #({price}_f64);
});

// Generates:
// pub const APPLE_PRICE: f64 = 1.20_f64;
// pub const CARROT_PRICE: f64 = 0.80_f64;
// pub const BANANA_PRICE: f64 = 0.90_f64;

assert_eq!(APPLE_PRICE, 1.20);

For more examples including filtering, pivoting, and advanced templating, see the full documentation.

Use Cases

  • Configuration from CSV - Generate constants and enums from configuration data
  • Test data - Create test fixtures from CSV files
  • Code tables - Transform lookup tables into efficient match statements
  • Translations - Create internationalization constants from CSV files

Limitations

  • CSV files are read at compile time - changes require recompilation
  • Empty cells are treated as empty strings
  • One csv file can be read per invocation (no joins)
  • Only basic filtering conditions are supported (==, !=, &&, ||) and only comparing between fields and literals

More Examples

For additional real-world examples including internationalization, enum generation, medical data processing, and complex nested filtering, see the integration test suite.

License

Licensed under Apache License, Version 2.0 (see LICENSE file or https://www.apache.org/licenses/LICENSE-2.0)

Dependencies

~1.7–2.4MB
~33K SLoC