use serde::{Deserialize, Serialize};
use std::fmt;
macro_rules! string_newtype {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct $name(String);
impl $name {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({:?})", stringify!($name), self.0)
}
}
impl From<&str> for $name {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
impl From<String> for $name {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&String> for $name {
fn from(value: &String) -> Self {
Self(value.clone())
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
};
}
string_newtype!(
UserId
);
string_newtype!(
UserName
);
string_newtype!(
MessageId
);
string_newtype!(
ArticleId
);
string_newtype!(
CommentId
);
string_newtype!(
ProfessionId
);
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ApiKey(String);
impl ApiKey {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for ApiKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Debug for ApiKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
write!(f, "ApiKey(<empty>)")
} else {
write!(f, "ApiKey(<redacted:{}>)", self.0.len())
}
}
}
impl From<&str> for ApiKey {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
impl From<String> for ApiKey {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&String> for ApiKey {
fn from(value: &String) -> Self {
Self(value.clone())
}
}
impl AsRef<str> for ApiKey {
fn as_ref(&self) -> &str {
&self.0
}
}