csv_gen
: Type-Safe CSV Code Generation for Rust
A Rust procedural macro that transforms CSV data into safe, zero-cost code with powerful diagnostics. Generate match arms, loops, and nested queries directly from CSV files, ensuring type safety and deterministic code generation.
๐ฆ Installation
Add csv_gen
to your Cargo.toml
:
[]
= "0.1.0"
๐ง Usage
csv_gen
uses attribute macros to inject code based on CSV data. It supports grouping, and subqueries, with a focus on clarity and safety.
Basic Example: match
with CSV Data
A match arm will be generated for each name. If a name appears twice the compiler will complain about duplicate match patterns.
from
: Path to the CSV file.where
: SQL-like filter (e.g.,category = 'citrus'
).{{name}}
: extracts the value of the fieldname
and includes in the source as a literal string.
The macro generates a match arm that checks against the CSV data, with $name
replaced with the strings from the file.
Advanced Example: Grouping and Subqueries
!;
csv_gen
[[
/]]
: Nested queries within grouped data.{{{name : PascalCase}}}
: Value included as identifier in pascal case
๐ Key Features
- Type Safety: Generate enums from CSV to enable trype checking that lookups can be performed safely
- Zero-Cost Abstraction: No runtime overheadโcode is expanded at compile time.
- Developer Diagnostics: Errors and warnings during code generation help catch issues early.
- SQL-Like
where
: Filters use familiar SQL syntax (e.g.,price > 10.0
). - Deterministic Ordering: Results are ordered lexicographically.
๐ Notes
- Lexical Ordering: Results are always ordered by selected fields.
- Results are deduplicated: equivalent to a select distinct in SQL
๐งช Example CSV Structure
For the above examples, your CSV might look like:
fruits.csv
id,name,category
1,Orange,citrus
2,Banana,fruit
3,Lemon,citrus
products.csv
id,name,category,price,active
1,Widget,A,9.99,true
2,Gadget,B,12.50,true
3,Thingy,A,5.00,false
๐ Getting Started
- Place your CSV files in your project directory.
- Use
#[csv_gen]
to inject code based on the data. - Compile and enjoy type-safe, deterministic code generation!
๐ Learn More
Check out the GitHub repository for examples, tests, and contribution guidelines. Let us know if you'd like to see additional features like CSV schema validation or support for more complex queries!