| Crates.io | celers-macros |
| lib.rs | celers-macros |
| version | 0.1.0 |
| created_at | 2026-01-18 14:53:59.128383+00 |
| updated_at | 2026-01-18 14:53:59.128383+00 |
| description | Procedural macros for CeleRS task definitions (#[task], #[derive(Task)]) |
| homepage | |
| repository | https://github.com/cool-japan/celers |
| max_upload_size | |
| id | 2052438 |
| size | 244,721 |
Procedural macros for simplified CeleRS task definitions.
This crate provides ergonomic procedural macros for defining CeleRS tasks without boilerplate:
#[task] - Attribute macro for converting async functions into tasks#[derive(Task)] - Derive macro for implementing the Task traitTask trait implementationuse celers_macros::task;
use celers_core::Result;
#[task]
async fn add_numbers(a: i32, b: i32) -> Result<i32> {
Ok(a + b)
}
// Generated automatically:
// - AddNumbersTask: impl Task
// - AddNumbersTaskInput { a: i32, b: i32 }
// - AddNumbersTaskOutput = i32
#[task(
name = "tasks.process_data",
timeout = 60,
priority = 10,
max_retries = 3
)]
async fn process_data(data: String) -> Result<String> {
Ok(format!("Processed: {}", data))
}
// Access configuration:
let task = ProcessDataTask;
assert_eq!(task.name(), "tasks.process_data");
assert_eq!(task.timeout(), Some(60));
#[task]
async fn register_user(
#[validate(min = 18, max = 120, message = "Age must be between 18 and 120")]
age: i32,
#[validate(email, message = "Invalid email address")]
email: String,
#[validate(password_strength)]
password: String,
) -> Result<String> {
Ok(format!("User registered with email {}", email))
}
Use convenient shortcuts for common validation patterns:
#[task]
async fn create_profile(
#[validate(email)] email: String,
#[validate(url)] website: String,
#[validate(phone)] phone: String,
#[validate(positive)] age: i32,
#[validate(alphanumeric)] username: String,
#[validate(uuid)] user_id: String,
#[validate(ipv4)] server_ip: String,
#[validate(credit_card)] card: String,
#[validate(bitcoin_address)] btc_wallet: String,
) -> Result<String> {
Ok("Profile created".to_string())
}
#[task]
async fn send_notification(
user_id: u64,
message: String,
email: Option<String>,
sms: Option<String>,
) -> Result<bool> {
// Optional fields automatically get serde support
// with skip_serializing_if and default
Ok(true)
}
#[task]
async fn process_items<T>(items: Vec<T>) -> Result<usize>
where
T: Send + Clone,
{
Ok(items.len())
}
// Use with specific type:
let task = ProcessItemsTask::<String>;
See the examples directory for comprehensive demonstrations:
basic_task.rs - Basic #[task] macro usagederive_task.rs - #[derive(Task)] usagevalidation.rs - All 37 predefined validators with 13 examplesRun examples:
cargo run --example basic_task
cargo run --example validation
Use #[validate(...)] on function parameters:
Numeric Validation:
min = N - Minimum value (inclusive)max = N - Maximum value (inclusive)positive - Must be > 0negative - Must be < 0String/Collection Length:
min_length = N - Minimum lengthmax_length = N - Maximum lengthnot_empty - Must not be emptyPattern Validation:
pattern = "regex" - Custom regex patternCustom Validator Functions:
custom = "function_name" - Call a custom validation functionFor complex validation logic, define a function with signature fn(&T) -> Result<(), String>:
fn validate_even(value: &i32) -> Result<(), String> {
if value % 2 == 0 {
Ok(())
} else {
Err("Value must be an even number".to_string())
}
}
#[task]
async fn process(
#[validate(custom = "validate_even")]
count: i32,
) -> Result<String> {
Ok(format!("Processing {} items", count))
}
Custom Error Messages:
message = "error text" - Custom error message for validation failuresAdd to your Cargo.toml:
[dependencies]
regex = "1.11" # For pattern validators
serde_json = "1.0" # For json validator (usually already present)
For detailed documentation including macro expansion examples, troubleshooting, and best practices, see the module documentation or run:
cargo doc --open
To see generated code, use cargo-expand:
cargo install cargo-expand
cargo expand --lib
cargo expand --example basic_task
asyncResult<T>impl Trait return typescelers-core: Core Task trait definitioncelers: Main CeleRS librarySee workspace license.