3 unstable releases
0.2.1 | Dec 26, 2024 |
---|---|
0.2.0 | Feb 4, 2023 |
0.1.0 | May 2, 2022 |
#372 in Text processing
122 downloads per month
13KB
207 lines
puid
puid is a flexible unique ID generator that produces IDs using a customizable prefix and various components. It's inspired by the simplicity and structure of ch_-style IDs.
Features
Each generated ID consists of the following components:
- Prefix: A user-defined string.
- Underscore character (
_
): Separates the prefix from the ID body. - Timestamp: Encoded in Base-36 to represent the time of generation.
- Counter: A single-byte (
u8
) counter to ensure uniqueness within the same millisecond. - Process Identifier (PID): Encoded in Base-36 to distinguish IDs generated by different processes.
- Random Characters: A sequence of cryptographically random characters to provide additional entropy.
This structure ensures uniqueness while keeping the IDs compact and human-readable.
Examples
Default Configuration
By default, puid
generates IDs with a random character length of 16. Here's an example:
use puid::Puid;
fn main() {
let id = Puid::builder()
.prefix("foo")
.unwrap()
.build()
.unwrap();
println!("{}", id); // Example: foo_l2ok01bl0yq2i2ElC7zWaCR8
}
Custom Random Length
You can specify the length of the random character sequence to suit your needs. For example, setting the entropy to 24:
use puid::Puid;
fn main() {
let id = Puid::builder()
.prefix("bar")
.unwrap()
.entropy(24)
.build()
.unwrap();
println!("{}", id); // Example: bar_l2ok1yvk1z4aOz1P7kecCTaqUGq1wgKfHGZC
}
Usage
- Add
puid
to your project's dependencies inCargo.toml
:
[dependencies]
puid = "<version>"
- Import and use
puid
as demonstrated in the examples above.
License
This project is licensed under the MIT License.
Dependencies
~315KB