|
| 1 | +use serde::{Serialize, Deserialize}; |
| 2 | +use serde::ser::Serializer; |
| 3 | +use serde::de::{Deserializer, Error}; |
| 4 | + |
| 5 | +#[derive(Debug, Serialize, Deserialize)] |
| 6 | +pub struct UserInfoVO { |
| 7 | + #[serde(serialize_with = "serialize_long")] |
| 8 | + pub id: Option<i64>, // 以字符串形式序列化的 Long 类型 |
| 9 | + |
| 10 | + pub name: Option<String>, // 用户昵称(别名 姓名) |
| 11 | + |
| 12 | + pub position: Option<String>, // 职位 |
| 13 | + |
| 14 | + pub email: Option<String>, // 邮箱 |
| 15 | + |
| 16 | + pub phone_number: Option<String>, // 电话号 |
| 17 | + |
| 18 | + pub description: Option<String>, // 用户个人简介 |
| 19 | + |
| 20 | + pub user_name: Option<String>, // 用户名(登陆的账户) |
| 21 | + |
| 22 | + pub avatar: Option<String>, // 用户头像地址 |
| 23 | + |
| 24 | + pub system_language: Option<String>, // 用户语言 |
| 25 | + |
| 26 | + pub token: Option<String>, // 用户token |
| 27 | + |
| 28 | + pub expire: Option<i64>, // 过期时间(Unix 时间戳) |
| 29 | +} |
| 30 | + |
| 31 | +fn serialize_long<S>(x: &Option<i64>, serializer: S) -> Result<S::Ok, S::Error> |
| 32 | +where |
| 33 | + S: Serializer, |
| 34 | +{ |
| 35 | + // 以字符串形式序列化 Long 类型 |
| 36 | + match *x { |
| 37 | + Some(value) => serializer.serialize_str(&value.to_string()), |
| 38 | + None => serializer.serialize_none(), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl UserInfoVO { |
| 43 | + // 你可以手动实现一个 builder 模式,或者使用一些第三方库来简化这个过程(比如 `derive_builder`)。 |
| 44 | + pub fn builder() -> UserInfoVOBuilder { |
| 45 | + UserInfoVOBuilder::default() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Default)] |
| 50 | +pub struct UserInfoVOBuilder { |
| 51 | + id: Option<i64>, |
| 52 | + name: Option<String>, |
| 53 | + position: Option<String>, |
| 54 | + email: Option<String>, |
| 55 | + phone_number: Option<String>, |
| 56 | + description: Option<String>, |
| 57 | + user_name: Option<String>, |
| 58 | + avatar: Option<String>, |
| 59 | + system_language: Option<String>, |
| 60 | + token: Option<String>, |
| 61 | + expire: Option<i64>, |
| 62 | +} |
| 63 | + |
| 64 | +impl UserInfoVOBuilder { |
| 65 | + pub fn id(mut self, id: i64) -> Self { |
| 66 | + self.id = Some(id); |
| 67 | + self |
| 68 | + } |
| 69 | + |
| 70 | + pub fn name(mut self, name: String) -> Self { |
| 71 | + self.name = Some(name); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + pub fn position(mut self, position: String) -> Self { |
| 76 | + self.position = Some(position); |
| 77 | + self |
| 78 | + } |
| 79 | + |
| 80 | + pub fn email(mut self, email: String) -> Self { |
| 81 | + self.email = Some(email); |
| 82 | + self |
| 83 | + } |
| 84 | + |
| 85 | + pub fn phone_number(mut self, phone_number: String) -> Self { |
| 86 | + self.phone_number = Some(phone_number); |
| 87 | + self |
| 88 | + } |
| 89 | + |
| 90 | + pub fn description(mut self, description: String) -> Self { |
| 91 | + self.description = Some(description); |
| 92 | + self |
| 93 | + } |
| 94 | + |
| 95 | + pub fn user_name(mut self, user_name: String) -> Self { |
| 96 | + self.user_name = Some(user_name); |
| 97 | + self |
| 98 | + } |
| 99 | + |
| 100 | + pub fn avatar(mut self, avatar: String) -> Self { |
| 101 | + self.avatar = Some(avatar); |
| 102 | + self |
| 103 | + } |
| 104 | + |
| 105 | + pub fn system_language(mut self, system_language: String) -> Self { |
| 106 | + self.system_language = Some(system_language); |
| 107 | + self |
| 108 | + } |
| 109 | + |
| 110 | + pub fn token(mut self, token: String) -> Self { |
| 111 | + self.token = Some(token); |
| 112 | + self |
| 113 | + } |
| 114 | + |
| 115 | + pub fn expire(mut self, expire: i64) -> Self { |
| 116 | + self.expire = Some(expire); |
| 117 | + self |
| 118 | + } |
| 119 | + |
| 120 | + pub fn build(self) -> UserInfoVO { |
| 121 | + UserInfoVO { |
| 122 | + id: self.id, |
| 123 | + name: self.name, |
| 124 | + position: self.position, |
| 125 | + email: self.email, |
| 126 | + phone_number: self.phone_number, |
| 127 | + description: self.description, |
| 128 | + user_name: self.user_name, |
| 129 | + avatar: self.avatar, |
| 130 | + system_language: self.system_language, |
| 131 | + token: self.token, |
| 132 | + expire: self.expire, |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments