nulid_macros
Procedural macros for convenient NULID generation.
This crate provides macros to simplify working with NULIDs in your Rust code.
Features
The nulid!() macro provides convenient NULID generation with flexible error handling:
nulid!()- Generate a NULID, panicking on error (for convenience)nulid!(?)- Generate a NULID, returningResult<Nulid, Error>(for error handling)
Usage
Add this to your Cargo.toml:
[]
= { = "0.5", = ["macros"] }
Then use the macro in your code:
use nulid;
Error Handling
Use nulid!(?) when you need to handle errors gracefully:
use nulid;
When to Use Each Variant
Use nulid!()
- In tests where panicking is acceptable
- In initialization code where failure should stop the program
- When you want concise, readable code and don't need error recovery
use nulid;
Use nulid!(?)
- In library code where you want to propagate errors
- In production code that needs graceful error handling
- When integrating with other Result-returning code
use nulid;
Comparison with Direct API
The macro provides syntactic sugar over the direct API:
use ;
// These are equivalent:
let id1 = nulid!;
let id2 = new.expect;
// These are equivalent:
let id3 = nulid!?;
let id4 = new?;
The macro makes code more concise and readable, especially when generating multiple IDs:
// With macro
let = ;
// Without macro
let user_id = new.expect;
let session_id = new.expect;
let request_id = new.expect;
Performance
The macro has zero runtime overhead - it expands to direct function calls at compile time:
// This macro call:
let id = nulid!;
// Expands to:
let id = new.expect;
Examples
See the examples directory in the nulid repository for more usage examples.
License
This project is licensed under the MIT License - see the LICENSE file for details.