17 unstable releases (3 breaking)

0.4.0 Feb 20, 2025
0.3.3 Feb 17, 2025
0.3.2 Oct 17, 2024
0.2.7 Oct 12, 2024
0.1.3 Oct 6, 2024

#610 in Database interfaces

Download history 9/week @ 2024-12-04 41/week @ 2024-12-11 5/week @ 2025-02-05 110/week @ 2025-02-12 216/week @ 2025-02-19 33/week @ 2025-02-26

364 downloads per month

MIT license

25KB
553 lines

souvenir

A type-safe, tagged identifier library.

The crate primarily provides the Id struct, which stores a 128-bit identifier with its corresponding type (tag). The String representation of an Id is the type's tag and the 128-bit value encoded into a variant of Crockford Base 32.

Here is a simple example of how this crate can be used.

use souvenir::{Type, Id};

struct User;

impl Type for User {
// Specify a prefix for all `Id<User>`
const PREFIX: &'static str = "user";
}

let id: Id<User> = Id::random();
println!("{}", id);

let id2: Id<User> = Id::parse("user_02v58c5a3fy30k560qrtg4rb2k").unwrap();
assert_eq!(id2.to_string(), "user_02v58c5a3fy30k560qrtg4rb2k");

Integrations for various libraries and databases are also (optionally) available:

  • (De)serialization with serde
  • Random ID generation with rand
  • Postgres, MySQL, and Sqlite support with sqlx and diesel

Dependencies

~1–16MB
~223K SLoC