Skip to main content

IdGenerator

Struct IdGenerator 

Source
pub struct IdGenerator { /* private fields */ }
Expand description

Generates TrackingIds according to a fixed policy.

§Choosing an entropy width

IDs are random, so duplicates follow the birthday bound: with n IDs drawn from a space of N = 2^bits, the chance that at least two collide is approximately 1 - e^(-n²/2N).

EntropyBody1% collision risk at50% collision risk at
32 bits8 hex chars~9,000 IDs~77,000 IDs
48 bits12 hex chars~2.4 million~20 million
64 bits16 hex chars~610 million~5.1 billion

The default is 32 bits, which reproduces the familiar PKG-9ED9285C shape. For production systems that will ever issue more than a few thousand IDs, configure 64 bits. Widening later is a data migration; choosing it now is a one-line change.

Randomness alone cannot guarantee uniqueness at any width. A durable system should still enforce a unique constraint at the storage layer and retry on conflict.

§Examples

use smart_package_tracker::{Checksum, IdGenerator};

let generator = IdGenerator::builder()
    .prefix("PKG")
    .entropy_bits(64)
    .checksum(Checksum::Iso7064Mod37_36)
    .build()?;

let id = generator.generate()?;
assert!(id.as_str().starts_with("PKG-"));
assert_eq!(id.body().len(), 17); // 16 hex characters + 1 check character
generator.validate(&id)?;

Implementations§

Source§

impl IdGenerator

Source

pub fn builder() -> IdGeneratorBuilder

Start building a generator with a custom policy.

Source

pub fn prefix(&self) -> &str

The prefix placed before the separator.

Source

pub fn entropy_bits(&self) -> u16

Bits of randomness in each generated ID.

Source

pub fn checksum(&self) -> Checksum

The configured check-character scheme.

Source

pub fn entropy_bytes(&self) -> usize

Number of random bytes needed per ID.

Source

pub fn generate(&self) -> Result<TrackingId>

Available on crate feature os-rng only.

Generate an ID using the operating system’s cryptographic RNG.

Requires the os-rng feature (enabled by default). Without it, use generate_from_entropy.

§Errors

Returns Error::Entropy if the OS entropy source is unavailable. This crate never silently falls back to a weaker source.

Source

pub fn generate_from_entropy(&self, bytes: &[u8]) -> Result<TrackingId>

Generate an ID from caller-supplied entropy.

Useful for deterministic tests, for reproducing an ID from stored bytes, or when the entropy comes from an HSM or a database sequence rather than the OS.

§Errors

Returns Error::InsufficientEntropy if fewer than entropy_bytes bytes are supplied. Extra bytes are ignored.

Source

pub fn validate(&self, id: &TrackingId) -> Result<()>

Check that id was produced by this generator’s policy.

Verifies the prefix, the body length, that the entropy characters are uppercase hexadecimal, and the check character. Note that this cannot prove provenance — it only rules out IDs that this policy could never have produced.

§Errors

Returns Error::IdPolicyMismatch describing the first failure.

Trait Implementations§

Source§

impl Clone for IdGenerator

Source§

fn clone(&self) -> IdGenerator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for IdGenerator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IdGenerator

Source§

fn default() -> Self

PKG- + 32 bits of entropy, no check character — the PKG-9ED9285C format. See the type-level docs before using this in production.

Source§

impl Eq for IdGenerator

Source§

impl PartialEq for IdGenerator

Source§

fn eq(&self, other: &IdGenerator) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for IdGenerator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.