Crate modelstruct

Crate modelstruct 

Source
Expand description

§ModelStruct

A Rust crate that provides a derive macro Model to automatically generate SQL CREATE TABLE IF NOT EXISTS statements from struct definitions.

§Features

  • Automatic SQL Generation: Derive the Model trait to generate SQL table creation statements
  • Type Safety: Only supports a limited set of Rust types that can be safely mapped to SQL types
  • Nullable Support: Handles Option<T> types as nullable columns
  • Simple API: Just derive Model and call create_table_sql() or table_name()

§Supported Types

Rust TypeSQL TypeNotes
i8, i16, i32INTEGER32-bit integers
i64BIGINT64-bit integers
u8, u16, u32INTEGERUnsigned integers
u64BIGINTUnsigned 64-bit integers
f32, f64REALFloating point numbers
boolBOOLEANBoolean values
StringTEXTString values
strTEXTString slices
Option<T>T NULLNullable columns

§Example

use modelstruct::Model;
 
#[derive(Model)]
struct User {
    id: i32,
    name: String,
    email: Option<String>,
    age: Option<i32>,
    is_active: bool,
}
 
fn main() {
    let sql = User::create_table_sql();
    println!("{}", sql);
    // Output:
    // CREATE TABLE IF NOT EXISTS user (
    //     id INTEGER,
    //     name TEXT,
    //     email TEXT NULL,
    //     age INTEGER NULL,
    //     is_active BOOLEAN
    // );
}

Traits§

Model
Trait that provides SQL table creation functionality

Derive Macros§

Model
Derive macro for generating SQL table creation code