db_derive (0.1.0)

Published 2026-03-06 20:35:25 +00:00 by faicel in homeiot/db_derive

Installation

[registries.forgejo]
index = "sparse+" # Sparse index
# index = "" # Git

[net]
git-fetch-with-cli = true
cargo add db_derive@0.1.0 --registry forgejo

About this package

Procedural macro to derive HasColumns: generates column_map() from struct field names and types

db_derive (HasColumns)

Rust License: MIT

Procedural macro crate that implements the HasColumns trait for structs. It generates a column_map() function returning a mapping of field names to their Rust types.

Useful for reflection-like use cases (e.g. ORM, serialization, or schema generation) when you need to know struct field names and types at runtime.

Requirements

  • Rust 1.70 or later (edition 2021)

Usage

Add the crate to your Cargo.toml (as a dependency and for the derive):

[dependencies]
db_derive = { path = "." }  # or from crates.io with the published name

# Define the trait in your crate (or in a shared lib):
# trait HasColumns { fn column_map() -> std::collections::HashMap<String, String>; }

In your code:

use db_derive::HasColumns;
use std::collections::HashMap;

trait HasColumns {
    fn column_map() -> HashMap<String, String>;
}

#[derive(HasColumns)]
struct User {
    id: i32,
    username: String,
    email: String,
}

fn main() {
    let columns = User::column_map();
    // e.g. {"id": "i32", "username": "String", "email": "String"}
}

Limitations

  • Only structs with named fields are supported (no tuple structs, no enums).
  • Types in the map are the Rust type names as written (e.g. String, i32, custom types like OpenClose).

Deploy (Forgejo Package Registry)

To publish this crate to your Forgejo package registry (manual test), see docs/DEPLOY.md for token setup, credentials.toml, and cargo publish --registry forgejo.

Dependencies

ID Version
proc-macro2 ^1.0.106
quote ^1.0.45
syn ^2.0.117
Details
Cargo
2026-03-06 20:35:25 +00:00
23
Faicel <faicelbhk@gmail.com>
8 KiB
Assets (1)
Versions (1) View all
0.1.0 2026-03-06