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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::Error;
use serde_derive::Serialize;

/// Object used to construct outgoing messages
pub struct MessageBuilder<'a> {
    content: &'a str,
    embed: Option<&'a EmbedBuilder<'a>>,
}

impl<'a> MessageBuilder<'a> {
    /// Create a new MessageBuilder
    pub fn new(content: &'a str) -> Self {
        Self {
            content,
            embed: None,
        }
    }

    /// Set an embed for this message
    pub fn set_embed(&mut self, embed: &'a EmbedBuilder<'a>) {
        self.embed = Some(embed);
    }

    /// Set an embed for this message
    pub fn with_embed(mut self, embed: &'a EmbedBuilder<'a>) -> Self {
        self.set_embed(embed);
        self
    }

    #[doc(hidden)]
    pub fn to_request_body(&self, channel: &str) -> Result<String, Error> {
        #[derive(Serialize, Debug)]
        struct MessageCreateBody<'a> {
            content: &'a str,
            channel: &'a str,
            embed: Option<&'a EmbedBuilder<'a>>,
        }
        serde_json::to_string(&MessageCreateBody {
            content: self.content,
            channel,
            embed: self.embed,
        })
        .map_err(|e| {
            Error::Other(format!(
                "Failed to serialize message creation body: {:?}",
                e
            ))
        })
    }
}

#[derive(Default, Serialize, Debug)]
/// Builder for a message embed
pub struct EmbedBuilder<'a> {
    title: Option<&'a str>,
    description: Option<&'a str>,
    url: Option<&'a str>,
    timestamp: Option<&'a str>,
    color: Option<u32>,
    footer: Option<&'a EmbedFooter<'a>>,
    image: Option<&'a str>,
    thumbnail: Option<&'a str>,
    author: Option<&'a EmbedAuthor<'a>>,
    fields: Vec<&'a EmbedField<'a>>,
}

#[allow(missing_docs)]
impl<'a> EmbedBuilder<'a> {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn set_title(&mut self, title: &'a str) {
        self.title = Some(title);
    }

    pub fn with_title(mut self, title: &'a str) -> Self {
        self.set_title(title);
        self
    }

    pub fn set_author(&mut self, author: &'a EmbedAuthor<'a>) {
        self.author = Some(author);
    }

    pub fn with_author(mut self, author: &'a EmbedAuthor<'a>) -> Self {
        self.set_author(author);
        self
    }

    pub fn set_color(&mut self, color: u32) {
        self.color = Some(color);
    }

    pub fn with_color(mut self, color: u32) -> Self {
        self.set_color(color);
        self
    }

    pub fn add_field(&mut self, field: &'a EmbedField<'a>) {
        self.fields.push(field);
    }

    pub fn with_field(mut self, field: &'a EmbedField<'a>) -> Self {
        self.add_field(field);
        self
    }

    pub fn set_footer(&mut self, footer: &'a EmbedFooter<'a>) {
        self.footer = Some(footer);
    }

    pub fn with_footer(mut self, footer: &'a EmbedFooter<'a>) -> Self {
        self.set_footer(footer);
        self
    }

    pub fn set_description(&mut self, description: &'a str) {
        self.description = Some(description);
    }

    pub fn with_description(mut self, description: &'a str) -> Self {
        self.set_description(description);
        self
    }

    pub fn set_timestamp(&mut self, timestamp: &'a str) {
        self.timestamp = Some(timestamp);
    }

    pub fn with_timestamp(mut self, timestamp: &'a str) -> Self {
        self.set_timestamp(timestamp);
        self
    }
}

#[derive(Default, Serialize, Debug)]
/// Representation of an embed author
pub struct EmbedAuthor<'a> {
    /// Name of author
    pub name: Option<&'a str>,
    /// URL of author
    pub url: Option<&'a str>,
    /// URL of author icon
    pub icon_url: Option<&'a str>,
}

impl<'a> EmbedAuthor<'a> {
    /// Create an empty embed author
    pub fn new() -> Self {
        Default::default()
    }
}

#[derive(Serialize, Debug)]
/// Representation of an embed footer
pub struct EmbedFooter<'a> {
    text: &'a str,
    icon_url: Option<&'a str>,
}

impl<'a> EmbedFooter<'a> {
    /// Create a text-only footer
    pub fn new(text: &'a str) -> Self {
        EmbedFooter {
            text,
            icon_url: None,
        }
    }
    /// Create a footer with an icon
    pub fn new_with_icon(text: &'a str, icon_url: &'a str) -> Self {
        EmbedFooter {
            text,
            icon_url: Some(icon_url),
        }
    }
}

#[derive(Serialize, Debug)]
/// Representation of an [embed field](https://discordapp.com/developers/docs/resources/channel#embed-object-embed-field-structure)
pub struct EmbedField<'a> {
    name: &'a str,
    value: &'a str,
    inline: bool,
}

impl<'a> EmbedField<'a> {
    /// Create a new embed field
    pub fn new(name: &'a str, value: &'a str) -> Self {
        EmbedField::new_internal(name, value, false)
    }
    /// Create a new inline embed field
    pub fn new_inline(name: &'a str, value: &'a str) -> Self {
        EmbedField::new_internal(name, value, true)
    }
    fn new_internal(name: &'a str, value: &'a str, inline: bool) -> Self {
        EmbedField {
            name,
            value,
            inline,
        }
    }
}