| Crates.io | modelstruct_derive |
| lib.rs | modelstruct_derive |
| version | 0.1.0 |
| created_at | 2025-06-29 21:27:47.627114+00 |
| updated_at | 2025-06-29 21:27:47.627114+00 |
| description | A Rust crate that provides a derive macro Model to automatically generate SQL CREATE TABLE IF NOT EXISTS statements from struct definitions. |
| homepage | https://github.com/famz/modelstruct |
| repository | https://github.com/famz/modelstruct |
| max_upload_size | |
| id | 1731148 |
| size | 9,035 |
A Rust crate that provides a derive macro Model to automatically generate SQL CREATE TABLE IF NOT EXISTS statements from struct definitions.
Model trait to generate SQL table creation statementsOption<T> types as nullable columnsModel and call create_table_sql() or table_name()| Rust Type | SQL Type | Notes |
|---|---|---|
i8, i16, i32 |
INTEGER |
32-bit integers |
i64 |
BIGINT |
64-bit integers |
u8, u16, u32 |
INTEGER |
Unsigned integers |
u64 |
BIGINT |
Unsigned 64-bit integers |
f32, f64 |
REAL |
Floating point numbers |
bool |
BOOLEAN |
Boolean values |
String |
TEXT |
String values |
str |
TEXT |
String slices |
Option<T> |
T NULL |
Nullable columns |
use modelstruct::Model;
#[derive(Model)]
struct User {
id: i32,
name: String,
email: String,
age: Option<i32>,
is_active: bool,
created_at: String,
}
fn main() {
// Generate the SQL statement
let sql = User::create_table_sql();
println!("{}", sql);
// Get the table name
let table_name = User::table_name();
println!("Table name: {}", table_name);
}
This will generate SQL like:
CREATE TABLE IF NOT EXISTS user (
id INTEGER,
name TEXT,
email TEXT,
age INTEGER NULL,
is_active BOOLEAN,
created_at TEXT
);
When you derive Model, the following methods are automatically implemented:
create_table_sql() -> String: Returns the complete SQL statement to create the tabletable_name() -> String: Returns the table name (struct name in lowercase)Add this to your Cargo.toml:
[dependencies]
modelstruct = "0.1.0"
See the examples/ directory for more usage examples.
MIT