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.
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
:
[]
= "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_template;
csv_template!;
// 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!;
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
License
Licensed under Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)