Features
- Unique Identifiers: ULID-based sortable and unique ID generation supporting both random and worker-based approaches
- UTC DateTime Handling: A UTC-focused datetime type optimized for distributed systems
- WASM Compatible: Full support for WebAssembly targets
- Database Support: PostgreSQL and MySQL integration via optional features
Installation
Addbomboni_common to your Cargo.toml:
Optional Features
serde
serde
Enable serialization and deserialization with serde
tokio
tokio
Enable async APIs using tokio runtime
chrono
chrono
Enable conversion with the chrono datetime crate
wasm
wasm
Enable WebAssembly support
postgres
postgres
Enable PostgreSQL type conversions
mysql
mysql
Enable MySQL type conversions
Unique Identifiers
TheId type provides semi-globally unique and sortable identifiers based on ULID (Universally Unique Lexicographically Sortable Identifier).
Random ID Generation
Generate random sortable IDs for general use:Parsing IDs
Parse IDs from string representations:Worker-Based ID Generation
For distributed systems where each worker needs to generate unique IDs without coordination:Each
WorkerIdGenerator should have a unique worker ID in your distributed system to ensure global uniqueness. The generator uses a 16-bit worker ID and 16-bit sequence number.Async ID Generation
With thetokio feature enabled, you can generate IDs in async contexts:
ID Construction and Decoding
Create IDs from components or decode existing IDs:UTC DateTime
TheUtcDateTime type provides a simple, UTC-focused datetime implementation with WASM support.
Basic Usage
Parsing and Formatting
Timestamp Conversion
Chrono Integration
With thechrono feature enabled:
Database Integration
PostgreSQL
With thepostgres feature, Id and UtcDateTime types can be used directly with PostgreSQL queries.
MySQL
With themysql feature, Id and UtcDateTime types can be used directly with MySQL queries.
Serialization
With theserde feature enabled:
API Reference
Id Type
A semi-globally unique and sortable identifier based on ULID.Methods:
generate()- Generates a new random sortable IDgenerate_multiple(count)- Generates multiple random IDs efficientlyfrom_worker_parts(time, worker, sequence)- Creates ID from worker componentsfrom_time_and_random(time, random)- Creates ID from time and random valuedecode_worker()- Decodes ID into (timestamp, worker, sequence)decode_time_and_random()- Decodes ID into (timestamp, random)
WorkerIdGenerator Type
Generator for IDs with a specific worker identifier for distributed systems.Methods:
new(worker)- Creates a new generator with the given worker IDgenerate()- Generates a new ID (blocking)generate_async()- Generates a new ID (async, requirestokiofeature)generate_multiple(count)- Generates multiple IDs efficiently (blocking)generate_multiple_async(count)- Generates multiple IDs efficiently (async)
UtcDateTime Type
A date and time in the UTC timezone.Methods:
now()- Returns the current UTC timenew(seconds, nanoseconds)- Creates from seconds and nanosecondsfrom_timestamp(seconds, nanoseconds)- Creates with error handlingfrom_seconds(seconds)- Creates from Unix timestampfrom_nanoseconds(nanos)- Creates from nanosecondstimestamp()- Returns (seconds, nanoseconds) tupleparse_rfc3339(string)- Parses RFC 3339 formatted stringformat_rfc3339()- Formats as RFC 3339 string
Related Modules
bomboni_core
Core utilities and abstractions
bomboni_macros
Convenient utility macros