db_derive (1.0.0)
Installation
[registries.forgejo]
index = "sparse+ " # Sparse index
# index = " " # Git
[net]
git-fetch-with-cli = truecargo add db_derive@1.0.0 --registry forgejoAbout this package
db_derive (HasColumns)
Important
The canonical repository for this project lives on Forgejo: https://code.bhk-itsolutions.com/homeiot/db_derive.git. This GitHub repository is only a mirror and is not the primary git remote.
Procedural macro crate that implements the HasColumns trait for structs. It generates a column_map() function returning a mapping of column keys 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.71 or later (edition 2021; the MSRV is pinned as
rust-versionin the crate manifest)
Install
The crate is published to the private Forgejo package registry:
[dependencies]
db_derive = { version = "0.1", registry = "forgejo" }
Usage
The HasColumns trait lives in the consumer crate: the generated code references it unqualified, so each consumer declares it once (for example in a columns module, like the companion crate db_handler) and imports it alongside the derive.
use db_derive::HasColumns;
use std::collections::HashMap;
trait HasColumns {
fn column_map() -> HashMap<String, String>;
}
#[derive(HasColumns)]
struct User<T> {
id: i32,
username: String,
r#type: T, // raw identifier: the column key is the clean `type`
}
fn main() {
// Generic parameters are supported: the impl carries the struct's generics.
let columns: HashMap<String, String> = User::<String>::column_map();
assert_eq!(columns["id"], "i32");
assert_eq!(columns["username"], "String");
// Values are the token-stream rendering of each declared field type, so
// a generic parameter renders as its name (`T`), not the concrete argument.
assert_eq!(columns["type"], "T");
}
Limitations
- Supported shapes: structs with named fields. Generic parameters, lifetimes and where-clauses are supported and carried into the generated implementation. Tuple structs, enums and unit structs are not supported: the derive emits a spanned compile error pointing at the annotated item (no panics).
- Type strings: map values are the standard token-stream rendering of each field type, deterministic within a toolchain. Real examples:
String,Vec < u8 >,& 'static str,std :: string :: String; grouped types print their contents:(i32, bool),[u8; 4]. Treat the values as opaque: exact-match consumers must compare only against values produced by the same crate build. - Column keys: the field names, with raw-identifier prefixes normalized (
r#typemaps to the key"type").
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 |
| trybuild | ^1 |