anda_db_derive
A Rust procedural macro crate that provides derive macros for AndaDB schema types.
Features
FieldTypedderive macro: Automatically generates afield_type()function for structs that maps Rust types to AndaDB schema field types.AndaDBSchemaderive macro: Automatically generates aschema()function for structs that creates complete AndaDB Schema definitions.
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Usage
FieldTyped Derive Macro
The FieldTyped derive macro analyzes struct fields and their types, mapping them to the appropriate FieldType enum variants. It supports common Rust types and handles Option<T> wrappers.
Supported Type Mappings
| Rust Type | AndaDB FieldType |
|---|---|
String, &str |
FieldType::Text |
bool |
FieldType::Bool |
i8, i16, i32, i64, isize |
FieldType::I64 |
u8, u16, u32, u64, usize |
FieldType::U64 |
f32 |
FieldType::F32 |
f64 |
FieldType::F64 |
Vec<u8>, [u8], ByteArray, ByteBuf, Bytes |
FieldType::Bytes |
Vec<bf16>, [bf16] |
FieldType::Vector |
Vec<T>, HashSet<T>, BTreeSet<T> |
FieldType::Array |
Option<T> |
FieldType::Option |
HashMap<String, T>, BTreeMap<String, T> |
FieldType::Map |
bf16, half::bf16 |
FieldType::Bf16 |
Json, serde_json::Value |
FieldType::Json |
| Custom structs | Nested FieldType::Map |
Attributes
field_type = "TypeName": Override the inferred type with a specific FieldTypeserde(rename = "field_name"): Use the renamed field in the generated schema
Example
use ;
use ;
use Map;
use HashMap;
use Xid;
// Define a struct with the FieldTyped derive macro
// Define a nested struct
The generated field_type() method returns a FieldType::Map containing the type information for each field, which can be used for schema validation, serialization, or other purposes in AndaDB.
AndaDBSchema Derive Macro
The AndaDBSchema derive macro creates complete AndaDB Schema definitions based on struct fields. It automatically handles field type mapping and supports various attributes for customization.
Attributes
field_type = "TypeName": Override the inferred type with a specific FieldTypeunique: Mark the field as unique in the collectionserde(rename = "new_name"): Use a different name in the schema- Doc comments (
///) are used as field descriptions
Special Field Handling
- The
_idfield is automatically handled by AndaDB and must be of typeu64 - Fields marked with
#[unique]will have unique constraints in the schema - Doc comments are extracted and used as field descriptions in the schema
Example
use ;
use AndaDBSchema;
use ;
use HashMap;
The generated schema() method returns a Result<Schema, SchemaError> containing the complete schema definition that can be used to create collections in AndaDB.
Schema Features
- Automatic Type Inference: Rust types are automatically mapped to appropriate AndaDB field types
- Unique Constraints: Fields marked with
#[unique]will have unique constraints - Field Descriptions: Doc comments are extracted and used as field descriptions
- Custom Types: Use
#[field_type = "TypeName"]to override automatic type inference - Field Renaming: Support for
serde(rename = "name")attributes - Nested Structures: Support for nested structs and complex types
Error Handling
Both derive macros provide compile-time error checking:
- Unsupported field types will generate compile errors with helpful messages
- Invalid
_idfield types (must beu64) will be caught at compile time - Malformed attributes will be detected during compilation
License
Copyright © 2026 LDC Labs.
ldclabs/anda-db is licensed under the MIT License. See LICENSE for the full license text.