pub struct EntityId<T: Prefix, I: Identifier> { /* private fields */ }Expand description
Generic ID type supporting both UUID and ULID
- Supports UUID (all versions) and ULID.
- Provides thread-safe caching.
- Allows custom delimiters.
- Type-safe through generic parameters.
Implementations§
Source§impl<T: Prefix, I: Identifier> EntityId<T, I>
impl<T: Prefix, I: Identifier> EntityId<T, I>
Sourcepub fn new<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>
pub fn new<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>
Create an EntityId from an identifier string
Accepts any type that can be referenced as a string slice.
Sourcepub fn from_identifier(id: I) -> Self
pub fn from_identifier(id: I) -> Self
Create an EntityId from an existing identifier
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the full ID string with prefix (cached)
Returns the complete entity ID string in the format <prefix><delimiter><identifier>.
For example: “user_123e4567-e89b-12d3-a456-426614174000”
Sourcepub fn id_str(&self) -> &str
pub fn id_str(&self) -> &str
Get the raw identifier string without prefix
Returns just the identifier part of the ID without the prefix and delimiter. For example: “123e4567-e89b-12d3-a456-426614174000”
Sourcepub fn timestamp_ms(&self) -> Option<u64>
pub fn timestamp_ms(&self) -> Option<u64>
Get the timestamp in milliseconds (if available)
Sourcepub fn identifier(&self) -> &I
pub fn identifier(&self) -> &I
Get the underlying identifier object
Returns a reference to the underlying identifier object (UuidIdentifier or UlidIdentifier).
Sourcepub fn builder() -> EntityIdBuilder<T, I>
pub fn builder() -> EntityIdBuilder<T, I>
Create a builder for this entity type
Sourcepub fn from_raw_str<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>
pub fn from_raw_str<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>
Implement TryFrom<&str> for the raw identifier string (without prefix)
This method parses a raw identifier string (UUID or ULID) without the prefix and creates an EntityId with the appropriate prefix.
§Example
use entid::{Prefix, UuidEntityId};
#[derive(Prefix)]
#[entid(prefix = "user")]
struct User;
// Parse a raw UUID string (without the "user_" prefix)
let uuid_str = "123e4567-e89b-12d3-a456-426614174000";
let user_id = UuidEntityId::<User>::from_raw_str(uuid_str).unwrap();
assert_eq!(user_id.as_str(), "user_123e4567-e89b-12d3-a456-426614174000");Sourcepub fn parse_raw_str<S, E, F>(s: S, error_mapper: F) -> Result<Self, E>
pub fn parse_raw_str<S, E, F>(s: S, error_mapper: F) -> Result<Self, E>
Parse a raw identifier string (without prefix) into an EntityId, with custom error handling
This method is similar to from_raw_str, but allows mapping the error to a custom type.
§Example
use entid::{Prefix, UuidEntityId};
#[derive(Prefix)]
#[entid(prefix = "user")]
struct User;
// Parse a raw UUID string with custom error handling
let uuid_str = "123e4567-e89b-12d3-a456-426614174000";
let user_id = UuidEntityId::<User>::parse_raw_str(uuid_str, |e| format!("Invalid UUID: {}", e)).unwrap();Source§impl<T: Prefix> EntityId<T, UuidIdentifier>
impl<T: Prefix> EntityId<T, UuidIdentifier>
Source§impl<T: Prefix> EntityId<T, UlidIdentifier>
impl<T: Prefix> EntityId<T, UlidIdentifier>
Sourcepub fn with_timestamp(timestamp_ms: u64) -> Self
pub fn with_timestamp(timestamp_ms: u64) -> Self
Create a new ULID entity ID with a specific timestamp
Sourcepub fn monotonic_from(previous: Option<&Self>) -> Self
pub fn monotonic_from(previous: Option<&Self>) -> Self
Create a monotonic ULID entity ID based on a previous one
Trait Implementations§
Source§impl<T: Prefix, I: Identifier> AsRef<str> for EntityId<T, I>
Implement AsRef<str> for easy conversion to string slices
impl<T: Prefix, I: Identifier> AsRef<str> for EntityId<T, I>
Implement AsRef<str> for easy conversion to string slices
Source§impl<T: Prefix, I: Identifier> Borrow<str> for EntityId<T, I>
Implement Borrow<str> to allow using EntityId in collections with string keys
impl<T: Prefix, I: Identifier> Borrow<str> for EntityId<T, I>
Implement Borrow<str> to allow using EntityId in collections with string keys
Source§impl<T: Prefix, I: Identifier> Clone for EntityId<T, I>
Implement Clone (Avoids duplicating cached value)
impl<T: Prefix, I: Identifier> Clone for EntityId<T, I>
Implement Clone (Avoids duplicating cached value)
Source§impl<'de, T: Prefix, I: Identifier> Deserialize<'de> for EntityId<T, I>
Deserialization - Reconstruct from stored ID string
impl<'de, T: Prefix, I: Identifier> Deserialize<'de> for EntityId<T, I>
Deserialization - Reconstruct from stored ID string
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T: Prefix, I: Identifier> From<&EntityId<T, I>> for String
Implement Into<String> for references to convert to string representation
impl<T: Prefix, I: Identifier> From<&EntityId<T, I>> for String
Implement Into<String> for references to convert to string representation
Source§impl<T: Prefix> From<&EntityId<T, UlidIdentifier>> for UlidIdentifier
Implement Into<UlidIdentifier> for references to ULID-based EntityId
impl<T: Prefix> From<&EntityId<T, UlidIdentifier>> for UlidIdentifier
Implement Into<UlidIdentifier> for references to ULID-based EntityId
Source§fn from(entity_id: &EntityId<T, UlidIdentifier>) -> Self
fn from(entity_id: &EntityId<T, UlidIdentifier>) -> Self
Source§impl<T: Prefix> From<&EntityId<T, UuidIdentifier>> for UuidIdentifier
Implement Into<UuidIdentifier> for references to UUID-based EntityId
impl<T: Prefix> From<&EntityId<T, UuidIdentifier>> for UuidIdentifier
Implement Into<UuidIdentifier> for references to UUID-based EntityId
Source§fn from(entity_id: &EntityId<T, UuidIdentifier>) -> Self
fn from(entity_id: &EntityId<T, UuidIdentifier>) -> Self
Source§impl<T: Prefix, I: Identifier> From<EntityId<T, I>> for String
Implement Into<String> to convert to string representation
impl<T: Prefix, I: Identifier> From<EntityId<T, I>> for String
Implement Into<String> to convert to string representation
Source§impl<T: Prefix> From<EntityId<T, UlidIdentifier>> for UlidIdentifier
Implement Into<UlidIdentifier> for ULID-based EntityId
impl<T: Prefix> From<EntityId<T, UlidIdentifier>> for UlidIdentifier
Implement Into<UlidIdentifier> for ULID-based EntityId
Source§fn from(entity_id: EntityId<T, UlidIdentifier>) -> Self
fn from(entity_id: EntityId<T, UlidIdentifier>) -> Self
Source§impl<T: Prefix> From<EntityId<T, UuidIdentifier>> for UuidIdentifier
Implement Into<UuidIdentifier> for UUID-based EntityId
impl<T: Prefix> From<EntityId<T, UuidIdentifier>> for UuidIdentifier
Implement Into<UuidIdentifier> for UUID-based EntityId
Source§fn from(entity_id: EntityId<T, UuidIdentifier>) -> Self
fn from(entity_id: EntityId<T, UuidIdentifier>) -> Self
Source§impl<T: Prefix, I: Identifier> From<I> for EntityId<T, I>
Implement From<I> to create an EntityId from an identifier
impl<T: Prefix, I: Identifier> From<I> for EntityId<T, I>
Implement From<I> to create an EntityId from an identifier
Source§impl<T: Prefix, I: Identifier> FromStr for EntityId<T, I>
Implement FromStr for parsing from strings
impl<T: Prefix, I: Identifier> FromStr for EntityId<T, I>
Implement FromStr for parsing from strings
Source§impl<T: Prefix, I: Identifier> Hash for EntityId<T, I>
Implement Hash based on identifier
impl<T: Prefix, I: Identifier> Hash for EntityId<T, I>
Implement Hash based on identifier
This ensures EntityId<T, I> can be used in HashSet or HashMap.
Source§impl<T: Prefix, I: Identifier + Ord> Ord for EntityId<T, I>
impl<T: Prefix, I: Identifier + Ord> Ord for EntityId<T, I>
Source§impl<T: Prefix, I: Identifier> PartialEq for EntityId<T, I>
Implement PartialEq and Eq (Comparison based on identifier)
impl<T: Prefix, I: Identifier> PartialEq for EntityId<T, I>
Implement PartialEq and Eq (Comparison based on identifier)
Source§impl<T: Prefix, I: Identifier + Ord> PartialOrd for EntityId<T, I>
Implement PartialOrd and Ord for lexicographical sorting
impl<T: Prefix, I: Identifier + Ord> PartialOrd for EntityId<T, I>
Implement PartialOrd and Ord for lexicographical sorting
Source§impl<T: Prefix, I: Identifier> Serialize for EntityId<T, I>
Serialization - Store as <prefix><delimiter><id>
impl<T: Prefix, I: Identifier> Serialize for EntityId<T, I>
Serialization - Store as <prefix><delimiter><id>
Source§impl<T: Prefix, I: Identifier> TryFrom<&String> for EntityId<T, I>
Implement TryFrom<&String> for converting from string references
impl<T: Prefix, I: Identifier> TryFrom<&String> for EntityId<T, I>
Implement TryFrom<&String> for converting from string references
Source§impl<T: Prefix, I: Identifier> TryFrom<&str> for EntityId<T, I>
Implement TryFrom<&str> for converting from string slices
impl<T: Prefix, I: Identifier> TryFrom<&str> for EntityId<T, I>
Implement TryFrom<&str> for converting from string slices
Source§impl<T: Prefix, I: Identifier> TryFrom<String> for EntityId<T, I>
Implement TryFrom<String> for converting from owned strings
impl<T: Prefix, I: Identifier> TryFrom<String> for EntityId<T, I>
Implement TryFrom<String> for converting from owned strings