ataraxia/models/
message.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use serde_json::Value;
4#[derive(Serialize, Deserialize, Debug)]
5pub struct Message {
6    pub _id: String,
7    pub author: String,
8    #[serde(rename = "channel")]
9    pub channel_id: String,
10    pub content: String,
11    pub nonce: String,
12    pub mentions: Option<Vec<String>>,
13    pub attachments: Option<Vec<MessageAttachments>>,
14    pub edited: Option<String>,
15    pub embed: Option<Vec<Embed>>
16}
17
18#[derive(Debug, Serialize, Deserialize)]
19pub struct  MessageAttachments {
20    pub _id: String,
21    pub tag: String,
22    pub filename: String,
23    pub metadata: MessageMetadata,
24    pub content_type: String,
25    pub size: usize
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct MessageMetadata {
30    #[serde(rename = "type")]
31    pub _type: String,
32    pub width: usize,
33    pub height: usize,
34}
35
36
37
38impl std::fmt::Display for Message {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        if self.content.is_empty() {
41            write!(f, "Channel: {}, Author: {}", self.channel_id, self.author)
42        } else {
43            write!(f, "Channel: {}, Author: {}, Content: {}", self.channel_id, self.author, self.content)
44        }
45    }
46}
47
48#[derive(Debug, Serialize, Deserialize)]
49pub struct Embed {
50    pub icon_url: Option<String>,
51    pub url: Option<String>,
52    pub title: Option<String>,
53    pub description: Option<String>,
54    pub media: Option<String>,
55    pub colour: Option<String>,
56}
57// Create a builder for a message
58#[derive(Serialize, Debug)]
59pub struct CreateMessage (
60    #[serde(borrow)]
61    pub HashMap<&'static str, Value>,
62);
63
64#[derive(Serialize, Debug)]
65pub struct CreateEmbed (
66    #[serde(borrow)]
67    pub HashMap<&'static str, Value>,
68);
69
70#[derive(Serialize, Deserialize, Debug)]
71struct MasqueradeMessage {
72    pub name: Option<String>,
73    pub avatar: Option<String>,
74}
75
76#[derive(Serialize, Deserialize, Debug)]
77pub struct CreateMasqueradeMessage {
78    name: Option<String>,
79    avatar: Option<String>,
80}
81
82
83
84impl CreateMessage {
85     /// Set the content of the message.
86    ///
87    /// **Note**: Message contents must be under 2000 unicode code points.
88    #[inline]
89    pub fn content<D: ToString>(&mut self, content: D) -> &mut Self {
90        self.0.insert("content", Value::from(content.to_string()));
91        self
92    }
93
94
95    pub fn masquerade(&mut self, name: &str) -> &mut Self {
96        self.0.insert("masquerade", serde_json::to_value(MasqueradeMessage {
97            name: Some(name.to_string()),
98            avatar: None,
99        }).unwrap());
100        self
101    }
102
103    pub fn set_masquerade<F>(&mut self, f: F) -> &mut Self
104    where F: FnOnce(&mut CreateMasqueradeMessage) -> &mut CreateMasqueradeMessage {
105        let mut mm = CreateMasqueradeMessage::default();
106        let masq = f(&mut mm);
107        self.0.insert("masquerade", serde_json::to_value(masq).unwrap());
108        self
109    }
110
111    /// Create an embed in the message. And push it to the embeds array.
112    pub fn create_embed<T>(&mut self, f: T) -> &mut Self
113    where T: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed {
114        let mut embed = CreateEmbed(HashMap::new());
115        let embed = f(&mut embed);
116        self.0.entry("embeds").or_insert(Value::Array(vec![])).as_array_mut().unwrap().push(to_value(embed).unwrap());
117        
118        self
119    }
120}
121
122impl CreateMasqueradeMessage {
123    pub fn name<D: ToString>(&mut self, name: D) -> &mut Self {
124        self.name = Some(name.to_string());
125        self
126    }
127
128    pub fn avatar<D: ToString>(&mut self, avatar: D) -> &mut Self {
129        self.avatar = Some(avatar.to_string());
130        self
131    }
132
133}
134
135
136impl CreateEmbed {
137    pub fn title<D: ToString>(&mut self, title: D) -> &mut Self {
138        self.0.insert("title", Value::from(title.to_string()));
139        self
140    }
141
142    pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
143        self.0.insert("description", Value::from(description.to_string()));
144        self
145    }
146
147    pub fn url<D: ToString>(&mut self, url: D) -> &mut Self {
148        self.0.insert("url", Value::from(url.to_string()));
149        self
150    }
151
152    /// Set the colour of the embed.
153    /// 
154    /// **Note**: The colour must be a hexadecimal string.
155    pub fn colour<D: ToString>(&mut self, colour: D) -> &mut Self {
156        self.0.insert("colour", Value::from(colour.to_string()));
157        self
158    }
159
160    pub fn icon_url<D: ToString>(&mut self, icon_url: D) -> &mut Self {
161        self.0.insert("icon_url", Value::from(icon_url.to_string()));
162        self
163    }
164
165}
166
167
168
169impl Default for CreateMessage {
170    fn default() -> CreateMessage {
171        let mut map = HashMap::new();
172        map.insert("title", Value::from("hello ataraxia!"));
173
174        CreateMessage(map)
175    }
176}
177
178impl Default for CreateEmbed {
179    fn default() -> CreateEmbed {
180        let mut map = HashMap::new();
181        map.insert("content", Value::from("hello ataraxia!"));
182
183        CreateEmbed(map)
184    }
185}
186
187impl Default for CreateMasqueradeMessage {
188    fn default() -> CreateMasqueradeMessage {
189        CreateMasqueradeMessage {
190            name: None,
191            avatar: None,
192        }
193    }
194}
195
196
197use std::result::Result as StdResult;
198
199pub type Result<T> = StdResult<T, serde_json::Error>;
200pub type JsonMap = serde_json::Map<String, Value>;
201
202// null value 
203pub const NULL: Value = Value::Null;
204
205/// Converts a HashMap into a final [`JsonMap`] representation.
206pub fn hashmap_to_json_map<H, T>(map: HashMap<T, Value, H>) -> JsonMap
207where
208    H: std::hash::BuildHasher,
209    T: Eq + std::hash::Hash + ToString,
210{
211    map.into_iter().map(|(k, v)| (k.to_string(), v)).collect()
212}
213
214pub(crate) fn to_value<T>(value: T) -> Result<Value>
215where
216    T: Serialize,
217{
218    Ok(serde_json::to_value(value)?)
219}