use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum ApiMethod {
SearchMessages,
ConversationsList,
ConversationsHistory,
ConversationsReplies,
UsersInfo,
UsersList,
ChatPostMessage,
ChatUpdate,
ChatDelete,
ReactionsAdd,
ReactionsRemove,
}
impl ApiMethod {
pub fn as_str(&self) -> &str {
match self {
ApiMethod::SearchMessages => "search.messages",
ApiMethod::ConversationsList => "conversations.list",
ApiMethod::ConversationsHistory => "conversations.history",
ApiMethod::ConversationsReplies => "conversations.replies",
ApiMethod::UsersInfo => "users.info",
ApiMethod::UsersList => "users.list",
ApiMethod::ChatPostMessage => "chat.postMessage",
ApiMethod::ChatUpdate => "chat.update",
ApiMethod::ChatDelete => "chat.delete",
ApiMethod::ReactionsAdd => "reactions.add",
ApiMethod::ReactionsRemove => "reactions.remove",
}
}
pub fn uses_get_method(&self) -> bool {
matches!(
self,
ApiMethod::SearchMessages
| ApiMethod::ConversationsList
| ApiMethod::ConversationsHistory
| ApiMethod::ConversationsReplies
| ApiMethod::UsersInfo
| ApiMethod::UsersList
)
}
#[allow(dead_code)]
pub fn is_write(&self) -> bool {
matches!(
self,
ApiMethod::ChatPostMessage
| ApiMethod::ChatUpdate
| ApiMethod::ChatDelete
| ApiMethod::ReactionsAdd
| ApiMethod::ReactionsRemove
)
}
#[allow(dead_code)]
pub fn is_destructive(&self) -> bool {
matches!(
self,
ApiMethod::ChatDelete | ApiMethod::ChatUpdate | ApiMethod::ReactionsRemove
)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse {
pub ok: bool,
#[serde(flatten)]
pub data: HashMap<String, serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
impl ApiResponse {
#[allow(dead_code)]
pub fn success(data: HashMap<String, serde_json::Value>) -> Self {
Self {
ok: true,
data,
error: None,
}
}
#[allow(dead_code)]
pub fn error(error: String) -> Self {
Self {
ok: false,
data: HashMap::new(),
error: Some(error),
}
}
}