Skip to main content

slack_rs/api/
types.rs

1//! API types and structures
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Slack API method identifier
7#[derive(Debug, Clone, PartialEq)]
8pub enum ApiMethod {
9    /// Search messages
10    SearchMessages,
11    /// List conversations
12    ConversationsList,
13    /// Get conversation history
14    ConversationsHistory,
15    /// Get user info
16    UsersInfo,
17    /// List users
18    UsersList,
19    /// Post message
20    ChatPostMessage,
21    /// Update message
22    ChatUpdate,
23    /// Delete message
24    ChatDelete,
25    /// Add reaction
26    ReactionsAdd,
27    /// Remove reaction
28    ReactionsRemove,
29}
30
31impl ApiMethod {
32    /// Convert to Slack API method string
33    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    /// Check if this method requires GET request
49    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    /// Check if this is a write operation
61    #[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    /// Check if this is a destructive operation requiring confirmation
74    #[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/// API response with metadata
84#[derive(Debug, Serialize, Deserialize)]
85pub struct ApiResponse {
86    /// Whether the request was successful
87    pub ok: bool,
88    /// Response data
89    #[serde(flatten)]
90    pub data: HashMap<String, serde_json::Value>,
91    /// Error message if ok is false
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub error: Option<String>,
94}
95
96impl ApiResponse {
97    /// Create a successful response
98    #[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    /// Create an error response
108    #[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}