src/config/search.rs
396 lines · 10583 bytes
1use crate::global::{
2 structs::Message,
3 traits::{AsUrlString, ConfigTrait, SearchFilterItem},
4};
5use serde::{Deserialize, Serialize};
6use typemap::Key;
7
8// can be turned into URL Params for the search term with filters
9/// Search query & filters
10#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
11pub struct Search {
12 pub query: String,
13 pub filters: SearchFilters,
14 pub page: u16,
15}
16
17impl Default for Search {
18 fn default() -> Self {
19 Self {
20 query: String::new(),
21 filters: SearchFilters::default(),
22 page: 1,
23 }
24 }
25}
26
27impl Key for Search {
28 type Value = Self;
29}
30
31impl ConfigTrait for Search {
32 const LABEL: &'static str = "search";
33}
34
35impl ToString for Search {
36 fn to_string(&self) -> String {
37 format!(
38 "{}&page={}",
39 vec![
40 format!("q={}", self.query),
41 self.filters.sort.as_url_string(),
42 self.filters.date.as_url_string(),
43 self.filters.duration.as_url_string(),
44 self.filters.r#type.as_url_string(),
45 ]
46 .join("&"),
47 self.page
48 )
49 }
50}
51
52/// Search filters, read comments for function docs
53#[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
54pub struct SearchFilters {
55 pub sort: SearchFilterSort,
56 pub date: SearchFilterDate,
57 pub duration: SearchFilterDuration,
58 pub r#type: SearchFilterType,
59}
60
61// These functions are used in the search filter item
62// Think of the ordering of stuff in the popup menu
63impl SearchFilters {
64 // This function returns [(Option name, [Options])]
65 // Option name is a string like "sory by" and "type"
66 // Options are the options that you can select like "relevance" and "upload date" for sorting
67 pub fn get_all() -> [(&'static str, Vec<&'static str>); 5] {
68 [
69 (SearchFilterSort::NAME, SearchFilterSort::ordering()),
70 (SearchFilterDate::NAME, SearchFilterDate::ordering()),
71 (SearchFilterDuration::NAME, SearchFilterDuration::ordering()),
72 (SearchFilterType::NAME, SearchFilterType::ordering()),
73 ("Reset filters", vec!["Are you sure?"]),
74 ]
75 }
76
77 // uses the index of the selected option name to get the index of the selected option
78 // aka using the hover location of the left textlist to get the hover location of the right text list
79 pub fn get_selected(&self, index: usize) -> usize {
80 match index {
81 0 => self.sort.selected_index(),
82 1 => self.date.selected_index(),
83 2 => self.duration.selected_index(),
84 3 => self.r#type.selected_index(),
85 4 => 0,
86 _ => unreachable!("`{index}` is not a valid index"),
87 }
88 }
89
90 // saves the settings for a filter given the index of the option (among other options) and the index of the selected options
91 // v left textlist v right textlist
92 pub fn set_index(&mut self, index_at: usize, set_index: usize, message: &mut Message) {
93 match index_at {
94 0 => self.sort = SearchFilterSort::at_index(set_index),
95 1 => self.date = SearchFilterDate::at_index(set_index),
96 2 => self.duration = SearchFilterDuration::at_index(set_index),
97 3 => self.r#type = SearchFilterType::at_index(set_index),
98 4 => {
99 if set_index == 0 {
100 self.reset(message)
101 } else {
102 unreachable!("index can only be `0` for option `4` (reset all)")
103 }
104 }
105 _ => unreachable!("`{index_at}` is not a valid index"),
106 }
107 }
108
109 pub fn reset(&mut self, message: &mut Message) {
110 *self = Self::default();
111 *message = Message::Success(String::from("Search filters has been reset"));
112 }
113}
114
115#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
116pub enum SearchFilterSort {
117 Relevance,
118 Rating,
119 Date,
120 Views,
121}
122
123#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
124pub enum SearchFilterDate {
125 None,
126 Hour,
127 Day,
128 Week,
129 Month,
130 Year,
131}
132
133#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
134pub enum SearchFilterDuration {
135 None,
136 Short,
137 Medium,
138 Long,
139}
140
141#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
142pub enum SearchFilterType {
143 All,
144 Video,
145 Channel,
146 Playlist,
147}
148
149impl Default for SearchFilterSort {
150 fn default() -> Self {
151 Self::Relevance
152 }
153}
154
155impl Default for SearchFilterDate {
156 fn default() -> Self {
157 Self::None
158 }
159}
160
161impl Default for SearchFilterDuration {
162 fn default() -> Self {
163 Self::None
164 }
165}
166
167impl Default for SearchFilterType {
168 fn default() -> Self {
169 Self::All
170 }
171}
172
173impl ToString for SearchFilterSort {
174 fn to_string(&self) -> String {
175 match self {
176 Self::Relevance => String::from("relevance"),
177 Self::Rating => String::from("rating"),
178 Self::Date => String::from("date"),
179 Self::Views => String::from("views"),
180 }
181 }
182}
183
184impl ToString for SearchFilterDate {
185 fn to_string(&self) -> String {
186 match self {
187 Self::None => String::from("none"),
188 Self::Hour => String::from("hour"),
189 Self::Day => String::from("day"),
190 Self::Week => String::from("week"),
191 Self::Month => String::from("month"),
192 Self::Year => String::from("year"),
193 }
194 }
195}
196
197impl ToString for SearchFilterDuration {
198 fn to_string(&self) -> String {
199 match self {
200 Self::None => String::from("none"),
201 Self::Short => String::from("short"),
202 Self::Medium => String::from("medium"),
203 Self::Long => String::from("long"),
204 }
205 }
206}
207
208impl ToString for SearchFilterType {
209 fn to_string(&self) -> String {
210 match self {
211 Self::All => String::from("all"),
212 Self::Video => String::from("video"),
213 Self::Channel => String::from("channel"),
214 Self::Playlist => String::from("playlist"),
215 }
216 }
217}
218
219impl AsUrlString for SearchFilterSort {
220 const TAG: &'static str = "sort";
221}
222
223impl AsUrlString for SearchFilterDate {
224 const TAG: &'static str = "date";
225}
226
227impl AsUrlString for SearchFilterDuration {
228 const TAG: &'static str = "duration";
229}
230
231impl AsUrlString for SearchFilterType {
232 const TAG: &'static str = "type";
233}
234
235const SORT_ORDERING: [SearchFilterSort; 4] = [
236 SearchFilterSort::Relevance,
237 SearchFilterSort::Rating,
238 SearchFilterSort::Date,
239 SearchFilterSort::Views,
240];
241const DATE_ORDERING: [SearchFilterDate; 6] = [
242 SearchFilterDate::None,
243 SearchFilterDate::Hour,
244 SearchFilterDate::Day,
245 SearchFilterDate::Week,
246 SearchFilterDate::Month,
247 SearchFilterDate::Year,
248];
249const DURATION_ORDERING: [SearchFilterDuration; 4] = [
250 SearchFilterDuration::None,
251 SearchFilterDuration::Short,
252 SearchFilterDuration::Medium,
253 SearchFilterDuration::Long,
254];
255const TYPE_ORDERING: [SearchFilterType; 4] = [
256 SearchFilterType::All,
257 SearchFilterType::Video,
258 SearchFilterType::Channel,
259 SearchFilterType::Playlist,
260];
261
262impl SearchFilterItem for SearchFilterSort {
263 const NAME: &'static str = "Sort by";
264
265 fn option_name(&self) -> &'static str {
266 match self {
267 Self::Relevance => "Relevance",
268 Self::Rating => "Rating",
269 Self::Date => "Upload date",
270 Self::Views => "View count",
271 }
272 }
273
274 fn ordering() -> Vec<&'static str> {
275 SORT_ORDERING
276 .iter()
277 .map(|item| item.option_name())
278 .collect::<Vec<_>>()
279 }
280
281 fn selected_index(&self) -> usize {
282 match self {
283 Self::Relevance => 0,
284 Self::Rating => 1,
285 Self::Date => 2,
286 Self::Views => 3,
287 }
288 }
289
290 fn at_index(index: usize) -> Self {
291 SORT_ORDERING[index]
292 }
293}
294
295impl SearchFilterItem for SearchFilterDate {
296 const NAME: &'static str = "Upload date";
297
298 fn option_name(&self) -> &'static str {
299 match self {
300 Self::None => "Any date",
301 Self::Hour => "Last hour",
302 Self::Day => "Today",
303 Self::Week => "This week",
304 Self::Month => "This month",
305 Self::Year => "This year",
306 }
307 }
308
309 fn ordering() -> Vec<&'static str> {
310 DATE_ORDERING
311 .iter()
312 .map(|item| item.option_name())
313 .collect::<Vec<_>>()
314 }
315
316 fn selected_index(&self) -> usize {
317 match self {
318 Self::None => 0,
319 Self::Hour => 1,
320 Self::Day => 2,
321 Self::Week => 3,
322 Self::Month => 4,
323 Self::Year => 5,
324 }
325 }
326
327 fn at_index(index: usize) -> Self {
328 DATE_ORDERING[index]
329 }
330}
331
332impl SearchFilterItem for SearchFilterDuration {
333 const NAME: &'static str = "Duration";
334
335 fn option_name(&self) -> &'static str {
336 match self {
337 Self::None => "Any duration",
338 Self::Short => "Short (< 4 minutes)",
339 Self::Medium => "Medium (4 - 20 minutes)",
340 Self::Long => "Long (> 20 minutes)",
341 }
342 }
343
344 fn ordering() -> Vec<&'static str> {
345 DURATION_ORDERING
346 .iter()
347 .map(|item| item.option_name())
348 .collect::<Vec<_>>()
349 }
350
351 fn selected_index(&self) -> usize {
352 match self {
353 Self::None => 0,
354 Self::Short => 1,
355 Self::Medium => 2,
356 Self::Long => 3,
357 }
358 }
359
360 fn at_index(index: usize) -> Self {
361 DURATION_ORDERING[index]
362 }
363}
364
365impl SearchFilterItem for SearchFilterType {
366 const NAME: &'static str = "Type";
367
368 fn option_name(&self) -> &'static str {
369 match self {
370 Self::All => "Any type",
371 Self::Video => "Video",
372 Self::Channel => "Channel",
373 Self::Playlist => "Playlist",
374 }
375 }
376
377 fn ordering() -> Vec<&'static str> {
378 TYPE_ORDERING
379 .iter()
380 .map(|item| item.option_name())
381 .collect::<Vec<_>>()
382 }
383
384 fn selected_index(&self) -> usize {
385 match self {
386 Self::All => 0,
387 Self::Video => 1,
388 Self::Channel => 2,
389 Self::Playlist => 3,
390 }
391 }
392
393 fn at_index(index: usize) -> Self {
394 TYPE_ORDERING[index]
395 }
396}