Module nid::alphabet

source ·
Expand description

This module defines the Alphabet trait and provides implementations for the most common alphabets used in Nano ID.

An alphabet is a set of symbols that can be used in Nano ID. In this crate, only ASCII characters can be used as symbols.

The default alphabet used in Nano ID is Base64UrlAlphabet, which contains A-Za-z0-9_- symbols.

§Implementing a custom alphabet

To implement a custom alphabet, you need to create a new type that implements the Alphabet trait.

§Examples

use nid::{alphabet::{Base36Alphabet, Base58Alphabet}, Nanoid};

// Use the default Base64URL alphabet, which contains `A-Za-z0-9_-` symbols.
type ShopId = Nanoid<9>;
let id: ShopId = Nanoid::new();
let id: ShopId = "kP_IH1DPM".parse()?;

// Use Base36 alphabet, which contains `A-Z0-9` symbols.
type UserId = Nanoid<21, Base36Alphabet>;
let id: UserId = Nanoid::new();
let id: UserId = "NDBIZRQSB6OGXJS06AN5L".parse()?;

// Use Base58 alphabet, which contains `A-Za-z0-9` symbols excluding `0OlI`.
type ItemId = Nanoid<16, Base58Alphabet>;
let id: ItemId = Nanoid::new();
let id: ItemId = "96MrjhHuWXJMLCKh".parse()?;

Structs§

Traits§

  • A set of symbols that can be used in Nano ID. In this crate, only ASCII characters can be used as symbols.