1
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::Attachment;
use af_core::prelude::*;
#[derive(Debug, Deserialize)]
pub struct MessageId {
pub channel: String,
pub ts: String,
}
#[derive(Debug, Serialize)]
pub struct Message<'a> {
pub attachments: Vec<Attachment<'a>>,
pub text: Cow<'a, str>,
}
impl<'a> Message<'a> {
pub const fn new() -> Self {
Self { attachments: Vec::new(), text: Cow::Borrowed("") }
}
pub fn add_attachment(&mut self, attachment: Attachment<'a>) -> &mut Self {
self.attachments.push(attachment);
self
}
pub fn set_text(&mut self, text: impl Into<Cow<'a, str>>) -> &mut Self {
self.text = text.into();
self
}
pub fn with_attachment(mut self, attachment: Attachment<'a>) -> Self {
self.attachments.push(attachment);
self
}
pub fn with_text(mut self, text: impl Into<Cow<'a, str>>) -> Self {
self.set_text(text);
self
}
}
impl<'a> Default for Message<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a, T> From<T> for Message<'a>
where
Cow<'a, str>: From<T>,
{
fn from(text: T) -> Message<'a> {
let mut msg = Message::new();
msg.set_text(text);
msg
}
}
impl<'a> From<Attachment<'a>> for Message<'a> {
fn from(attachment: Attachment<'a>) -> Self {
let mut message = Self::new();
message.attachments.push(attachment);
message
}
}