Skip to main content

fishpi_sdk/
ids.rs

1//! 强类型 ID,编译期防止传错参数。
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6macro_rules! string_newtype {
7    ($(#[$meta:meta])* $name:ident) => {
8        $(#[$meta])*
9        #[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
10        #[serde(transparent)]
11        pub struct $name(String);
12
13        impl $name {
14            pub fn new(value: impl Into<String>) -> Self {
15                Self(value.into())
16            }
17
18            pub fn as_str(&self) -> &str {
19                &self.0
20            }
21
22            pub fn into_string(self) -> String {
23                self.0
24            }
25        }
26
27        impl fmt::Display for $name {
28            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29                f.write_str(&self.0)
30            }
31        }
32
33        impl fmt::Debug for $name {
34            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35                write!(f, "{}({:?})", stringify!($name), self.0)
36            }
37        }
38
39        impl From<&str> for $name {
40            fn from(value: &str) -> Self {
41                Self(value.to_string())
42            }
43        }
44
45        impl From<String> for $name {
46            fn from(value: String) -> Self {
47                Self(value)
48            }
49        }
50
51        impl From<&String> for $name {
52            fn from(value: &String) -> Self {
53                Self(value.clone())
54            }
55        }
56
57        impl AsRef<str> for $name {
58            fn as_ref(&self) -> &str {
59                &self.0
60            }
61        }
62    };
63}
64
65string_newtype!(
66    /// 用户 ID(服务端 `oId`)
67    UserId
68);
69string_newtype!(
70    /// 用户名(登录名,用于 URL 路径)
71    UserName
72);
73string_newtype!(
74    /// 聊天/私聊消息 ID
75    MessageId
76);
77string_newtype!(
78    /// 文章 ID
79    ArticleId
80);
81string_newtype!(
82    /// 评论 ID
83    CommentId
84);
85string_newtype!(
86    /// 职业 ID
87    ProfessionId
88);
89
90/// 认证 token,`Debug` 脱敏。
91#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
92#[serde(transparent)]
93pub struct ApiKey(String);
94
95impl ApiKey {
96    pub fn new(value: impl Into<String>) -> Self {
97        Self(value.into())
98    }
99
100    pub fn as_str(&self) -> &str {
101        &self.0
102    }
103
104    pub fn into_string(self) -> String {
105        self.0
106    }
107
108    pub fn is_empty(&self) -> bool {
109        self.0.is_empty()
110    }
111}
112
113impl fmt::Display for ApiKey {
114    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115        f.write_str(&self.0)
116    }
117}
118
119impl fmt::Debug for ApiKey {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        if self.0.is_empty() {
122            write!(f, "ApiKey(<empty>)")
123        } else {
124            write!(f, "ApiKey(<redacted:{}>)", self.0.len())
125        }
126    }
127}
128
129impl From<&str> for ApiKey {
130    fn from(value: &str) -> Self {
131        Self(value.to_string())
132    }
133}
134
135impl From<String> for ApiKey {
136    fn from(value: String) -> Self {
137        Self(value)
138    }
139}
140
141impl From<&String> for ApiKey {
142    fn from(value: &String) -> Self {
143        Self(value.clone())
144    }
145}
146
147impl AsRef<str> for ApiKey {
148    fn as_ref(&self) -> &str {
149        &self.0
150    }
151}