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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Types for generating widget embed URLs.

use crate::{
    endpoints,
    error::{InvalidUrl, Result},
};
use snafu::ResultExt;
use std::collections::HashMap;
use url::Url;

#[derive(Clone, Debug)]
struct Widget(u64, HashMap<&'static str, String>, bool);

impl Widget {
    fn build(self) -> Result<String> {
        let widget_uri = if self.2 {
            endpoints::png_widget(self.0)
        } else {
            endpoints::widget(self.0)
        };

        let params = self.1.into_iter().map(|(k, v)| (k, v)).collect::<Vec<_>>();

        let url = Url::parse_with_params(&widget_uri, params)
            .with_context(|| InvalidUrl { uri: widget_uri })?;

        Ok(url.into_string())
    }

    fn insert(&mut self, k: &'static str, v: impl Into<String>) -> &mut Self {
        self._insert(k, v.into())
    }

    fn _insert(&mut self, k: &'static str, v: String) -> &mut Self {
        self.1.insert(k, v);

        self
    }
}

/// Type-safe and guarenteed method of making a large widget.
#[derive(Clone, Debug)]
pub struct LargeWidget(Widget);

impl LargeWidget {
    /// Creates a new builder for making a large widget.
    pub fn new(bot_id: u64) -> Self {
        Self(Widget(bot_id, HashMap::new(), false))
    }

    /// Builds into a valid URL.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidUrl`] if one of the query parameters is invalid.
    ///
    /// [`Error::InvalidUrl`]: ../enum.Error.html#variant.InvalidUrl
    pub fn build(self) -> Result<String> {
        self.0.build()
    }

    /// Sets the top color of the widget.
    pub fn top_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("topcolor", value);

        self
    }

    /// Sets the middle color of the widget.
    pub fn middle_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("middlecolor", value);

        self
    }

    /// Sets the username color of the widget.
    pub fn username_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("usernamecolor", value);

        self
    }

    /// Sets the certified color of the widget.
    pub fn certified_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("certifiedcolor", value);

        self
    }

    /// Sets the data color of the widget.
    pub fn data_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("datacolor", value);

        self
    }

    /// Sets the label color of the widget.
    pub fn label_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("labelcolor", value);

        self
    }

    /// Sets if the widget should be a png instead of a svg.
    pub fn png(&mut self, value: bool) -> &mut Self {
        (self.0).2 = value;

        self
    }
}

/// Type-safe and guarenteed method of making a small widget.
#[derive(Clone, Debug)]
pub struct SmallWidget(Widget);

impl SmallWidget {
    /// Creates a new builder for making a small widget.
    pub fn new(bot_id: u64) -> Self {
        Self(Widget(bot_id, HashMap::new(), false))
    }

    /// Builds into a valid URL.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidUrl`] if one of the query parameters is invalid.
    ///
    /// [`Error::InvalidUrl`]: ../enum.Error.html#variant.InvalidUrl
    pub fn build(self) -> Result<String> {
        self.0.build()
    }

    /// Sets the background color of the widget.
    pub fn avatar_background(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("avatarbg", value);

        self
    }

    /// Sets the left color of the widget.
    pub fn left_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("leftcolor", value);

        self
    }

    /// Sets the left text color of the widget.
    pub fn left_text_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("lefttextcolor", value);

        self
    }

    /// Sets the right color of the widget.
    pub fn right_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("rightcolor", value);

        self
    }

    /// Sets the right text color of the widget.
    pub fn right_text_color(&mut self, value: impl Into<String>) -> &mut Self {
        self.0.insert("righttextcolor", value);

        self
    }

    /// Sets if the widget should be a png instead of a svg.
    pub fn png(&mut self, value: bool) -> &mut Self {
        (self.0).2 = value;

        self
    }
}

#[cfg(test)]
mod tests {
    use super::{LargeWidget, SmallWidget};
    use crate::Result;

    // The ordering of `url`'s parsed query parameters isn't always
    // reproducable.
    #[test]
    fn test_small_widget() -> Result<()> {
        let mut widget = SmallWidget::new(1);
        widget
            .avatar_background("00FF00")
            .left_color("FF0000")
            .left_text_color("FFFFFF")
            .right_color("0F0F0F")
            .right_text_color("F0F0F0")
            .png(true);

        let url = widget.build()?;
        assert!(url.contains("avatarbg=00FF00"));
        assert!(url.contains("lefttextcolor=FFFFFF"));
        assert!(url.contains("leftcolor=FF0000"));
        assert!(url.contains("rightcolor=0F0F0F"));
        assert!(url.contains("righttextcolor=F0F0F0"));
        assert!(url.starts_with("https://top.gg/api/widget/1.png?"));

        Ok(())
    }

    #[test]
    fn test_large_widget() -> Result<()> {
        let mut widget = LargeWidget::new(1);
        widget
            .certified_color("FF0000")
            .data_color("00FF00")
            .label_color("0000FF")
            .middle_color("FFF000")
            .top_color("000FFF")
            .username_color("AAAAAA");

        let url = widget.build()?;
        assert!(url.contains("certifiedcolor=FF0000"));
        assert!(url.contains("datacolor=00FF00"));
        assert!(url.contains("labelcolor=0000FF"));
        assert!(url.contains("middlecolor=FFF000"));
        assert!(url.contains("topcolor=000FFF"));
        assert!(url.contains("usernamecolor=AAAAAA"));
        assert!(url.starts_with("https://top.gg/api/widget/1.svg?"));

        Ok(())
    }
}