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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
manual_braid! {
    /// A Badge set ID
    pub struct BadgeSetId;
    pub struct BadgeSetIdRef;
}
impl_extra!(BadgeSetId, BadgeSetIdRef);

manual_braid! {
    /// A channel chat badge ID
    pub struct ChatBadgeId;
    pub struct ChatBadgeIdRef;
}
impl_extra!(ChatBadgeId, ChatBadgeIdRef);

manual_braid! {
    /// A chat Emote ID
    pub struct EmoteId;
    pub struct EmoteIdRef;
}
impl_extra!(EmoteId, EmoteIdRef);

impl EmoteIdRef {
    /// Generates url for this emote.
    ///
    /// Generated URL will be `"https://static-cdn.jtvnw.net/emoticons/v2/{emote_id}/default/light/1.0"`
    pub fn default_render(&self) -> String {
        EmoteUrlBuilder {
            id: self.into(),
            animation_setting: None,
            theme_mode: EmoteThemeMode::Light,
            scale: EmoteScale::Size1_0,
            template: EMOTE_V2_URL_TEMPLATE.into(),
        }
        .render()
    }

    /// Create a [`EmoteUrlBuilder`] for this emote
    pub fn url(&self) -> EmoteUrlBuilder<'_> { EmoteUrlBuilder::new(self) }
}

/// Emote url template
pub(crate) static EMOTE_V2_URL_TEMPLATE: &str =
    "https://static-cdn.jtvnw.net/emoticons/v2/{{id}}/{{format}}/{{theme_mode}}/{{scale}}";

/// Formats for an emote.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum EmoteAnimationSetting {
    /// Static
    Static,
    /// Animated
    Animated,
}

impl std::fmt::Display for EmoteAnimationSetting {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            EmoteAnimationSetting::Static => "static",
            EmoteAnimationSetting::Animated => "animated",
        })
    }
}

/// Background themes available for an emote.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum EmoteThemeMode {
    /// Light
    Light,
    /// Dark
    Dark,
}

impl Default for EmoteThemeMode {
    fn default() -> Self { Self::Light }
}

impl std::fmt::Display for EmoteThemeMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            EmoteThemeMode::Light => "light",
            EmoteThemeMode::Dark => "dark",
        })
    }
}

/// Scales available for an emote.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub enum EmoteScale {
    /// 1.0
    #[cfg_attr(feature = "serde", serde(rename = "1.0"))]
    Size1_0,
    /// 2.0
    #[cfg_attr(feature = "serde", serde(rename = "2.0"))]
    Size2_0,
    /// 3.0
    #[cfg_attr(feature = "serde", serde(rename = "3.0"))]
    Size3_0,
}

impl Default for EmoteScale {
    fn default() -> Self { Self::Size1_0 }
}

impl std::fmt::Display for EmoteScale {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            EmoteScale::Size1_0 => "1.0",
            EmoteScale::Size2_0 => "2.0",
            EmoteScale::Size3_0 => "3.0",
        })
    }
}

/// Builder for [emote URLs](https://dev.twitch.tv/docs/irc/emotes#emote-cdn-url-format).
///
/// # Examples
///
/// ```rust
/// # use twitch_types::EmoteId;
/// let emote_id = EmoteId::from("emotesv2_dc24652ada1e4c84a5e3ceebae4de709");
/// assert_eq!(emote_id.url().size_3x().dark_mode().render(), "https://static-cdn.jtvnw.net/emoticons/v2/emotesv2_dc24652ada1e4c84a5e3ceebae4de709/default/dark/3.0")
/// ```
#[derive(Debug, Clone)]
pub struct EmoteUrlBuilder<'a> {
    pub(crate) id: std::borrow::Cow<'a, EmoteIdRef>,
    pub(crate) animation_setting: Option<EmoteAnimationSetting>,
    pub(crate) theme_mode: EmoteThemeMode,
    pub(crate) scale: EmoteScale,
    pub(crate) template: std::borrow::Cow<'a, str>,
}

