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 conversation replies (thread messages)
16    ConversationsReplies,
17    /// Get user info
18    UsersInfo,
19    /// List users
20    UsersList,
21    /// Post message
22    ChatPostMessage,
23    /// Update message
24    ChatUpdate,
25    /// Delete message
26    ChatDelete,
27    /// Add reaction
28    ReactionsAdd,
29    /// Remove reaction
30    ReactionsRemove,
31}
32
33impl ApiMethod {
34    /// Convert to Slack API method string
35    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    /// Check if this method requires GET request
52    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    /// Check if this is a write operation
65    #[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    /// Check if this is a destructive operation requiring confirmation
78    #[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/// API response with metadata
88#[derive(Debug, Serialize, Deserialize)]
89pub struct ApiResponse {
90    /// Whether the request was successful
91    pub ok: bool,
92    /// Response data
93    #[serde(flatten)]
94    pub data: HashMap<String, serde_json::Value>,
95    /// Error message if ok is false
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub error: Option<String>,
98}
99
100impl ApiResponse {
101    /// Create a successful response
102    #[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    /// Create an error response
112    #[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}