uvb-core 0.2.1

Core error types and shared primitives for the UVB authentication platform
Documentation
use serde::{Deserialize, Serialize};
use std::fmt;

/// Type-safe user identifier wrapper
///
/// This newtype provides compile-time guarantees that:
/// 1. User IDs cannot be accidentally mixed with other string types
/// 2. All user-scoped operations explicitly require a UserId
/// 3. Cross-user operations are caught at compile time
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct UserId(String);

impl UserId {
    /// Create a new UserId from a string
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Get the user ID as a string slice
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Consume the UserId and return the inner String
    pub fn into_inner(self) -> String {
        self.0
    }

    /// Validate that the user ID is well-formed
    pub fn is_valid(&self) -> bool {
        !self.0.is_empty() && self.0.len() <= 255
    }
}

impl fmt::Display for UserId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl AsRef<str> for UserId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<String> for UserId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for UserId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<UserId> for String {
    fn from(user_id: UserId) -> Self {
        user_id.0
    }
}

impl PartialEq<str> for UserId {
    fn eq(&self, other: &str) -> bool {
        self.0 == other
    }
}

impl PartialEq<&str> for UserId {
    fn eq(&self, other: &&str) -> bool {
        self.0 == *other
    }
}

impl PartialEq<String> for UserId {
    fn eq(&self, other: &String) -> bool {
        &self.0 == other
    }
}