Crate souvenir

Crate souvenir 

Source
Expand description

§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 random value encoded into a variant of Crockford Base 32.

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

use souvenir::Id;

let id: Id = Id::random("user".parse().unwrap());
println!("{}", id);

let id2: Id = Id::parse("user_02v58c5a3fy30k560qrtg4").unwrap();
assert_eq!(id2.to_string(), "user_02v58c5a3fy30k560qrtg4");

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

Modules§

prelude
Re-exports of the most common imports.

Macros§

id
Create an Id based on some literal input. All inputs are verified at compile time to ensure that the Id is valid.
prefix
Create a Prefix based on some literal input. All inputs are verified at compile time to ensure that the Prefix is valid.

Structs§

Id
A 128-bit identifier consisting of a 1-4 character tag and 108 random bits.
Prefix
A valid Id prefix.
Suffix
A valid Id suffix.

Enums§

Error
An enum providing all possible errors generated by this crate.

Constants§

ALPHABET
The alphabet of characters used in the suffix part of an Id.

Traits§

Identifiable
A type which can by identified with an Id.
Tagged
A constant, tagged prefix.

Type Aliases§

IdBytes
Type of the underlying data stored in an Id, which is an array of 16 bytes. Note that not all possible values represent valid identifiers.
Result
A convenience type for Result<T, Error>

Derive Macros§

Identifiable
Automatically implement Identifiable.
Tagged
Automatically implement Tagged.