MongoDB Collection Macros
Procedural macros for MongoDB collection trait derivation.
Collection Derive Macro
The Collection derive macro automatically implements the Collection trait, which identifies MongoDB collection names for your Rust types.
Features
- Automatic trait implementation - Derive the
Collectiontrait with a single attribute - Custom collection names - Override default naming with
#[collection(name = "...")] - Smart naming conventions - Default plural snake_case naming (User → users, Category → categories)
- Compile-time type safety - Catch errors at compile time, not runtime
- Thread-safe - Built-in
Send + Syncsupport for concurrent applications - Zero runtime overhead - All name resolution happens at compile time
Usage
1. Using Default Collection Name (Auto-Pluralized Snake Case)
use ;
use Collection;
// Collection name will automatically be "users" (pluralized)
assert_eq!;
2. Specifying a Custom Collection Name
use ;
use Collection;
// Overrides default naming with custom collection name
assert_eq!;
3. Complex Struct Example
use ;
use Collection;
// Without specifying name, defaults to "user_profiles" (pluralized snake_case)
assert_eq!;
Naming Conversion Rules
When #[collection(name = "...")] is not specified, the macro automatically converts struct names to plural snake_case:
| Struct Name | Default Collection Name | Description |
|---|---|---|
User |
users |
Singular → Plural |
UserProfile |
user_profiles |
CamelCase → snake_case + Plural |
Course |
courses |
Singular → Plural |
Category |
categories |
y → ies |
Book |
books |
Singular → Plural |
CourseEnrollment |
course_enrollments |
CamelCase → snake_case + Plural |
Requirements
The Collection trait has the following constraints:
Send + Sync + Sized- Required by the trait itself for thread-safe operations
Recommended traits for MongoDB usage:
When working with MongoDB collections, your types should typically implement:
Serialize(from serde) - Required for writing to MongoDBDeserialize(from serde) - Required for reading from MongoDBDebug- Useful for debuggingClone- Often needed for data manipulation
Note: The Collection derive macro itself doesn't enforce serde traits, but MongoDB operations require them for serialization/deserialization.
Complete Example
use ;
use Collection;
// Example 1: User entity (using default plural naming)
// Example 2: Course entity (using default plural naming)
// Example 3: Category entity (demonstrating smart pluralization)
Error Handling
If you attempt to use the Collection macro on a non-struct type, you'll get a compile error:
// ❌ Error: Collection can only be derived for structs
MongoDB Integration
The Collection trait provides a convenient collection() method for direct MongoDB integration:
use ;
use ;
use Collection;
async
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
= "2.0"
Best Practices
Thread Safety
Types implementing Collection are required to be Send + Sync, making them safe to use across threads. This is particularly important for web servers and concurrent applications:
use Arc;
use task;
use Collection;
use ;
async
Generic Functions
Use the Collection trait for generic database operations:
use ;
use Collection;
async
// Usage
let user_count = .await;
let post_count = .await;
Repository Pattern
Combine with the repository pattern for clean architecture:
// Usage
let user_repo = new;
let user = user_repo.find_by_id.await;
License
MIT