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
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct ChatMessage {
pub text: String,
pub username: String,
pub bot_id: String,
#[serde(rename = "type")]
pub type_name: String,
pub subtype: String,
pub ts: String,
}
#[derive(Debug, Deserialize)]
pub struct PostMessageResponse {
pub ok: bool,
pub error: Option<String>,
pub channel: Option<String>,
pub ts: Option<String>,
pub message: Option<ChatMessage>,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_de_ok() {
let json = r#"{
"ok": true,
"channel": "random",
"ts": "150",
"message": {
"text": "hello",
"username": "bot",
"bot_id": "id",
"type": "message",
"subtype": "bot_message",
"ts": "150"
}
}"#;
let res = serde_json::from_str::<PostMessageResponse>(json).unwrap();
assert!(res.ok);
assert_eq!(res.channel.unwrap(), "random");
assert_eq!(res.ts.unwrap(), "150");
let message = res.message.unwrap();
assert_eq!(message.text, "hello");
assert_eq!(message.username, "bot");
assert_eq!(message.bot_id, "id");
assert_eq!(message.type_name, "message");
assert_eq!(message.subtype, "bot_message");
assert_eq!(message.ts, "150");
}
#[test]
fn test_de_err() {
let json = r#"{
"ok": false,
"error": "error"
}"#;
let res = serde_json::from_str::<PostMessageResponse>(json).unwrap();
assert!(!res.ok);
assert_eq!(res.error.unwrap(), "error");
}
}