Skip to main content

dingtalk_stream/frames/
mod.rs

1//! Frames module for DingTalk Stream SDK
2//!
3//! Contains all message types and structures for communication with DingTalk
4
5use serde::{Deserialize, Serialize};
6use std::ops::Deref;
7use std::sync::Arc;
8use crate::frames::down_message::MessageHeaders;
9
10pub mod down_message;
11pub mod up_message;
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct DingTalkUserId(pub String);
15
16impl<S: Into<String>> From<S> for DingTalkUserId {
17    fn from(value: S) -> Self {
18        Self(value.into())
19    }
20}
21
22impl Deref for DingTalkUserId {
23    type Target = str;
24
25    fn deref(&self) -> &Self::Target {
26        &self.0
27    }
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
31pub struct DingTalkPrivateConversationId(pub String);
32
33impl<S: Into<String>> From<S> for DingTalkPrivateConversationId {
34    fn from(value: S) -> Self {
35        Self(value.into())
36    }
37}
38impl Deref for DingTalkPrivateConversationId {
39    type Target = str;
40
41    fn deref(&self) -> &Self::Target {
42        &self.0
43    }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
47pub struct DingTalkGroupConversationId(pub String);
48
49impl<S: Into<String>> From<S> for DingTalkGroupConversationId {
50    fn from(value: S) -> Self {
51        Self(value.into())
52    }
53}
54impl Deref for DingTalkGroupConversationId {
55    type Target = str;
56
57    fn deref(&self) -> &Self::Target {
58        &self.0
59    }
60}
61
62/// ACK message status codes
63pub mod ack_status {
64    pub const OK: i32 = 200;
65    pub const BAD_REQUEST: i32 = 400;
66    pub const NOT_IMPLEMENTED: i32 = 404;
67    pub const SYSTEM_EXCEPTION: i32 = 500;
68}
69
70/// ACK status constants for convenience
71pub const OK: i32 = ack_status::OK;
72pub const BAD_REQUEST: i32 = ack_status::BAD_REQUEST;
73pub const NOT_IMPLEMENTED: i32 = ack_status::NOT_IMPLEMENTED;
74pub const SYSTEM_EXCEPTION: i32 = ack_status::SYSTEM_EXCEPTION;
75
76/// ACK message for responding to messages
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct AckMessage {
79    pub code: i32,
80    pub headers: MessageHeaders,
81    #[serde(rename = "message")]
82    pub message: String,
83    pub data: Option<String>,
84}
85
86impl AckMessage {
87    pub fn ok(message: &str) -> Self {
88        Self {
89            code: OK,
90            headers: MessageHeaders::new(),
91            message: message.to_string(),
92            data: None,
93        }
94    }
95
96    pub fn error(code: i32, message: &str) -> Self {
97        Self {
98            code,
99            headers: MessageHeaders::new(),
100            message: message.to_string(),
101            data: None,
102        }
103    }
104
105    pub fn with_message_id(mut self, message_id: String) -> Self {
106        self.headers.message_id = Some(message_id);
107        self
108    }
109
110    pub fn with_content_type(mut self, content_type: &str) -> Self {
111        self.headers.content_type = Some(content_type.to_string());
112        self
113    }
114
115    pub fn with_data(mut self, data: serde_json::Value) -> Self {
116        self.data = Some(serde_json::to_string(&data).unwrap_or_default());
117        self
118    }
119
120    pub fn response_data(data: serde_json::Value) -> Self {
121        let response = serde_json::json!({ "response": data });
122        Self {
123            code: OK,
124            headers: MessageHeaders::new().with_content_type("application/json"),
125            message: "OK".to_string(),
126            data: Some(serde_json::to_string(&response).unwrap_or_default()),
127        }
128    }
129}
130
131pub type SendMessageCallbackFn =
132    dyn Fn(Result<SendMessageCallbackData, anyhow::Error>) + Send + Sync + 'static;
133#[derive(Clone)]
134pub struct SendMessageCallback(Arc<SendMessageCallbackFn>);
135
136impl Deref for SendMessageCallback {
137    type Target = SendMessageCallbackFn;
138
139    fn deref(&self) -> &Self::Target {
140        &*self.0
141    }
142}
143
144impl<F> From<F> for SendMessageCallback
145where
146    F: Fn(Result<SendMessageCallbackData, anyhow::Error>) + Send + Sync + 'static,
147{
148    fn from(value: F) -> Self {
149        SendMessageCallback(Arc::new(value))
150    }
151}
152
153#[derive(Clone, Default)]
154pub struct OptionSendMessageCallback(Option<SendMessageCallback>);
155
156impl<T: Into<SendMessageCallback>> From<T> for OptionSendMessageCallback {
157    fn from(value: T) -> Self {
158        Self(Some(value.into()))
159    }
160}
161
162impl Deref for OptionSendMessageCallback {
163    type Target = Option<SendMessageCallback>;
164
165    fn deref(&self) -> &Self::Target {
166        &self.0
167    }
168}
169
170#[derive(Debug, Clone)]
171pub struct SendMessageCallbackData {
172    pub http_status: u16,
173    pub text: String,
174}