UserId

Struct UserId 

Source
pub struct UserId(/* private fields */);
Expand description

User identifier value object for type-safe authentication and authorization

This value object provides type-safe user authentication with authorization management, identity validation, and comprehensive security features. It implements Domain-Driven Design (DDD) value object patterns with multiple user ID format support.

§Key Features

  • Type Safety: Strongly-typed user identifiers that cannot be confused with other string types
  • Authentication: Comprehensive user authentication with format validation
  • Authorization: User-specific permission checking and access control
  • Cross-Platform: Consistent representation across languages and storage systems
  • Security Features: Audit trails, user classification, and domain management
  • Serialization: Full serialization support for storage and API integration

§Benefits Over Raw Strings

  • Type Safety: UserId cannot be confused with other string types
  • Domain Semantics: Clear intent in function signatures and authentication business logic
  • User Validation: Comprehensive validation rules and format checking
  • Future Evolution: Extensible for user-specific methods and security features

§Security Benefits

  • Type Safety: Cannot be confused with other string types in security contexts
  • Validation: Format checking and constraint enforcement for reliable authentication
  • Audit Trails: Clear user action tracking and accountability
  • Authorization: User-specific permission checking and access control

§User ID Formats

  • Email: user@domain.com (most common, normalized to lowercase)
  • Username: username123 (alphanumeric with underscores and hyphens)
  • UUID: 550e8400-e29b-41d4-a716-446655440000 (standard UUID format)
  • System: system-backup (automatically prefixed system accounts)
  • API: api-webhook (automatically prefixed API accounts)

§Use Cases

  • User Authentication: Authenticate users with various ID formats
  • Authorization Management: User-specific permission checking and access control
  • Identity Validation: Comprehensive format validation and constraint enforcement
  • Audit Trails: Track user actions with proper identification

§Usage Examples

§Cross-Language Mapping

  • Rust: UserId newtype wrapper with full validation
  • Go: UserID struct with equivalent interface
  • JSON: String representation for API compatibility
  • Database: TEXT column with validation constraints

Implementations§

Source§

impl UserId

Source

pub fn new(user_id: String) -> Result<Self, PipelineError>

Creates a new user ID with format validation

§Purpose

Creates a type-safe user identifier with comprehensive format validation. Supports email, username, UUID, and system account formats.

§Why

Type-safe user IDs provide:

  • Prevention of authentication errors from invalid formats
  • Clear API contracts for authentication systems
  • Audit trail support with validated identities
  • Domain-based authorization capabilities
§Arguments
  • user_id - User identifier string (email, username, UUID, or system)
§Returns
  • Ok(UserId) - Valid user ID
  • Err(PipelineError::InvalidConfiguration) - Invalid format
§Errors

Returns error when:

  • User ID is empty
  • User ID exceeds 256 characters
  • Contains invalid characters
§Examples
Source

pub fn parse(user_id: &str) -> Result<Self, PipelineError>

Creates a user ID from a string slice

Source

pub fn value(&self) -> &str

Gets the user ID string

Source

pub fn is_email(&self) -> bool

Checks if this is an email format user ID

Source

pub fn is_username(&self) -> bool

Checks if this is a username format user ID

Source

pub fn is_uuid(&self) -> bool

Checks if this is a UUID format user ID

Source

pub fn email_domain(&self) -> Option<&str>

Gets the domain from email format user ID

Source

pub fn email_local(&self) -> Option<&str>

Gets the local part from email format user ID

Source

pub fn belongs_to_domain(&self, domain: &str) -> bool

Checks if user belongs to a specific domain

§Purpose

Validates user’s email domain for domain-based authorization. Used for multi-tenant access control and organization filtering.

§Why

Domain-based authorization enables:

  • Multi-tenant access control
  • Organization-level permissions
  • Domain-specific feature access
  • Email-based user grouping
§Arguments
  • domain - Domain to check (case-insensitive)
§Returns

true if user’s email domain matches (case-insensitive), false otherwise

§Examples
Source

pub fn is_system_user(&self) -> bool

Checks if this is a system user account

§Purpose

Identifies service accounts and system users for special handling. System users typically have elevated permissions and different audit requirements.

§Why

System user detection enables:

  • Service account identification
  • Automated process authentication
  • Different authorization rules
  • Separate audit logging
§Returns

true if user ID starts with: system-, service-, bot-, or api-

§Examples
Source

pub fn is_admin_user(&self) -> bool

Checks if this is an admin user (contains ‘admin’ or ends with ‘-admin’)

Source

pub fn user_type(&self) -> UserType

Gets the user type based on format and content

Source

pub fn validate(&self) -> Result<(), PipelineError>

Validates the user ID

Source

pub fn normalize(&self) -> UserId

Normalizes the user ID (lowercase for emails)

Source§

impl UserId

Predefined user ID builders

Source

pub fn email(email: &str) -> Result<Self, PipelineError>

Creates an email-based user ID

Source

pub fn username(username: &str) -> Result<Self, PipelineError>

Creates a username-based user ID

Source

pub fn uuid(uuid: &str) -> Result<Self, PipelineError>

Creates a UUID-based user ID

Source

pub fn system(name: &str) -> Result<Self, PipelineError>

Creates a system user ID

Source

pub fn api(name: &str) -> Result<Self, PipelineError>

Creates an API user ID

Trait Implementations§

Source§

impl AsRef<str> for UserId

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for UserId

Source§

fn clone(&self) -> UserId

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UserId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for UserId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<UserId> for String

Source§

fn from(user_id: UserId) -> Self

Converts to this type from the input type.
Source§

impl FromStr for UserId

Source§

type Err = PipelineError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for UserId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for UserId

Source§

fn cmp(&self, other: &UserId) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for UserId

Source§

fn eq(&self, other: &UserId) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for UserId

Source§

fn partial_cmp(&self, other: &UserId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for UserId

Source§

impl StructuralPartialEq for UserId

Auto Trait Implementations§

§

impl Freeze for UserId

§

impl RefUnwindSafe for UserId

§

impl Send for UserId

§

impl Sync for UserId

§

impl Unpin for UserId

§

impl UnwindSafe for UserId

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V