Skip to main content

nodedb_types/id/
id_type.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Enum representing the detected or assigned format of a string-based ID.
4
5/// The format of a detected or generated string-based identifier.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum IdType {
8    /// A UUID (any version) in the standard `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` format.
9    Uuid,
10    /// A ULID — 26-character Crockford Base32, time-sortable.
11    Ulid,
12    /// A CUID2 — lowercase alphanumeric string, 4–32 characters, starts with a letter.
13    Cuid2,
14    /// A NanoID — URL-safe alphanumeric + `_-`, 10–64 characters.
15    NanoId,
16    /// Any other identifier format that does not match the known patterns.
17    Custom,
18}
19
20impl IdType {
21    /// Return the canonical string name used in SQL function output and JSON responses.
22    pub fn as_str(&self) -> &'static str {
23        match self {
24            IdType::Uuid => "uuid",
25            IdType::Ulid => "ulid",
26            IdType::Cuid2 => "cuid2",
27            IdType::NanoId => "nanoid",
28            IdType::Custom => "custom",
29        }
30    }
31}