Skip to main content

Crate rdbi_codegen

Crate rdbi_codegen 

Source
Expand description

rdbi-codegen: Generate Rust structs and rdbi DAO functions from MySQL schema DDL

This crate provides both a CLI tool and a library for generating Rust code from MySQL schema files. It parses SQL DDL using sqlparser-rs and generates:

  • Serde-compatible structs with #[derive(Serialize, Deserialize, rdbi::FromRow, rdbi::ToParams)]
  • Async DAO functions using rdbi with index-aware query methods

Configure in your Cargo.toml:

[package.metadata.rdbi-codegen]
schema_file = "schema.sql"
output_structs_dir = "src/generated/models"
output_dao_dir = "src/generated/dao"

Then use a minimal build.rs:

fn main() {
    rdbi_codegen::generate_from_cargo_metadata()
        .expect("Failed to generate rdbi code");
}

Include the generated code in your crate root (src/main.rs or src/lib.rs):

mod generated {
    pub mod models;
    pub mod dao;
}

§Alternative: Programmatic Configuration

use std::path::PathBuf;

fn main() {
    rdbi_codegen::CodegenBuilder::new("schema.sql")
        .output_dir(PathBuf::from("src/generated"))
        .generate()
        .expect("Failed to generate rdbi code");

    println!("cargo:rerun-if-changed=schema.sql");
}

§CLI Usage

rdbi-codegen --schema schema.sql --output ./src/generated generate

Re-exports§

pub use config::CodegenConfig;
pub use error::CodegenError;
pub use error::Result;

Modules§

codegen
Code generation module
config
Configuration module for rdbi-codegen
error
Error types for rdbi-codegen
parser
SQL schema parser module using sqlparser-rs

Structs§

CodegenBuilder
Builder pattern for easy configuration in build.rs

Functions§

generate
Main entry point for code generation
generate_from_cargo_metadata
Generate code from [package.metadata.rdbi-codegen] in Cargo.toml