1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, PartialEq)]
8pub enum ApiMethod {
9 SearchMessages,
11 ConversationsList,
13 ConversationsHistory,
15 UsersInfo,
17 UsersList,
19 ChatPostMessage,
21 ChatUpdate,
23 ChatDelete,
25 ReactionsAdd,
27 ReactionsRemove,
29}
30
31impl ApiMethod {
32 pub fn as_str(&self) -> &str {
34 match self {
35 ApiMethod::SearchMessages => "search.messages",
36 ApiMethod::ConversationsList => "conversations.list",
37 ApiMethod::ConversationsHistory => "conversations.history",
38 ApiMethod::UsersInfo => "users.info",
39 ApiMethod::UsersList => "users.list",
40 ApiMethod::ChatPostMessage => "chat.postMessage",
41 ApiMethod::ChatUpdate => "chat.update",
42 ApiMethod::ChatDelete => "chat.delete",
43 ApiMethod::ReactionsAdd => "reactions.add",
44 ApiMethod::ReactionsRemove => "reactions.remove",
45 }
46 }
47
48 pub fn uses_get_method(&self) -> bool {
50 matches!(
51 self,
52 ApiMethod::SearchMessages
53 | ApiMethod::ConversationsList
54 | ApiMethod::ConversationsHistory
55 | ApiMethod::UsersInfo
56 | ApiMethod::UsersList
57 )
58 }
59
60 #[allow(dead_code)]
62 pub fn is_write(&self) -> bool {
63 matches!(
64 self,
65 ApiMethod::ChatPostMessage
66 | ApiMethod::ChatUpdate
67 | ApiMethod::ChatDelete
68 | ApiMethod::ReactionsAdd
69 | ApiMethod::ReactionsRemove
70 )
71 }
72
73 #[allow(dead_code)]
75 pub fn is_destructive(&self) -> bool {
76 matches!(
77 self,
78 ApiMethod::ChatDelete | ApiMethod::ChatUpdate | ApiMethod::ReactionsRemove
79 )
80 }
81}
82
83#[derive(Debug, Serialize, Deserialize)]
85pub struct ApiResponse {
86 pub ok: bool,
88 #[serde(flatten)]
90 pub data: HashMap<String, serde_json::Value>,
91 #[serde(skip_serializing_if = "Option::is_none")]
93 pub error: Option<String>,
94}
95
96impl ApiResponse {
97 #[allow(dead_code)]
99 pub fn success(data: HashMap<String, serde_json::Value>) -> Self {
100 Self {
101 ok: true,
102 data,
103 error: None,
104 }
105 }
106
107 #[allow(dead_code)]
109 pub fn error(error: String) -> Self {
110 Self {
111 ok: false,
112 data: HashMap::new(),
113 error: Some(error),
114 }
115 }
116}