mirai_ws/protocol/
common.rs

1//! # common data structure
2//! 
3//! 
4//! 
5//! 
6//! 
7//! 
8//! 
9//! 
10//! 
11//! 
12//! 
13//!
14//!
15//!
16//!
17//!
18//!
19//!
20//!
21//!
22//!
23//!
24//!
25//!
26//!
27//!
28//!
29//!
30//!
31//!
32
33
34use serde::{Serialize, Deserialize};
35#[derive(Serialize, Deserialize, Debug)]
36pub struct PersonalSender {
37    pub id: i64,
38    pub nickname: String,
39    pub remark: String,
40}
41
42#[derive(Serialize, Deserialize, Debug)]
43#[serde(rename_all = "UPPERCASE")]
44pub enum GroupPermisson {
45    Member,
46    Administrator,
47    Owner,
48}
49
50#[derive(Serialize, Deserialize, Debug)]
51pub struct GroupInfo {
52    pub id: i64,
53    pub name: String,
54    pub permission: GroupPermisson
55}
56
57#[derive(Serialize, Deserialize, Debug)]
58#[serde(rename_all = "camelCase")]
59pub struct GroupSender {
60    pub id: i64,
61    pub member_name: String,
62    pub special_title: String,
63    pub permission: GroupPermisson,
64    pub join_timestamp: i64,
65    pub last_speak_timestamp: i64,
66    pub mute_time_remaining: i64,
67    pub group: GroupInfo
68}
69
70#[derive(Serialize, Deserialize, Debug)]
71#[serde(rename_all = "UPPERCASE")]
72pub enum ClientDevice {
73    Windows,
74    Mobile,
75    // for the devices those 我也知不道的
76    Unknown
77}
78
79impl Default for ClientDevice {
80    fn default() -> Self {
81        Self::Unknown
82    }
83}
84
85#[derive(Serialize, Deserialize, Debug)]
86pub struct OtherClientSender {
87    pub id: i64,
88    #[serde(default)]
89    pub platform: ClientDevice
90}
91
92#[derive(Serialize, Deserialize, Debug)]
93#[serde(rename_all = "PascalCase", tag="type")]
94pub enum MsgUnit {
95    Source {
96        id: i64,
97        time: i64,
98    },
99    #[serde(rename_all = "camelCase")]
100    Quote {
101        group_id: i64,
102        sender_id: i64,
103        target_id: i64,
104        origin: Vec<MsgUnit>
105    },
106    At {
107        target: i64,
108        display: String
109    },
110    AtAll,
111    #[serde(rename_all = "camelCase")]
112    Face {
113        face_id: i64,
114        name: String
115    },
116    Plain {
117        text: String
118    },
119    #[serde(rename_all = "camelCase")]
120    Image {
121        image_id: String,
122        url: String,
123    },
124    #[serde(rename_all = "camelCase")]
125    FlashImage{
126        image_id: String,
127        url: String,
128    },
129    Xml {
130        xml:String
131    },
132    Json {
133        json: String
134    },
135    App {
136        content: String
137    },
138    Poke {
139        name: String
140    },
141    Dice {
142        value: i8
143    },
144    #[serde(rename_all = "camelCase")]
145    MusicShare {
146        kind: String,
147        title: String,
148        summary: String,
149        jump_url: String,
150        picture_url: String,
151        music_url: String,
152        brief: String
153    },
154    #[serde(rename_all = "camelCase")]
155    Forward {
156        node_list: Vec<MsgNode>
157    },
158    File {
159        id: String,
160        name: String,
161        size: i64
162    },
163    MiraiCode {
164        code: String
165    }
166}
167
168#[derive(Serialize, Deserialize, Debug)]
169#[serde(rename_all = "camelCase")]
170pub struct MsgNode {
171    sender_id: i64,
172    time: i64,
173    sender_name: String,
174    message_chain: Vec<MsgUnit>,
175    message_id: Option<usize>
176}
177
178/// # Usage
179/// 
180/// use this macro to create a plain text `MsgUnit`
181/// ```rust
182/// let msg_chain = vec![
183///     text!("hello"),
184///     text!("just simply using the macro {}!()", "text")
185/// ];
186/// ```
187#[macro_export]
188macro_rules! text {
189    ($s:expr) => {
190        MsgUnit::Plain{text:$s.into()}
191    };
192    ($($arg:tt)*) => {{
193        MsgUnit::Plain{text: format!($($arg)*)}
194    }}
195}
196
197/// # Usage
198/// 
199/// use this macro to create a image `MsgUnit`
200/// ```rust
201/// let msg_chain = vec![
202///     // in default case, this macro create image by url
203///     img!("https://some.web.com/img.jpg"),
204///     // you can specify whether url 
205///     img!(url = "https://some.web.com/img.jpg"),
206///     // or image-id you are going to use
207///     img!(id = "abcdefg-hijk")
208/// ];
209/// ```
210#[macro_export]
211macro_rules! img {
212    (url:$url:expr) => {
213        MsgUnit::Image{image_id:"".into(), url:($url).into()}
214    };
215    (id:$id:expr) => {
216        MsgUnit::Image{image_id:($id).into(), url:"".into()}
217    };
218    ($url:expr) => {
219        MsgUnit::Image{image_id:"".into(), url:($url).into()}
220    };
221}
222
223/// # Usage
224/// 
225/// use this macro to create a image `At`
226/// ```rust
227/// let msg_chain = vec![
228///     // at some body
229///     at!(123456789),
230///     // at all
231///     at!(/all),
232/// ];
233/// ```
234#[macro_export]
235macro_rules! at {
236    ($target:expr) => {
237        MsgUnit::At{target:$target.into(), display:"".into()}
238    };
239    (/all) => {
240        MsgUnit::AtAll
241    }
242}
243
244#[macro_export]
245macro_rules! chain {
246    ($($x:expr,)*) => {
247        vec![$($x),*]
248    };
249}