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
Modeltrait 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
Modeland callcreate_table_sql()ortable_name()
Supported Types
| 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 |
Usage
Basic Example
use Model;
This will generate SQL like:
(
id INTEGER,
name TEXT,
email TEXT,
age INTEGER NULL,
is_active BOOLEAN,
created_at TEXT
);
Generated Methods
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)
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Examples
See the examples/ directory for more usage examples.
Limitations
- Only supports structs (not enums or unions)
- Only supports the limited set of types listed above
- Table names are automatically generated from struct names (lowercase)
- No support for custom column names or constraints yet
License
MIT