use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct UserId(String);
impl UserId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
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
}
}