Skip to main content

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 (cuid feature) - Compact, collision-resistant IDs.
    • ULID (ulid feature) - Universally unique identifiers.
    • UUID (uuid feature) - Universally unique identifiers.
    • Snowflake (snowflake feature) - Time-based, distributed IDs.
  • Entity Labeling: Labels provide contextual meaning to identifiers.
  • Serialization & Database Support:
    • serde integration for JSON and binary serialization.
    • [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 = "1.0", features = ["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.
"cuid"Enables the CuidGenerator for CUID-based IDs.
"ulid"Enables the [UlidGenerator] for ULID-based IDs.
"uuid"Enables the [UuidGenerator] for UUID-based IDs.
"snowflake"Enables the [SnowflakeGenerator] for distributed IDs.
"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.

Re-exports§

pub use id::Entity;
pub use id::Id;
pub use id::IdGenerator;
pub use id::LabelMode;
pub use id::Labeled;
pub use id::Provenance;
pub use id::Sourced;
pub use id::CuidGenerator;
pub use id::CuidId;

Modules§

id

Structs§

CustomLabeling
A user-defined labeling mechanism.
MakeLabeling
A labeling implementation that derives labels from Rust type names.
NoLabeling
A marker type representing the absence of a label.

Enums§

LabelPolicy
Controls how IDs are displayed to humans (via .labeled()).

Constants§

DELIMITER

Traits§

Label
Trait for types that can provide a labeling mechanism.
Labeling
A trait representing types that can provide a label.