#unique-id #prefix #string #generate #ids #nanoid #nano

prefix_id

A library for generating unique IDs nano ids with a prefix

3 stable releases

1.0.2 Aug 12, 2024
1.0.1 Jun 9, 2024

#3 in #nanoid

Apache-2.0

11KB
235 lines

prefix_id

All the magic from nanoid but with a prefix

Inspired by nanoid

Installation

cargo add prefix_id

Usage

create_id!(MyEntityId, "id");

let id = MyEntityId::new(); // => "id_1234567890avjfwdnfvqdp"

// then you can use the impl methods

id.as_str(); // => "id_1234567890avjfwdnfvqdp"
let my_string: String = id.into();

// you can also validate/convert it from string

let str = "id_1234567890avjfwdnfvqdp";
let id = MyEntityId::from_str(str).unwrap();

// you can optionally set the feature `serde` flag to serialize/deserialize

let serialized = serde_json::to_string(&id).unwrap();
let deserialized = serde_json::from_str::<MyEntityId>(&serialized).unwrap();

License

Licensed under Apache License, Version 2.0

Dependencies

~0.6–1.7MB
~35K SLoC