serde_sql

Crates.ioserde_sql
lib.rsserde_sql
version0.1.1
created_at2025-10-12 09:46:15.711113+00
updated_at2025-10-12 11:49:20.287764+00
descriptionStream serde structs into SQLite INSERT statements using a reusable buffer.
homepage
repositoryhttps://github.com/JakkuSakura/serde_sql
max_upload_size
id1879109
size32,989
Jakku Sakura (JakkuSakura)

documentation

README

serde_sql

Crates.io Docs.rs CI

serde_sql streams serde::Serialize structs directly into SQLite statements without allocating intermediate field structures. You provide a String buffer and value; the crate appends the literal value list (v1, v2, …) so you can embed it in any SQL template. When bootstrapping tables, the crate can also infer a basic CREATE TABLE definition from an example record.

Features

  • Zero-copy output: serialise straight into an existing String.
  • Handles Option, numbers, booleans, and strings with proper escaping.
  • Nested structures (maps/arrays) are emitted as JSON text literals.

Quick start

[dependencies]
serde_sql = "0.1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
use serde::Serialize;
use serde_sql::{infer_table_ddl, serialize_insert};

#[derive(Serialize)]
struct User {
    id: u64,
    name: String,
    active: bool,
    notes: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut insert = String::new();
    insert.push_str("INSERT INTO users VALUES ");
    serialize_insert(
        &mut insert,
        &User {
            id: 1,
            name: "Alice".into(),
            active: true,
            notes: Some("admin".into()),
        },
    )?;

    let ddl = infer_table_ddl(
        "users",
        &User {
            id: 0,
            name: "example".into(),
            active: false,
            notes: None,
        },
    )?;

    assert_eq!(insert, "INSERT INTO users VALUES (1, 'Alice', 1, 'admin')");
    assert_eq!(
        ddl,
        r#"CREATE TABLE IF NOT EXISTS "users" (
    "id" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "active" INTEGER NOT NULL,
    "notes" TEXT
);"#
    );
    Ok(())
}

Example

cargo run --example basic
cargo run --example ddl

Testing

cargo test

Documentation

  • API docs can be generated locally via cargo doc --open.
  • Additional usage notes live under docs/usage.md.
Commit count: 0

cargo fmt