impl EmoteUrlBuilder<'_> {
    // FIXME: AsRef
    /// Construct a new [`EmoteUrlBuilder`] from a [`EmoteId`]
    ///
    /// Defaults to `1.0` scale, `default` animation and `light` theme.
    pub fn new(id: &EmoteIdRef) -> EmoteUrlBuilder<'_> {
        EmoteUrlBuilder {
            id: id.into(),
            animation_setting: <_>::default(),
            theme_mode: <_>::default(),
            scale: <_>::default(),
            template: EMOTE_V2_URL_TEMPLATE.into(),
        }
    }

    /// Set size to 1.0
    pub fn size_1x(mut self) -> Self {
        self.scale = EmoteScale::Size1_0;
        self
    }

    /// Set size to 2.0
    pub fn size_2x(mut self) -> Self {
        self.scale = EmoteScale::Size2_0;
        self
    }

    /// Set size to 3.0
    pub fn size_3x(mut self) -> Self {
        self.scale = EmoteScale::Size3_0;
        self
    }

    /// Set theme to dark mode
    pub fn dark_mode(mut self) -> Self {
        self.theme_mode = EmoteThemeMode::Dark;
        self
    }

    /// Set theme to light mode
    pub fn light_mode(mut self) -> Self {
        self.theme_mode = EmoteThemeMode::Light;
        self
    }

    /// Set animation mode to default
    pub fn animation_default(mut self) -> Self {
        self.animation_setting = None;
        self
    }

    /// Set animation mode to static
    pub fn animation_static(mut self) -> Self {
        self.animation_setting = Some(EmoteAnimationSetting::Static);
        self
    }

    /// Set animation mode to animate
    pub fn animation_animated(mut self) -> Self {
        self.animation_setting = Some(EmoteAnimationSetting::Animated);
        self
    }

    /// Create the URL for this emote.
    pub fn render(self) -> String {
        if self.template != "https://static-cdn.jtvnw.net/emoticons/v2/{{id}}/{{format}}/{{theme_mode}}/{{scale}}" {
            let custom_template = |builder: &EmoteUrlBuilder| -> Option<String> {
                let mut template = self.template.clone().into_owned();
                let emote_id_range = template.find("{{id}}")?;
                template.replace_range(emote_id_range..emote_id_range+"{{id}}".len(), builder.id.as_str());
                let format_range = template.find("{{format}}")?;
                template.replace_range(format_range..format_range+"{{format}}".len(), &builder.animation_setting.as_ref().map(|s| s.to_string()).unwrap_or_else(|| String::from("default")));
                let theme_mode_range = template.find("{{theme_mode}}")?;
                template.replace_range(theme_mode_range..theme_mode_range+"{{theme_mode}}".len(), &builder.theme_mode.to_string());
                let scale_range = template.find("{{scale}}")?;
                template.replace_range(scale_range..scale_range+"{{scale}}".len(), &builder.scale.to_string());
                if template.contains("{{") || template.contains("}}") {
                    None
                } else {
                    Some(template)
                }
            };
            if let Some(template) = custom_template(&self) {
                return template
            } else {
                #[cfg(feature = "tracing")]
                tracing::warn!(template = %self.template, "emote builder was supplied an invalid or unknown template url, falling back to standard builder");
            }
        }
        // fallback to known working template
        format!("https://static-cdn.jtvnw.net/emoticons/v2/{emote_id}/{animation_setting}/{theme_mode}/{scale}",
            emote_id = self.id,
            animation_setting = self.animation_setting.as_ref().map(|s| s.to_string()).unwrap_or_else(|| String::from("default")),
            theme_mode = self.theme_mode,
            scale = self.scale,
        )
    }
}

manual_braid! {
    /// An Emote Set ID
    pub struct EmoteSetId;
    pub struct EmoteSetIdRef;
}
impl_extra!(EmoteSetId, EmoteSetIdRef);

/// An emote index as defined by eventsub, similar to IRC `emotes` twitch tag.
#[derive(PartialEq, Eq, Debug, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ResubscriptionEmote {
    /// The index of where the Emote starts in the text.
    pub begin: i64,
    /// The index of where the Emote ends in the text.
    pub end: i64,
    /// The emote ID.
    pub id: EmoteId,
}

impl std::fmt::Display for ResubscriptionEmote {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}-{}", self.id, self.begin, self.end)
    }
}

/// Links to the same image of different sizes
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct Image {
    /// URL to png of size 28x28
    pub url_1x: String,
    /// URL to png of size 56x56
    pub url_2x: String,
    /// URL to png of size 112x112
    pub url_4x: String,
}