Crate tagid

Crate tagid 

Source
Expand description

§tagid - Typed Unique Identifiers for Rust Entities

tagid provides a robust system for defining and managing typed unique identifiers in Rust. It supports multiple ID generation strategies (CUID, UUID, Snowflake) and integrates with serde, sqlx, and other frameworks for seamless use in databases and serialization.

§Features

  • Typed Identifiers: Define entity-specific IDs with compile-time safety.
  • Multiple ID Generators:
    • CUID (with-cuid feature) - Compact, collision-resistant IDs.
    • ULID (with-ulid feature) - Universally unique identifiers.
    • UUID (with-uuid feature) - Universally unique identifiers.
    • Snowflake (with-snowflake feature) - Time-based, distributed IDs.
  • Entity Labeling: Labels provide contextual meaning to identifiers.
  • Serialization & Database Support:
    • serde integration for JSON and binary serialization (serde feature).
    • [sqlx] integration for database storage (sqlx feature).
  • Custom Labeling: Define custom label formats for entities.

§Installation

Add tagid to your Cargo.toml, enabling the desired features:

[dependencies]
tagid = { version = "0.1", features = ["with-uuid", "sqlx"] }

§Usage

§Defining an Entity with a Typed ID

use tagid::{Entity, Id, Label};

#[derive(Label)]
struct User;

impl Entity for User {
    type IdGen = tagid::UuidGenerator;
}

let user_id = User::next_id();
println!("User ID: {}", user_id);

§Labeling System

Labels help associate an identifier with an entity, improving clarity in logs and databases:

use tagid::{Label, Labeling};
use tagid::snowflake::pretty::{IdPrettifier, BASE_23};
IdPrettifier::global_initialize(BASE_23.clone());

#[derive(Label)]
struct Order;

let order_labeler = Order::labeler();
let order_label = order_labeler.label();
assert_eq!(order_label, "Order");

§Features Overview

FeatureDescription
"derive"Enables #[derive(Label)] macro for automatic labeling.
"with-cuid"Enables the CuidGenerator for CUID-based IDs.
"with-ulid"Enables the [UlidGenerator] for ULID-based IDs.
"with-uuid"Enables the [UuidGenerator] for UUID-based IDs.
"with-snowflake"Enables the [SnowflakeGenerator] for distributed IDs.
"serde"Enables serialization support via serde.
"sqlx"Enables database integration via sqlx.
"envelope"Provides an envelope struct for wrapping IDs with metadata.

§Contributing

Contributions are welcome! Open an issue or submit a pull request on GitHub.

§License

This project is licensed under the MIT License.

Structs§

CuidGenerator
A generator for creating CUID-based unique identifiers.
CustomLabeling
A user-defined labeling mechanism.
Id
A struct representing an identifier for an entity, and supports id labeling in logs and other output.
MakeLabeling
A labeling implementation that derives labels from Rust type names.
NoLabeling
A marker type representing the absence of a label.

Constants§

DELIMITER

Traits§

Entity
A trait for entities that have a unique identifier.
IdGenerator
A trait for generating unique identifiers.
Label
Trait for types that can provide a labeling mechanism.
Labeling
A trait representing types that can provide a label.

Type Aliases§

CuidId
A type alias for a CUID-based identifier wrapped in the Id struct.