redis-derive
redis-derive
This crate implements the FromRedisValue(redis::FromRedisValue) and ToRedisArgs(redis::ToRedisArgs) traits
from redis-rs(https://github.com/redis-rs/redis-rs) for any struct or enum.
This allows seamless type conversion between Rust structs and Redis hash sets, which is more beneficial than JSON encoding the struct and storing the result in a Redis key because when saving as a Redis hash set, sorting algorithms can be performed without having to move data out of the database.
There is also the benefit of being able to retrieve just one value of the struct in the database.
Initial development was done by @Michaelvanstraten 🙏🏽.
Features
- RESP3 Support: Native support for Redis 7+ protocol features including VerbatimString
- Hash Field Expiration: Per-field TTL support using Redis 7.4+ HEXPIRE commands
- Client-Side Caching: Automatic cache management with Redis 6+ client caching
- Cluster Awareness: Hash tag generation for Redis Cluster deployments
- Flexible Naming: Support for various case conversion rules (snake_case, kebab-case, etc.)
- Comprehensive Error Handling: Clear error messages for debugging
- Performance Optimized: Efficient serialization with minimal allocations
Usage and Examples
Add this to your Cargo.toml:
[]
= "0.2.0"
= "0.32"
Import the procedural macros:
use ;
Basic Struct Example
use Commands;
use ;
Enum with Case Conversion
// Works seamlessly with Redis
let role = PowerUser;
con.set?;
let retrieved: UserRole = con.get?;
assert_eq!;
Naming Conventions and Attributes
Case Conversion Rules
The rename_all attribute supports multiple case conversion rules:
Supported case conversion rules:
"lowercase":MyField→myfield"UPPERCASE":MyField→MYFIELD"PascalCase":my_field→MyField"camelCase":my_field→myField"snake_case":MyField→my_field"kebab-case":MyField→my-field
Important Naming Behavior
Key insight: The case conversion applies to both serialization and deserialization:
// With rename_all = "snake_case"
let role = PowerUser;
// Serialization: PowerUser → "power_user"
con.set?;
// Deserialization: "power_user" → PowerUser
let retrieved: UserRole = con.get?;
// Error messages also use converted names:
// "Unknown variant 'admin' for UserRole. Valid variants: [administrator, power_user, regular_user, guest_user]"
Redis Protocol Support
This crate handles multiple Redis value types automatically:
- BulkString: Most common for stored hash fields and string values
- SimpleString: Direct Redis command responses
- VerbatimString: Redis 6+ RESP3 protocol feature (automatically supported)
- Proper error handling: Clear messages for nil values and type mismatches
Advanced Features
Hash Field Expiration (Redis 7.4+)
Cluster-Aware Keys
Client-Side Caching
Development and Testing
The crate includes comprehensive examples in the examples/ directory:
# Start Redis with Docker
&&
# Run basic example
# Test all enum deserialization branches
# Debug attribute parsing behavior
Limitations
- Only unit enums (variants without fields) are currently supported
- Requires redis-rs 0.32.4 or later for full compatibility
Compatibility
- Redis: Compatible with Redis 6+ (RESP2) and Redis 7+ (RESP3)
- Rust: MSRV 1.70+ (follows redis-rs requirements)
- redis-rs: 0.32.4+ (uses
num_of_args()instead of deprecatednum_args())
License: MIT OR Apache-2.0
License: MIT OR Apache-2.0