rsbadges 1.1.5

Create code badges from the comfort and safety of Rust
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// BSD 3-Clause License
//
// Copyright (c) 2021 RSBadges Authors
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
// OF SUCH DAMAGE.

//! Generate an SVG from badge information

use super::badge_type::*;
use super::format_helper;
use askama::Template;
use rand::{distributions::Alphanumeric, Rng};

#[derive(Template, Debug)]
#[template(path = "badge_template_flat.xml", escape = "xml")]
/// Holds all information necessary for a Flat badge template.
struct BadgeTemplateFlat<'a> {
    /// text for label (left side)
    label_text: &'a str,
    /// text for message (right side)
    msg_text: &'a str,
    /// link for entire badge
    badge_link: &'a str,
    /// URL link for label
    label_link: &'a str,
    /// URL link for message
    msg_link: &'a str,
    /// color for label
    label_color: &'a str,
    /// color for message
    msg_color: &'a str,
    /// link to logo, placed on label side
    logo: &'a str,
    /// title of badge
    full_badge_title: &'a str,
    /// title for label
    label_title: &'a str,
    /// title for message
    msg_title: &'a str,
    /// height of the badge, in px
    badge_height: f32,
    /// logo width of the badge, in px
    logo_width: f32,
    /// logo placement in x
    logo_x: f32,
    /// logo placement in y
    logo_y: f32,
    /// width of text for label, in px
    label_text_width: f32,
    /// width of text for message, in px
    msg_text_width: f32,
    /// logo text placement in x
    label_text_x: f32,
    /// message text placement in x
    msg_text_x: f32,
    /// width of left side of badge
    left_width: f32,
    /// width of right side of badge
    right_width: f32,
    /// unique ID for smooth gradient
    id_smooth: &'a str,
    /// unique ID for round shape
    id_round: &'a str,
}

/// Minify SVG string
fn minify_svg_str(svg_str: String) -> String {
    svg_str
        .chars()
        .filter(|c| *c != '\r' && *c != '\n')
        .collect::<String>()
}

/// Generate the SVG string corresponding to a Flat badge with this Badge info
pub(crate) fn flat_svg(badge: &Badge, layout: Layout) -> Result<String, BadgeError> {
    let id_suffix: String = rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(7)
        .map(char::from)
        .collect();
    let id_smooth = format!("smooth{}", id_suffix);
    let id_round = format!("round{}", id_suffix);
    let mut logo_uri = badge.logo.clone();
    if badge.embed_logo {
        logo_uri = format_helper::attempt_logo_download(&badge.logo)?;
    }
    let flat_badge = BadgeTemplateFlat {
        label_text: &layout.label_text_norm,
        msg_text: &layout.msg_text_norm,
        badge_link: &badge.badge_link,
        label_link: &badge.label_link,
        msg_link: &badge.msg_link,
        label_color: &layout.label_color,
        msg_color: &layout.msg_color,
        logo: &logo_uri,
        full_badge_title: &badge.badge_title,
        label_title: &badge.label_title,
        msg_title: &badge.msg_title,
        badge_height: layout.badge_height,
        logo_width: layout.logo_width,
        logo_x: layout.logo_x,
        logo_y: layout.logo_y,
        label_text_width: layout.label_text_width,
        msg_text_width: layout.msg_text_width,
        label_text_x: layout.label_text_x,
        msg_text_x: layout.msg_text_x,
        left_width: layout.label_total_width,
        right_width: layout.msg_total_width,
        id_smooth: &id_smooth,
        id_round: &id_round,
    };
    Ok(minify_svg_str(flat_badge.render().unwrap()))
}

#[derive(Template, Debug)]
#[template(path = "badge_template_plastic.xml", escape = "xml")]
/// Holds all information necessary for a Plastic badge template.
struct BadgeTemplatePlastic<'a> {
    /// text for label (left side)
    label_text: &'a str,
    /// text for message (right side)
    msg_text: &'a str,
    /// link for entire badge
    badge_link: &'a str,
    /// URL link for label
    label_link: &'a str,
    /// URL link for message
    msg_link: &'a str,
    /// color for label
    label_color: &'a str,
    /// color for message
    msg_color: &'a str,
    /// link to logo, placed on label side
    logo: &'a str,
    /// title of badge
    full_badge_title: &'a str,
    /// title for label
    label_title: &'a str,
    /// title for message
    msg_title: &'a str,
    /// height of the badge, in px
    badge_height: f32,
    /// logo width of the badge, in px
    logo_width: f32,
    /// logo placement in x
    logo_x: f32,
    /// logo placement in y
    logo_y: f32,
    /// width of text for label, in px
    label_text_width: f32,
    /// width of text for message, in px
    msg_text_width: f32,
    /// logo text placement in x
    label_text_x: f32,
    /// message text placement in x
    msg_text_x: f32,
    /// width of left side of badge
    left_width: f32,
    /// width of right side of badge
    right_width: f32,
    /// unique ID for smooth gradient
    id_smooth: &'a str,
    /// unique ID for round shape
    id_round: &'a str,
}

