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.
use ;
;
// type alias for your custom typed id
type UserId = ;
let user_id1 = new;
# sleep;
let user_id2 = new;
let uid1 = user_id1.to_string;
let uid2 = user_id2.to_string;
dbg!;
assert!;
let user_id3: UserId = uid1.parse.expect;
let user_id4: UserId = uid2.parse.expect;
assert_eq!;
assert_eq!;
DynamicType prefixes
If you can't know what the prefix will be, you can use the DynamicType prefix.
use ;
let id: = "prefix_01h2xcejqtf2nbrexx3vqjhp41".parse.unwrap;
assert_eq!;
assert_eq!;
Help
error: evaluation of `<Index as StaticType>__TYPE_PREFIX_IS_VALID` failed
-/Users/conrad/Documents/code/type-safe-id/src/lib.rs:76:13
|
76 | assert!;
This compiler error suggests that your static type prefix is not valid because it contains non ascii-lowercase values.
error: evaluation of `<Index as StaticType>__TYPE_PREFIX_IS_VALID` failed
-/Users/conrad/Documents/code/type-safe-id/src/lib.rs:73:9
|
73 | assert!;
This compiler error suggests that your static type prefix is not valid because it contains more than 63 characters.