fishpi-sdk 0.1.5

A Rust SDK for interacting with the FishPi community API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::str::FromStr;

use serde_json::Value;

use crate::{impl_str_enum, utils::error::Error};

/// 猜拳类型
#[derive(Clone, Debug)]
#[repr(u8)]
pub enum GestureType {
    ///    Rock = 0,
    /// 剪刀
    Scissors = 1,
    ///    Paper = 2,
}

/// 红包类型
#[derive(Clone, Debug)]
pub enum RedPacketType {
    /// 拼手气
    Random,
    /// 平分
    Average,
    /// 专属
    Specify,
    /// 心跳
    Heartbeat,
    /// 猜拳
    RockPaperScissors,
}

/// 红包数据
#[derive(Clone, Debug)]
pub struct RedPacket {
    /// 红包类型
    pub r#type: RedPacketType,
    /// 红包积分
    pub money: u32,
    /// 红包个数
    pub count: u32,
    /// 祝福语
    pub msg: String,
    /// 接收者, 专属红包有效
    pub recivers: Vec<String>,
    /// 出拳, 猜拳红包有效
    pub gesture: Option<GestureType>,
}

/// 红包领取者信息
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct RedPacketGot {
    /// 用户ID
    pub userId: String,
    /// 用户名
    pub userName: String,
    /// 用户头像
    pub avatar: String,
    /// 领取到的积分
    pub userMoney: u32,
    /// 领取时间
    pub time: String,
}

/// 红包历史信息
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct RedPacketMessage {
    /// 消息类型,固定为redPacket
    pub msgType: String,
    /// 红包数
    pub count: u32,
    /// 领取数
    pub got: u32,
    /// 内含积分
    pub money: u32,
    /// 祝福语
    pub msg: String,
    /// 发送者ID
    pub senderId: String,
    /// 出拳,猜拳红包有效
    pub GestureType: Option<GestureType>,
    /// 接收者,专属红包有效
    pub recivers: Vec<String>,
    /// 已领取者列表
    pub who: Vec<RedPacketGot>,
}

/// 红包基本信息
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct RedPacketBase {
    /// 数量
    pub count: u32,
    /// 猜拳类型
    pub gesture: Option<GestureType>,
    /// 领取数
    pub got: u32,
    /// 祝福语
    pub msg: String,
    /// 发送者用户名
    pub userName: String,
    /// 用户头像
    pub userAvatarURL: String,
}

/// 红包信息
#[derive(Clone, Debug)]
pub struct RedPacketInfo {
    pub info: RedPacketBase,
    pub recivers: Vec<String>,
    pub who: Vec<RedPacketGot>,
}

/// 红包状态信息
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
pub struct RedPacketStatusMsg {
    pub oId: String,
    pub count: u32,
    pub got: u32,
    pub whoGive: String,
    pub whoGot: Vec<String>,
    pub avatarURL20: String,
    pub avatarURL48: String,
    pub avatarURL210: String,
}

impl RedPacketStatusMsg {
    pub fn from_value(data: &Value) -> Result<Self, Error> {
        Ok(RedPacketStatusMsg {
            oId: data["oId"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing oId in RedPacketStatusMsg".to_string()))?
                .to_string(),
            count: data["count"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid count in RedPacketStatusMsg".to_string())
            })? as u32,
            got: data["got"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid got in RedPacketStatusMsg".to_string())
            })? as u32,
            whoGive: data["whoGive"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing whoGive in RedPacketStatusMsg".to_string()))?
                .to_string(),
            whoGot: if let Some(who_got_array) = data["whoGot"].as_array() {
                who_got_array
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            } else {
                data["whoGot"]
                    .as_str()
                    .map(|s| vec![s.to_string()])
                    .unwrap_or_default()
            },
            avatarURL20: data["userAvatarURL20"]
                .as_str()
                .ok_or_else(|| {
                    Error::Parse("Missing userAvatarURL20 in RedPacketStatusMsg".to_string())
                })?
                .to_string(),
            avatarURL48: data["userAvatarURL48"]
                .as_str()
                .ok_or_else(|| {
                    Error::Parse("Missing userAvatarURL48 in RedPacketStatusMsg".to_string())
                })?
                .to_string(),
            avatarURL210: data["userAvatarURL210"]
                .as_str()
                .ok_or_else(|| {
                    Error::Parse("Missing userAvatarURL210 in RedPacketStatusMsg".to_string())
                })?
                .to_string(),
        })
    }
}

impl Default for RedPacket {
    fn default() -> Self {
        RedPacket {
            r#type: RedPacketType::Random,
            money: 32,
            count: 1,
            msg: "摸鱼者, 事竟成!".to_string(),
            recivers: Vec::new(),
            gesture: None,
        }
    }
}

impl RedPacket {
    pub fn from_value(data: &Value) -> Result<Self, Error> {
        Ok(RedPacket {
            r#type: RedPacketType::from_str(
                data["type"]
                    .as_str()
                    .ok_or_else(|| Error::Parse("Missing type in RedPacket".to_string()))?,
            )
            .map_err(|_| Error::Parse("Invalid type in RedPacket".to_string()))?,
            money: data["money"]
                .as_u64()
                .ok_or_else(|| Error::Parse("Missing or invalid money in RedPacket".to_string()))?
                as u32,
            count: data["count"]
                .as_u64()
                .ok_or_else(|| Error::Parse("Missing or invalid count in RedPacket".to_string()))?
                as u32,
            msg: data["msg"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing msg in RedPacket".to_string()))?
                .to_string(),
            recivers: if let Some(recivers_array) = data["recivers"].as_array() {
                recivers_array
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            } else {
                Vec::new()
            },
            gesture: if let Some(gesture_str) = data["gesture"].as_str() {
                Some(
                    GestureType::from_str(gesture_str)
                        .map_err(|_| Error::Parse("Invalid gesture in RedPacket".to_string()))?,
                )
            } else {
                None
            },
        })
    }
}

