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

Download history 7/week @ 2024-09-20 3/week @ 2024-09-27 3/week @ 2024-12-06 72/week @ 2024-12-20 47/week @ 2024-12-27

122 downloads per month

MIT license

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:

  1. Prefix: A user-defined string.
  2. Underscore character (_): Separates the prefix from the ID body.
  3. Timestamp: Encoded in Base-36 to represent the time of generation.
  4. Counter: A single-byte (u8) counter to ensure uniqueness within the same millisecond.
  5. Process Identifier (PID): Encoded in Base-36 to distinguish IDs generated by different processes.
  6. 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

  1. Add puid to your project's dependencies in Cargo.toml:
[dependencies]
puid = "<version>"
  1. Import and use puid as demonstrated in the examples above.

License

This project is licensed under the MIT License.

Dependencies

~315KB