/// Generate the SVG string corresponding to a Plastic badge with this Badge info
pub(crate) fn plastic_svg(badge: &Badge, layout: Layout) -> Result<String, BadgeError> {
    let id_suffix: String = rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(7)
        .map(char::from)
        .collect();
    let id_smooth = format!("smooth{}", id_suffix);
    let id_round = format!("round{}", id_suffix);
    let mut logo_uri = badge.logo.clone();
    if badge.embed_logo {
        logo_uri = format_helper::attempt_logo_download(&badge.logo)?;
    }
    let plastic_badge = BadgeTemplatePlastic {
        label_text: &layout.label_text_norm,
        msg_text: &layout.msg_text_norm,
        badge_link: &badge.badge_link,
        label_link: &badge.label_link,
        msg_link: &badge.msg_link,
        label_color: &layout.label_color,
        msg_color: &layout.msg_color,
        logo: &logo_uri,
        full_badge_title: &badge.badge_title,
        label_title: &badge.label_title,
        msg_title: &badge.msg_title,
        badge_height: layout.badge_height,
        logo_width: layout.logo_width,
        logo_x: layout.logo_x,
        logo_y: layout.logo_y,
        label_text_width: layout.label_text_width,
        msg_text_width: layout.msg_text_width,
        label_text_x: layout.label_text_x,
        msg_text_x: layout.msg_text_x,
        left_width: layout.label_total_width,
        right_width: layout.msg_total_width,
        id_smooth: &id_smooth,
        id_round: &id_round,
    };
    Ok(minify_svg_str(plastic_badge.render().unwrap()))
}

#[derive(Template, Debug)]
#[template(path = "badge_template_flat_square.xml", escape = "xml")]
/// Holds all information necessary for a Flat Square badge template.
struct BadgeTemplateFlatSquare<'a> {
    /// text for label (left side)
    label_text: &'a str,
    /// text for message (right side)
    msg_text: &'a str,
    /// link for entire badge
    badge_link: &'a str,
    /// URL link for label
    label_link: &'a str,
    /// URL link for message
    msg_link: &'a str,
    /// color for label
    label_color: &'a str,
    /// color for message
    msg_color: &'a str,
    /// link to logo, placed on label side
    logo: &'a str,
    /// title of badge
    full_badge_title: &'a str,
    /// title for label
    label_title: &'a str,
    /// title for message
    msg_title: &'a str,
    /// height of the badge, in px
    badge_height: f32,
    /// logo width of the badge, in px
    logo_width: f32,
    /// logo placement in x
    logo_x: f32,
    /// logo placement in y
    logo_y: f32,
    /// width of text for label, in px
    label_text_width: f32,
    /// width of text for message, in px
    msg_text_width: f32,
    /// logo text placement in x
    label_text_x: f32,
    /// message text placement in x
    msg_text_x: f32,
    /// width of left side of badge
    left_width: f32,
    /// width of right side of badge
    right_width: f32,
}

/// Generate the SVG string corresponding to a Flat Square badge with this Badge info
pub(crate) fn flat_square_svg(badge: &Badge, layout: Layout) -> Result<String, BadgeError> {
    let mut logo_uri = badge.logo.clone();
    if badge.embed_logo {
        logo_uri = format_helper::attempt_logo_download(&badge.logo)?;
    }
    let flat_square_badge = BadgeTemplateFlatSquare {
        label_text: &layout.label_text_norm,
        msg_text: &layout.msg_text_norm,
        badge_link: &badge.badge_link,
        label_link: &badge.label_link,
        msg_link: &badge.msg_link,
        label_color: &layout.label_color,
        msg_color: &layout.msg_color,
        logo: &logo_uri,
        full_badge_title: &badge.badge_title,
        label_title: &badge.label_title,
        msg_title: &badge.msg_title,
        badge_height: layout.badge_height,
        logo_width: layout.logo_width,
        logo_x: layout.logo_x,
        logo_y: layout.logo_y,
        label_text_width: layout.label_text_width,
        msg_text_width: layout.msg_text_width,
        label_text_x: layout.label_text_x,
        msg_text_x: layout.msg_text_x,
        left_width: layout.label_total_width,
        right_width: layout.msg_total_width,
    };
    Ok(minify_svg_str(flat_square_badge.render().unwrap()))
}

#[derive(Template, Debug)]
#[template(path = "badge_template_forthebadge.xml", escape = "xml")]
/// Holds all information necessary for a "For the Badge" badge template.
struct BadgeTemplateForTheBadge<'a> {
    /// text for label (left side)
    label_text: &'a str,
    /// text for message (right side)
    msg_text: &'a str,
    /// link for entire badge
    badge_link: &'a str,
    /// URL link for label
    label_link: &'a str,
    /// URL link for message
    msg_link: &'a str,
    /// color for label
    label_color: &'a str,
    /// color for message
    msg_color: &'a str,
    /// link to logo, placed on label side
    logo: &'a str,
    /// title of badge
    full_badge_title: &'a str,
    /// title for label
    label_title: &'a str,
    /// title for message
    msg_title: &'a str,
    /// height of the badge, in px
    badge_height: f32,
    /// logo width of the badge, in px
    logo_width: f32,
    /// logo placement in x
    logo_x: f32,
    /// logo placement in y
    logo_y: f32,
    /// width of text for label, in px
    label_text_width: f32,
    /// width of text for message, in px
    msg_text_width: f32,
    /// logo text placement in x
    label_text_x: f32,
    /// message text placement in x
    msg_text_x: f32,
    /// width of left side of badge
    left_width: f32,
    /// width of right side of badge
    right_width: f32,
}

