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
//! URL Builders for badge, large and small widgets.

use std::collections::HashMap;
use url::{ParseError, Url};

use crate::types::BotId;

/// URL Builder for [badge widgets](https://discordbots.org/api/docs#widgets).
pub enum Badge {
    Owner,
    Upvotes,
    Servers,
    Status,
    Library,
}

impl Badge {
    pub fn build<T>(&self, bot: T, show_avatar: bool) -> Result<Url, ParseError>
    where
        T: Into<BotId>,
    {
        let kind = match self {
            Badge::Owner => "owner",
            Badge::Library => "lib",
            Badge::Servers => "servers",
            Badge::Status => "status",
            Badge::Upvotes => "upvotes",
        };
        let mut url = api!("/widget/{}/{}.svg", kind, bot.into());
        if !show_avatar {
            url.push_str("?noavatar=true");
        }
        Url::parse(&url)
    }
}

/// URL Builder for [large widgets](https://discordbots.org/api/docs#widgets).
pub struct LargeWidget(HashMap<&'static str, String>);
/// URL Builder for [small widgets](https://discordbots.org/api/docs#widgets).
pub struct SmallWidget(HashMap<&'static str, String>);

macro_rules! impl_widget {
    (
        $widget:ident {
            $(
                $(#[$fn_meta:ident $($meta_args:tt)*])*
                $fn:ident: $name:expr;
            )+
        }
    ) => {
        impl $widget {
            /// Build the widget url.
            pub fn build<T>(self, bot: T) -> Result<Url, ParseError>
            where
                T: Into<BotId>,
            {
                Url::parse_with_params(&api!("/widget/{}.svg", bot.into()), self.0)
            }

            $(
                $(#[$fn_meta $($meta_args)*])*
                pub fn $fn<T>(mut self, color: T) -> Self
                where
                    T: ToString,
                {
                    self.0.insert($name, color.to_string());
                    self
                }
            )+
        }
    };
}

impl_widget!(LargeWidget {
    top_color: "topcolor";
    middle_color: "middlecolor";
    username_color: "usernamecolor";
    certified_color: "certifiedcolor";
    data_color: "datacolor";
    label_color: "labelcolor";
    hightlight_color: "hightlightcolor";
});

impl_widget!(SmallWidget {
    avatarbg_color: "avatarbgcolor";
    left_color: "leftcolor";
    right_color: "rightcolor";
    lefttext_color: "lefttextcolor";
    righttext_color: "righttextcolor";
});