id
k to z.struct Primitive
pub struct Primitive([u8; 16]);A 128-bit identifier encoded as a 32-character string using the k-z alphabet.
Each byte is split into two nibbles and mapped through a reverse-hex alphabet,
producing a lowercase, URL-safe string that sorts in reverse byte order.
The encoding is bijective: every valid 32-character [k-z] string decodes
to exactly one [u8; 16] and vice-versa.
Examples
let id = Id::new();
assert_eq!(id.to_string().len(), 32);
let parsed: Id = id.to_string().parse().unwrap();
assert_eq!(id, parsed);Implements Clone, Debug, Deserialize<'de>, Eq, Hash, PartialEq, Serialize, Default, Display, From<Primitive>, FromStr, From<String>, From<&str>.
const Primitive::REVERSE_HEX_CHARS
const REVERSE_HEX_CHARS: &[u8; 16] = b"zyxwvutsrqponmlk";Alphabet used to encode bytes into the k-z character range.
Primitive::new
pub fn new() -> SelfGenerate a new random Id using the thread-local RNG.
| Parameter | Type | Description |
|---|---|---|
| returns | Self | An id of 16 random bytes. |
Primitive::validate_prefix
pub fn validate_prefix(s: &str) -> Result<&str, String>Validate that s is a valid ID prefix: 1-32 characters, all in [k-z].
| Parameter | Type | Description |
|---|---|---|
s |
&str | the prefix to check |
| returns | Result<&str, String> | Returns Ok(s) on success so callers can chain the result. Rejects empty strings, strings longer than 32 characters, and any character outside the ID alphabet. The error message says what is wrong. |
Primitive::short
pub fn short(&self) -> StringReturns the first 8 characters of the encoded ID as a short prefix.
| Parameter | Type | Description |
|---|---|---|
| returns | String | The characters that encode the first 4 bytes. |
Primitive::decode
fn decode(s: &str) -> Result<[u8; 16], String>Decode a 32-character [k-z] string back into 16 raw bytes.
| Parameter | Type | Description |
|---|---|---|
s |
&str | the encoded id |
| returns | Result<[u8; 16], String> | The bytes, or an error message when s is not 32 bytes long or holds a character outside k-z. |
Primitive::encode
fn encode(bytes: &[u8; 16]) -> StringEncode 16 raw bytes into a 32-character [k-z] string.
| Parameter | Type | Description |
|---|---|---|
bytes |
&[u8; 16] | the raw id |
| returns | String | The encoded id, two characters per byte, high nibble first. |
Primitive::encode_prefix
fn encode_prefix(bytes: &[u8; 16]) -> StringEncode only the first 4 bytes into an 8-character string for Primitive::short.
| Parameter | Type | Description |
|---|---|---|
bytes |
&[u8; 16] | the raw id |
| returns | String | The 8-character prefix. |
Primitive::nibble_from_char
fn nibble_from_char(c: u8) -> Result<u8, String>Convert a single [k-z] character byte to its nibble value (0..=15).
| Parameter | Type | Description |
|---|---|---|
c |
u8 | the character, as a byte |
| returns | Result<u8, String> | The nibble, where z is 0 and k is 15, or an error message for any other byte. |