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