| Crates.io | doless |
| lib.rs | doless |
| version | 0.4.2 |
| created_at | 2025-02-27 08:58:52.770212+00 |
| updated_at | 2025-10-16 07:27:31.567394+00 |
| description | A Rust macro to simplify struct mapping , injects cache lookup logic directly into your functions ,and function utilities. |
| homepage | |
| repository | https://github.com/nawajar/DoLess |
| max_upload_size | |
| id | 1571496 |
| size | 138,054 |
DoLess is a Rust library offering procedural macros that simplify both
data-to-struct mapping and cache integration patterns.
It provides two main features:
#[derive(FromHashMap)] — auto-generates a type-safe
From<HashMap<String, String>> implementation for simple and nested structs.#[cache_it(...)] — injects cache lookup logic directly into your functions,HashMap<String, String>String, numeric primitives, bool, Option<T>Vec<T>, Vec<Option<T>>details.name)#[cache_it(...)] to perform cache lookups automaticallykey = "some:key"key = format!("user:{}", id)var = redis — custom cache variable namename = cached_data — custom binding nameCache or AsyncCache.await where needed[dependencies]
doless = "0.4.1"
Includes:
doless_core — Cache and AsyncCache traitsdoless_macros — Procedural macrosdoless — Unified re-export crateFromHashMapuse doless::FromHashMap;
use std::collections::HashMap;
#[derive(FromHashMap, Debug)]
struct Car {
model: String,
brand: String,
details: CarDetails,
tags: Vec<String>,
}
#[derive(FromHashMap, Debug)]
struct CarDetails {
name: String,
description: String,
}
fn main() {
let mut data = HashMap::new();
data.insert("model".into(), "GT-R".into());
data.insert("brand".into(), "Nissan".into());
data.insert("details.name".into(), "Skyline".into());
data.insert("details.description".into(), "Legendary Sports Car".into());
data.insert("tags".into(), "fast,collectible,cool".into());
let car: Car = Car::from(data);
println!("{:#?}", car);
}
use doless::cache_it;
use doless_core::cache::Cache;
use serde::{Serialize, de::DeserializeOwned};
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
#[derive(Clone, Default)]
struct DummyCache {
store: Arc<Mutex<HashMap<String, String>>>,
}
impl Cache for DummyCache {
fn get<T: DeserializeOwned + Clone>(&self, key: &str) -> Option<T> {
let guard = self.store.lock().ok()?;
serde_json::from_str(guard.get(key)?).ok()
}
fn set<T: Serialize>(&self, key: &str, value: &T) {
if let Ok(json) = serde_json::to_string(value) {
if let Ok(mut m) = self.store.lock() {
m.insert(key.into(), json);
}
}
}
}
#[cache_it(key = "user:list")]
fn get_users(cache: &impl Cache) -> Vec<String> {
let cache_data: Option<Vec<String>> = cache_data;
if let Some(users) = cache_data {
return users;
}
let users = vec!["alice".into(), "bob".into()];
cache.set("user:list", &users);
users
}
fn main() {
let cache = DummyCache::new();
println!("{:?}", get_users(&cache));
}
AsyncCacheuse doless::cache_it;
use doless_core::cache::AsyncCache;
use serde::{Serialize, de::DeserializeOwned};
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
#[derive(Clone, Default)]
struct RedisCache {
store: Arc<Mutex<HashMap<String, String>>>,
}
#[async_trait::async_trait]
impl AsyncCache for RedisCache {
async fn get<T>(&self, key: &str) -> Option<T>
where
T: DeserializeOwned + Clone + Send + Sync,
{
let guard = self.store.lock().ok()?;
serde_json::from_str(guard.get(key)?).ok()
}
async fn set<T>(&self, key: &str, value: &T)
where
T: Serialize + Send + Sync,
{
if let Ok(json) = serde_json::to_string(value) {
if let Ok(mut map) = self.store.lock() {
map.insert(key.to_string(), json);
}
}
}
async fn set_with_ttl<T>(&self, key: &str, value: &T, ttl_secs: u64)
where
T: Serialize + Send + Sync,
{
println!("Setting with TTL: {} seconds", ttl_secs);
self.set(key, value).await;
}
}
#[cache_it(key = "user:async")]
async fn get_user_async(cache: &impl AsyncCache) -> Option<String> {
let cached: Option<String> = cache_data;
if cached.is_some() {
return cached;
}
let data = String::from("jeff");
cache.set("user:async", &data).await;
Some(data)
}
#[tokio::main]
async fn main() {
let cache = RedisCache::new();
let user = get_user_async(&cache).await;
println!("User = {:?}", user);
}
🧠 The macro detects async fn automatically and inserts .await where needed.
| Feature | Status |
|---|---|
FromHashMap with nested struct |
✅ |
Vec<T> and Vec<Option<T>> support |
✅ |
| Synchronous cache macro | ✅ |
| Async cache support | ✅ NEW |
TTL + extended cache (via set_with) |
✅ |
| Error diagnostics and reporting | 🚧 Planned |