impl RedPacketMessage {
    pub fn from_value(data: &Value) -> Result<Self, Error> {
        Ok(RedPacketMessage {
            msgType: data["msgType"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing msgType in RedPacketMessage".to_string()))?
                .to_string(),
            count: data["count"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid count in RedPacketMessage".to_string())
            })? as u32,
            got: data["got"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid got in RedPacketMessage".to_string())
            })? as u32,
            money: data["money"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid money in RedPacketMessage".to_string())
            })? as u32,
            msg: data["msg"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing msg in RedPacketMessage".to_string()))?
                .to_string(),
            senderId: data["senderId"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing senderId in RedPacketMessage".to_string()))?
                .to_string(),
            GestureType: if let Some(gesture_str) = data["gesture"].as_str() {
                Some(
                    GestureType::from_str(gesture_str).map_err(|_| {
                        Error::Parse("Invalid gesture in RedPacketMessage".to_string())
                    })?,
                )
            } else {
                None
            },
            recivers: if let Some(recivers_array) = data["recivers"].as_array() {
                recivers_array
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            } else {
                Vec::new()
            },
            who: if let Some(who_array) = data["who"].as_array() {
                let mut got_list = Vec::new();
                for item in who_array {
                    got_list.push(RedPacketGot {
                        userId: item["userId"]
                            .as_str()
                            .ok_or_else(|| Error::Parse("Missing userId in who".to_string()))?
                            .to_string(),
                        userName: item["userName"]
                            .as_str()
                            .ok_or_else(|| Error::Parse("Missing userName in who".to_string()))?
                            .to_string(),
                        avatar: item["avatar"]
                            .as_str()
                            .ok_or_else(|| Error::Parse("Missing avatar in who".to_string()))?
                            .to_string(),
                        userMoney: item["userMoney"].as_u64().ok_or_else(|| {
                            Error::Parse("Missing or invalid userMoney in who".to_string())
                        })? as u32,
                        time: item["time"]
                            .as_str()
                            .ok_or_else(|| Error::Parse("Missing time in who".to_string()))?
                            .to_string(),
                    });
                }
                got_list
            } else {
                Vec::new()
            },
        })
    }
}

impl RedPacketBase {
    pub fn from_value(data: &Value) -> Result<Self, Error> {
        Ok(RedPacketBase {
            count: data["count"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid count in RedPacketBase".to_string())
            })? as u32,
            gesture: if let Some(gesture_str) = data["gesture"].as_str() {
                Some(
                    GestureType::from_str(gesture_str).map_err(|_| {
                        Error::Parse("Invalid gesture in RedPacketBase".to_string())
                    })?,
                )
            } else {
                None
            },
            got: data["got"].as_u64().ok_or_else(|| {
                Error::Parse("Missing or invalid got in RedPacketBase".to_string())
            })? as u32,
            msg: data["msg"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing msg in RedPacketBase".to_string()))?
                .to_string(),
            userName: data["userName"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing userName in RedPacketBase".to_string()))?
                .to_string(),
            userAvatarURL: data["userAvatarURL"]
                .as_str()
                .ok_or_else(|| Error::Parse("Missing userAvatarURL in RedPacketBase".to_string()))?
                .to_string(),
        })
    }
}

impl RedPacketInfo {
    pub fn from_value(data: &Value) -> Result<Self, Error> {
        let info_data = &data["info"];
        let info = RedPacketBase::from_value(info_data)?;

        let recivers = if let Some(recivers_array) = data["recivers"].as_array() {
            recivers_array
                .iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        } else {
            Vec::new()
        };

        let who = if let Some(who_array) = data["who"].as_array() {
            let mut got_list = Vec::new();
            for item in who_array {
                got_list.push(RedPacketGot {
                    userId: item["userId"]
                        .as_str()
                        .ok_or_else(|| Error::Parse("Missing userId in who".to_string()))?
                        .to_string(),
                    userName: item["userName"]
                        .as_str()
                        .ok_or_else(|| Error::Parse("Missing userName in who".to_string()))?
                        .to_string(),
                    avatar: item["avatar"]
                        .as_str()
                        .ok_or_else(|| Error::Parse("Missing avatar in who".to_string()))?
                        .to_string(),
                    userMoney: item["userMoney"].as_u64().ok_or_else(|| {
                        Error::Parse("Missing or invalid userMoney in who".to_string())
                    })? as u32,
                    time: item["time"]
                        .as_str()
                        .ok_or_else(|| Error::Parse("Missing time in who".to_string()))?
                        .to_string(),
                });
            }
            got_list
        } else {
            Vec::new()
        };

        Ok(RedPacketInfo {
            info,
            recivers,
            who,
        })
    }
}

impl_str_enum!(GestureType {
    Rock => "石头",
    Scissors => "剪刀",
    Paper => "",
});

impl_str_enum!(RedPacketType {
    Random => "random",
    Average => "average",
    Specify => "specify",
    Heartbeat => "heartbeat",
    RockPaperScissors => "rockPaperScissors",
});