type-safe-id

A type-safe, K-sortable, globally unique identifier.

Typed implementation of https://github.com/jetpack-io/typeid in Rust.

Examples

StaticType prefixes

This is the intended happy path. Using a StaticType implementation, you ensure that the ID being parsed is of the intended type.

```rust use typesafeid::{StaticType, TypeSafeId};

[derive(Default)]

struct User;

impl StaticType for User { // must be lowercase ascii [a-z] only const TYPE: &'static str = "user"; }

// type alias for your custom typed id type UserId = TypeSafeId;

let user_id1 = UserId::new();

std::thread::sleep(std::time::Duration::from_millis(10));

let user_id2 = UserId::new();

let uid1 = userid1.tostring(); let uid2 = userid2.tostring(); dbg!(&uid1, &uid2); assert!(uid2 > uid1, "type safe IDs are ordered");

let userid3: UserId = uid1.parse().expect("invalid user id"); let userid4: UserId = uid2.parse().expect("invalid user id");

asserteq!(userid1.uuid(), userid3.uuid(), "round trip works"); asserteq!(userid2.uuid(), userid4.uuid(), "round trip works"); ```

DynamicType prefixes

If you can't know what the prefix will be, you can use the DynamicType prefix.

```rust use typesafeid::{DynamicType, TypeSafeId};

let id: TypeSafeId = "prefix_01h2xcejqtf2nbrexx3vqjhp41".parse().unwrap();

asserteq!(id.typeprefix(), "prefix"); assert_eq!(id.uuid(), uuid::uuid!("0188bac7-4afa-78aa-bc3b-bd1eef28d881")); ```