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
//! Artwork information
use crate::error::Error;
use serde::{Deserialize, Serialize};
use tinytemplate::TinyTemplate;
/// Artwork information
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct Artwork {
/// Original image width in pixels
pub width: Option<u32>,
/// Original image height in pixels
pub height: Option<u32>,
/// Template image url
///
/// DO NOT USE FOR REQUESTS:
/// for getting the image use the [`Artwork::get_image`] method
pub url: String,
/// Text color 1 in rgb hex
#[serde(with = "crate::utils::hex::option", default)]
pub text_color_1: Option<u32>,
/// Text color 2 in rgb hex
#[serde(with = "crate::utils::hex::option", default)]
pub text_color_2: Option<u32>,
/// Text color 3 in rgb hex
#[serde(with = "crate::utils::hex::option", default)]
pub text_color_3: Option<u32>,
/// Text color 4 in rgb hex
#[serde(with = "crate::utils::hex::option", default)]
pub text_color_4: Option<u32>,
}
/// Artwork image formats
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ArtworkImageFormat {
/// Png image format,
Png,
/// Webp image format
Webp,
/// Jpeg image format
Jpeg,
}
impl ArtworkImageFormat {
fn get_format_string(&self) -> &str {
match self {
ArtworkImageFormat::Png => "png",
ArtworkImageFormat::Webp => "webp",
ArtworkImageFormat::Jpeg => "jpg",
}
}
}
impl Artwork {
/// Get artwork image url
///
/// # Parameters
///
/// * width - preferred width
///
/// * height - preferred height
///
/// * image_format - image format in which the image should be retrieved
pub fn get_image_url(
&self,
width: u32,
height: u32,
image_format: ArtworkImageFormat,
) -> Result<String, Error> {
let mut tt = TinyTemplate::new();
tt.add_template("url", &self.url)?;
#[derive(Serialize)]
struct UrlContext<'a> {
w: u32,
h: u32,
f: &'a str,
}
let context = UrlContext {
w: width,
h: height,
f: image_format.get_format_string(),
};
Ok(tt.render("url", &context)?)
}
}