| Crates.io | nulid_macros |
| lib.rs | nulid_macros |
| version | 0.7.0 |
| created_at | 2025-12-18 17:22:15.224876+00 |
| updated_at | 2026-01-20 11:09:20.694407+00 |
| description | Procedural macros for nulid - convenient NULID generation |
| homepage | https://github.com/kakilangit/nulid |
| repository | https://github.com/kakilangit/nulid |
| max_upload_size | |
| id | 1992909 |
| size | 9,090 |
Procedural macros for convenient NULID generation.
This crate provides macros to simplify working with NULIDs in your Rust code.
The nulid!() macro provides convenient NULID generation with flexible error handling:
nulid!() - Generate a NULID, panicking on error (for convenience)nulid!(?) - Generate a NULID, returning Result<Nulid, Error> (for error handling)Add this to your Cargo.toml:
[dependencies]
nulid = { version = "0.5", features = ["macros"] }
Then use the macro in your code:
use nulid::nulid;
fn main() {
// Simple generation (panics on error)
let id1 = nulid!();
println!("Generated: {}", id1);
// Multiple generations
let id2 = nulid!();
let id3 = nulid!();
assert_ne!(id1, id2);
assert_ne!(id2, id3);
}
Use nulid!(?) when you need to handle errors gracefully:
use nulid::nulid;
fn create_user_id() -> nulid::Result<nulid::Nulid> {
// Returns Result for explicit error handling
let id = nulid!(?)?;
Ok(id)
}
fn main() -> nulid::Result<()> {
let user_id = create_user_id()?;
println!("User ID: {}", user_id);
// Or use with expect
let order_id = nulid!(?).expect("Failed to generate order ID");
println!("Order ID: {}", order_id);
Ok(())
}
nulid!()use nulid::nulid;
#[test]
fn test_user_creation() {
let user_id = nulid!(); // Panic is fine in tests
// ... rest of test
}
fn main() {
let config_id = nulid!(); // App initialization
// ... rest of app
}
nulid!(?)use nulid::nulid;
pub fn create_entity() -> nulid::Result<Entity> {
let id = nulid!(?)?; // Propagate errors to caller
Ok(Entity { id, /* ... */ })
}
fn handle_request() -> Result<Response, AppError> {
let request_id = nulid!(?).map_err(|e| AppError::IdGeneration(e))?;
// ... process request
}
The macro provides syntactic sugar over the direct API:
use nulid::{nulid, Nulid};
// These are equivalent:
let id1 = nulid!();
let id2 = Nulid::new().expect("Failed to generate NULID");
// These are equivalent:
let id3 = nulid!(?)?;
let id4 = Nulid::new()?;
The macro makes code more concise and readable, especially when generating multiple IDs:
// With macro
let (user_id, session_id, request_id) = (nulid!(), nulid!(), nulid!());
// Without macro
let user_id = Nulid::new().expect("Failed to generate NULID");
let session_id = Nulid::new().expect("Failed to generate NULID");
let request_id = Nulid::new().expect("Failed to generate NULID");
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 = ::nulid::Nulid::new().expect("Failed to generate NULID");
See the examples directory in the nulid repository for more usage examples.
This project is licensed under the MIT License - see the LICENSE file for details.