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
use {
crate::{core::Config, utils::ResizedImageDetails},
serde::{Deserialize, Serialize},
std::{collections::HashMap, path::PathBuf},
};
type PathBufPictureRegister = HashMap<PathBuf, Picture>;
#[derive(Serialize, Deserialize, Debug)]
pub enum MediaWidth {
Max(String),
Min(String),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SourceAttributes {
pub media_width: MediaWidth,
pub srcset: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Picture {
pub sources: Vec<SourceAttributes>,
pub fallback_uri: String,
}
impl Picture {
pub fn from(
image_file_name: &PathBuf,
scaled_images_count: u8,
) -> Result<Self, String> {
if scaled_images_count == 0 {
return Err("scaled_images_count must be > 0".to_string());
}
let resized_image_details =
ResizedImageDetails::from(&image_file_name, scaled_images_count)?;
let mut sources = vec![];
let mut input_dir = image_file_name.clone();
input_dir.pop();
for details in &resized_image_details {
let out_file_name =
match input_dir.join(&details.output_file_name).to_str() {
Some(v) => String::from(v),
None => {
return Err(String::from(
"Could not convert output_file_name!",
))
}
};
sources.push(SourceAttributes {
media_width: MediaWidth::Max(details.width.to_string()),
srcset: out_file_name,
});
}
let mut full_scale_image = image_file_name.clone();
full_scale_image.set_extension("webp");
let full_scale_image = match full_scale_image.to_str() {
Some(v) => String::from(v),
None => {
return Err(String::from(
"Could not convert full_scale_image file name!",
))
}
};
sources.push(SourceAttributes {
media_width: MediaWidth::Min(
(resized_image_details.last().unwrap().width + 1).to_string(),
),
srcset: full_scale_image,
});
Ok(Self {
sources,
fallback_uri: image_file_name.to_str().unwrap().to_string(),
})
}
pub fn to_html_string(
&self,
srcset_prefix: Option<String>,
alt_text: &str,
) -> String {
let mut html = String::from("<picture>");
let uri_prefix = match &srcset_prefix {
Some(v) => format!("{}/", v),
None => String::new(),
};
for src_attrs in &self.sources {
let (min_max, value) = match &src_attrs.media_width {
MediaWidth::Max(v) => ("max", v),
MediaWidth::Min(v) => ("min", v),
};
html.push_str(&format!(
"<source media=\"({}-width: {}px)\" srcset=\"{}{}\">",
min_max, value, &uri_prefix, src_attrs.srcset
));
}
html.push_str(&format!(
"<img src=\"{}{}\" alt=\"{}\" />",
uri_prefix, self.fallback_uri, alt_text
));
html.push_str("</picture>");
html
}
}
#[derive(Debug)]
pub struct PictureRegister {
config: Config,
register: PathBufPictureRegister,
}
impl PictureRegister {
pub fn from(config: &Config) -> Result<Self, String> {
match &config.install_images_into {
None => {
return Err(
"The install_images_into parameter needs to be set!"
.to_string(),
)
}
Some(v) => {
if !v.is_dir() {
return Err("The install_images_into parameter is not a valid directory".to_string());
}
}
}
Ok(Self {
config: config.clone(),
register: Self::create_register(&config)?,
})
}
fn create_register(
config: &Config,
) -> Result<PathBufPictureRegister, String> {
let images_path = match &config.install_images_into {
None => {
return Err(
"The install_images_into parameter needs to be set!"
.to_string(),
)
}
Some(v) => {
if !v.is_dir() {
return Err("The install_images_into parameter is not a valid directory".to_string());
}
v
}
};
let mut register = PathBufPictureRegister::new();
let png_file_names = crate::collect_png_file_names(&images_path, None);
for png in png_file_names {
let pic = Picture::from(&png, config.scaled_images_count)?;
register.insert(png, pic);
}
Ok(register)
}
pub fn get(&self, image: &PathBuf) -> Result<&Picture, String> {
match self.register.get(image) {
None => Err("Image not found!".to_string()),
Some(v) => Ok(v),
}
}
}