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