/// Generate the SVG string corresponding to a "for the badge" badge with this Badge info
pub(crate) fn for_the_badge_svg(badge: &Badge, layout: Layout) -> Result<String, BadgeError> {
    let mut logo_uri = badge.logo.clone();
    if badge.embed_logo {
        logo_uri = format_helper::attempt_logo_download(&badge.logo)?;
    }
    let forthebadge_badge = BadgeTemplateForTheBadge {
        label_text: &layout.label_text_norm,
        msg_text: &layout.msg_text_norm,
        badge_link: &badge.badge_link,
        label_link: &badge.label_link,
        msg_link: &badge.msg_link,
        label_color: &layout.label_color,
        msg_color: &layout.msg_color,
        logo: &logo_uri,
        full_badge_title: &badge.badge_title,
        label_title: &badge.label_title,
        msg_title: &badge.msg_title,
        badge_height: layout.badge_height,
        logo_width: layout.logo_width,
        logo_x: layout.logo_x,
        logo_y: layout.logo_y,
        label_text_width: layout.label_text_width,
        msg_text_width: layout.msg_text_width,
        label_text_x: layout.label_text_x,
        msg_text_x: layout.msg_text_x,
        left_width: layout.label_total_width,
        right_width: layout.msg_total_width,
    };
    Ok(minify_svg_str(forthebadge_badge.render().unwrap()))
}

#[derive(Template, Debug)]
#[template(path = "badge_template_social.xml", escape = "xml")]
/// Holds all information necessary for a Social badge template.
struct BadgeTemplateSocial<'a> {
    /// text for label (left side)
    label_text: &'a str,
    /// text for message (right side)
    msg_text: &'a str,
    /// link for entire badge
    badge_link: &'a str,
    /// URL link for label
    label_link: &'a str,
    /// URL link for message
    msg_link: &'a str,
    /// link to logo, placed on label side
    logo: &'a str,
    /// title of badge
    full_badge_title: &'a str,
    /// height of the badge, in px
    badge_height: f32,
    /// logo width of the badge, in px
    logo_width: f32,
    /// logo placement in x
    logo_x: f32,
    /// logo placement in y
    logo_y: f32,
    /// width of text for label, in px
    label_text_width: f32,
    /// width of text for message, in px
    msg_text_width: f32,
    /// logo text placement in x
    label_text_x: f32,
    /// message text placement in x
    msg_text_x: f32,
    /// width of left side of badge
    left_width: f32,
    /// width of right side of badge
    right_width: f32,
    /// unique ID for smooth gradient
    id_smooth: &'a str,
    /// unique ID for round shape
    id_round: &'a str,
    /// message bubble placement in x
    msg_bubble_x: f32,
    /// width of label bounding box
    label_rect_width: f32,
    /// width of message bounding box
    msg_rect_width: f32,
}

/// Generate the SVG string corresponding to a Social badge with this Badge info
pub(crate) fn social_svg(badge: &Badge, layout: Layout) -> Result<String, BadgeError> {
    let id_suffix: String = rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(7)
        .map(char::from)
        .collect();
    let id_smooth = format!("smooth{}", id_suffix);
    let id_round = format!("round{}", id_suffix);
    let mut logo_uri = badge.logo.clone();
    if badge.embed_logo {
        logo_uri = format_helper::attempt_logo_download(&badge.logo)?;
    }
    let social_badge = BadgeTemplateSocial {
        label_text: &layout.label_text_norm,
        msg_text: &layout.msg_text_norm,
        badge_link: &badge.badge_link,
        label_link: &badge.label_link,
        msg_link: &badge.msg_link,
        logo: &logo_uri,
        full_badge_title: &badge.badge_title,
        badge_height: layout.badge_height,
        logo_width: layout.logo_width,
        logo_x: layout.logo_x,
        logo_y: layout.logo_y,
        label_text_width: layout.label_text_width,
        msg_text_width: layout.msg_text_width,
        label_text_x: layout.label_text_x,
        msg_text_x: layout.msg_text_x,
        left_width: layout.label_total_width,
        right_width: layout.msg_total_width,
        id_smooth: &id_smooth,
        id_round: &id_round,
        msg_bubble_x: layout.msg_bubble_x,
        label_rect_width: layout.label_rect_width,
        msg_rect_width: layout.msg_rect_width,
    };
    Ok(minify_svg_str(social_badge.render().unwrap()))
}