csv-codegen 0.1.0

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

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:

[dependencies]
csv_gen = "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

fn handle_citrus(fruit_name: &str) -> &'static str {
    csv_macro::csv_gen!("tests/fruits.csv", where(category = "citrus");
        match fruit_name {
            [[
                {{name}} => concat!("Found citrus: ", {{name}}),
            ]]
            _ => "Not a citrus fruit",
        }
    )
}

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 field name 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_macro::csv_gen!("tests/products.csv", where(active = true);
        #[derive(Debug, PartialEq)]
        enum Product {
            [[
                {{{name : PascalCase}}},
            ]]
        }
        impl FromStr for Product {
            type Err = ();
            fn from_str(string: &str) -> Result<Self, Self::Err> {
                match string {
                    [[
                        {{name}} => Ok(Self::{{{name: PascalCase}}}),
                    ]]
                    _ => Err(()),
                }
            }
        }
    );
  • [[/]]: 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

  1. Place your CSV files in your project directory.
  2. Use #[csv_gen] to inject code based on the data.
  3. 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!